source: branches/version-2_12-dev/data/class/SC_Image.php @ 22206

Revision 22206, 8.8 KB checked in by kim, 11 years ago (diff)

#2003 copyrightを2013に更新

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24//---- アップロードファイル加工クラス(thumb.phpとセットで使用する)
25class SC_Image {
26
27    var $tmp_dir;
28
29    function __construct($tmp_dir) {
30        // ヘッダファイル読込
31        $this->tmp_dir = rtrim($tmp_dir, '/') . '/';
32    }
33
34    //--- 一時ファイル生成(サムネイル画像生成用)
35    function makeTempImage($keyname, $max_width, $max_height) {
36        // 一意なIDを取得する。
37        $mainname = uniqid('').'.';
38        // 拡張子以外を置き換える。
39        $newFileName = preg_replace("/^.*\./", $mainname, $_FILES[$keyname]['name']);
40        $result  = $this->MakeThumb($_FILES[$keyname]['tmp_name'], $this->tmp_dir , $max_width, $max_height, $newFileName);
41        GC_Utils_Ex::gfDebugLog($result);
42        return $newFileName;
43    }
44
45    //--- ファイルを指定保存DIRへ移動
46    function moveTempImage($filename, $save_dir) {
47        // コピー元ファイル、コピー先ディレクトリが存在する場合にのみ実行する
48        $from_path = $this->tmp_dir.$filename;
49        $to_path = $save_dir.'/'.$filename;
50        if (file_exists($from_path) && file_exists($save_dir)) {
51            if (copy($from_path , $to_path)) {
52                unlink($from_path);
53            }
54        } else {
55            GC_Utils_Ex::gfDebugLog($from_path.'->'.$to_path.'のcopyに失敗しました。');
56        }
57    }
58
59    //---- 指定ファイルを削除
60    function deleteImage($filename, $dir) {
61        if (file_exists($dir.'/'.$filename)) {
62            unlink($dir.'/'.$filename);
63        }
64    }
65
66    /**
67     * 指定サイズで画像を出力する.
68     *
69     * @param string $FromImgPath ファイル名までのパス
70     * @param string $ToImgPath 出力先パス
71     * @param integer $tmpMW 最大横幅
72     * @param integer $tmpMH 最大縦幅
73     * @param integer $newFileName 新ファイル名
74     * @param array 新ファイル名を格納した配列
75     */
76    function MakeThumb($FromImgPath , $ToImgPath , $tmpMW , $tmpMH, $newFileName = '') {
77        // 画像の最大横幅(単位:ピクセル)
78        $ThmMaxWidth = LARGE_IMAGE_WIDTH;
79
80        // 画像の最大縦幅(単位:ピクセル)
81        $ThmMaxHeight = LARGE_IMAGE_HEIGHT;
82
83        //サムネイル画像の接頭文字
84        $PreWord = $head;
85
86        //拡張子取得
87        $array_ext = explode('.', $FromImgPath);
88        $ext = $array_ext[count($array_ext) - 1];
89
90        $MW = $ThmMaxWidth;
91        if ($tmpMW) $MW = $tmpMW; // $MWに最大横幅セット
92
93        $MH = $ThmMaxHeight;
94        if ($tmpMH) $MH = $tmpMH; // $MHに最大縦幅セット
95
96        if (empty($FromImgPath) || empty($ToImgPath)) {
97            return array(0,'出力元画像パス、または出力先フォルダが指定されていません。');
98        }
99
100        if (!file_exists($FromImgPath)) {
101            return array(0,'出力元画像が見つかりません。');
102        }
103
104        $size = @GetImageSize($FromImgPath);
105        $re_size = $size;
106
107        // 画像の種類が不明 or swf
108        if (!$size[2] || $size[2] > 3) {
109            return array(0,'画像形式がサポートされていません。');
110        }
111
112        //アスペクト比固定処理
113        $tmp_w = $size[0] / $MW;
114
115        if ($MH != 0) {
116            $tmp_h = $size[1] / $MH;
117        }
118
119        if ($tmp_w > 1 || $tmp_h > 1) {
120            if ($MH == 0) {
121                if ($tmp_w > 1) {
122                    $re_size[0] = $MW;
123                    $re_size[1] = $size[1] * $MW / $size[0];
124                }
125            } else {
126                if ($tmp_w > $tmp_h) {
127                    $re_size[0] = $MW;
128                    $re_size[1] = $size[1] * $MW / $size[0];
129                } else {
130                    $re_size[1] = $MH;
131                    $re_size[0] = $size[0] * $MH / $size[1];
132                }
133            }
134        }
135
136        // サムネイル画像ファイル名作成処理
137        $tmp = array_pop(explode('/',$FromImgPath)); // /の一番最後を切り出し
138        $FromFileName = array_shift(explode('.',$tmp)); // .で区切られた部分を切り出し
139        $ToFile = $PreWord.$FromFileName; // 拡張子以外の部分までを作成
140
141        $ImgNew = imagecreatetruecolor($re_size[0],$re_size[1]);
142
143        switch ($size[2]) {
144            case '1': //gif形式
145                if ($tmp_w <= 1 && $tmp_h <= 1) {
146                    if ($newFileName) {
147                        $ToFile = $newFileName;
148                    } elseif ($ext) {
149                        $ToFile .= '.' . $ext;
150                    } else {
151                        $ToFile .= '.gif';
152                    }
153                    if (!@copy($FromImgPath , $ToImgPath.$ToFile)) { // エラー処理
154                        return array(0,'ファイルのコピーに失敗しました。');
155                    }
156                    ImageDestroy($ImgNew);
157                    return array(1,$ToFile);
158                }
159
160                ImageColorAllocate($ImgNew,255,235,214); //背景色
161                $black = ImageColorAllocate($ImgNew,0,0,0);
162                $red = ImageColorAllocate($ImgNew,255,0,0);
163                Imagestring($ImgNew,4,5,5,"GIF $size[0]x$size[1]", $red);
164                ImageRectangle ($ImgNew,0,0,($re_size[0]-1),($re_size[1]-1),    $black);
165
166                if ($newFileName) {
167                    $ToFile = $newFileName;
168                } elseif ($ext) {
169                    $ToFile .= '.' . $ext;
170                } else {
171                    $ToFile .= '.png';
172                }
173                $TmpPath = $ToImgPath.$ToFile;
174                @Imagepng($ImgNew,$TmpPath);
175                // 画像が作成されていない場合
176                if (!@file_exists($TmpPath)) {
177                    return array(0,'画像の出力に失敗しました。');
178                }
179                ImageDestroy($ImgNew);
180                return array(1,$ToFile);
181
182            case '2': //jpg形式
183                $ImgDefault = ImageCreateFromJpeg($FromImgPath);
184                //ImageCopyResized($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
185
186                if ($re_size[0] != $size[0] || $re_size[0] != $size[0]) {
187                    ImageCopyResampled($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
188                }
189
190                GC_Utils_Ex::gfDebugLog($size);
191                GC_Utils_Ex::gfDebugLog($re_size);
192
193                if ($newFileName) {
194                    $ToFile = $newFileName;
195                } elseif ($ext) {
196                    $ToFile .= '.' . $ext;
197                } else {
198                    $ToFile .= '.jpg';
199                }
200                $TmpPath = $ToImgPath.$ToFile;
201                @ImageJpeg($ImgNew,$TmpPath);
202                // 画像が作成されていない場合
203                if (!@file_exists($TmpPath)) {
204                    return array(0,"画像の出力に失敗しました。<br>${ImgNew}<br>${TmpPath}");
205                }
206                $RetVal = $ToFile;
207                break;
208
209            case '3': //png形式
210                $ImgDefault = ImageCreateFromPNG($FromImgPath);
211                //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
212                ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
213
214                if ($newFileName) {
215                    $ToFile = $newFileName;
216                } elseif ($ext) {
217                    $ToFile .= '.' . $ext;
218                } else {
219                    $ToFile .= '.png';
220                }
221                $TmpPath = $ToImgPath.$ToFile;
222                @ImagePNG($ImgNew,$TmpPath);
223                // 画像が作成されていない場合
224                if (!@file_exists($TmpPath)) {
225                    return array(0,'画像の出力に失敗しました。');
226                }
227                $RetVal = $ToFile;
228                break;
229        }
230
231        ImageDestroy($ImgDefault);
232        ImageDestroy($ImgNew);
233
234        return array(1,$RetVal);
235    }
236}
Note: See TracBrowser for help on using the repository browser.