source: branches/version-2_13-dev/data/class/SC_Image.php @ 23124

Revision 23124, 8.8 KB checked in by kimoto, 11 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

  • 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    public $tmp_dir;
28
29    public function __construct($tmp_dir)
30    {
31        // ヘッダファイル読込
32        $this->tmp_dir = rtrim($tmp_dir, '/') . '/';
33    }
34
35    //--- 一時ファイル生成(サムネイル画像生成用)
36    public function makeTempImage($keyname, $max_width, $max_height)
37    {
38        // 一意なIDを取得する。
39        $mainname = uniqid('').'.';
40        // 拡張子以外を置き換える。
41        $newFileName = preg_replace("/^.*\./", $mainname, $_FILES[$keyname]['name']);
42        $result  = $this->MakeThumb($_FILES[$keyname]['tmp_name'], $this->tmp_dir , $max_width, $max_height, $newFileName);
43        GC_Utils_Ex::gfDebugLog($result);
44
45        return $newFileName;
46    }
47
48    //--- ファイルを指定保存DIRへ移動
49    public function moveTempImage($filename, $save_dir)
50    {
51        // コピー元ファイル、コピー先ディレクトリが存在する場合にのみ実行する
52        $from_path = $this->tmp_dir.$filename;
53        $to_path = $save_dir.'/'.$filename;
54        if (file_exists($from_path) && file_exists($save_dir)) {
55            if (copy($from_path , $to_path)) {
56                unlink($from_path);
57            }
58        } else {
59            GC_Utils_Ex::gfDebugLog($from_path.'->'.$to_path.'のcopyに失敗しました。');
60        }
61    }
62
63    //---- 指定ファイルを削除
64    public function deleteImage($filename, $dir)
65    {
66        if (file_exists($dir.'/'.$filename)) {
67            unlink($dir.'/'.$filename);
68        }
69    }
70
71    /**
72     * 指定サイズで画像を出力する.
73     *
74     * @param string  $FromImgPath ファイル名までのパス
75     * @param string  $ToImgPath   出力先パス
76     * @param integer $tmpMW       最大横幅
77     * @param integer $tmpMH       最大縦幅
78     * @param integer $newFileName 新ファイル名
79     * @param array 新ファイル名を格納した配列
80     */
81    public function MakeThumb($FromImgPath , $ToImgPath , $tmpMW , $tmpMH, $newFileName = '')
82    {
83        // 画像の最大横幅(単位:ピクセル)
84        $ThmMaxWidth = LARGE_IMAGE_WIDTH;
85
86        // 画像の最大縦幅(単位:ピクセル)
87        $ThmMaxHeight = LARGE_IMAGE_HEIGHT;
88
89        //拡張子取得
90        $array_ext = explode('.', $FromImgPath);
91        $ext = $array_ext[count($array_ext) - 1];
92
93        $MW = $ThmMaxWidth;
94        if ($tmpMW) $MW = $tmpMW; // $MWに最大横幅セット
95
96        $MH = $ThmMaxHeight;
97        if ($tmpMH) $MH = $tmpMH; // $MHに最大縦幅セット
98
99        if (empty($FromImgPath) || empty($ToImgPath)) {
100            return array(0,'出力元画像パス、または出力先フォルダが指定されていません。');
101        }
102
103        if (!file_exists($FromImgPath)) {
104            return array(0,'出力元画像が見つかりません。');
105        }
106
107        $size = @GetImageSize($FromImgPath);
108        $re_size = $size;
109
110        // 画像の種類が不明 or swf
111        if (!$size[2] || $size[2] > 3) {
112            return array(0,'画像形式がサポートされていません。');
113        }
114
115        //アスペクト比固定処理
116        $tmp_w = $size[0] / $MW;
117
118        if ($MH != 0) {
119            $tmp_h = $size[1] / $MH;
120        }
121
122        if ($tmp_w > 1 || $tmp_h > 1) {
123            if ($MH == 0) {
124                if ($tmp_w > 1) {
125                    $re_size[0] = $MW;
126                    $re_size[1] = $size[1] * $MW / $size[0];
127                }
128            } else {
129                if ($tmp_w > $tmp_h) {
130                    $re_size[0] = $MW;
131                    $re_size[1] = $size[1] * $MW / $size[0];
132                } else {
133                    $re_size[1] = $MH;
134                    $re_size[0] = $size[0] * $MH / $size[1];
135                }
136            }
137        }
138
139        // サムネイル画像ファイル名作成処理
140        $tmp = array_pop(explode('/',$FromImgPath)); // /の一番最後を切り出し
141        $FromFileName = array_shift(explode('.',$tmp)); // .で区切られた部分を切り出し
142        $ToFile = $FromFileName; // 拡張子以外の部分までを作成
143
144        $ImgNew = imagecreatetruecolor($re_size[0],$re_size[1]);
145
146        switch ($size[2]) {
147            case '1': //gif形式
148                if ($tmp_w <= 1 && $tmp_h <= 1) {
149                    if ($newFileName) {
150                        $ToFile = $newFileName;
151                    } elseif ($ext) {
152                        $ToFile .= '.' . $ext;
153                    } else {
154                        $ToFile .= '.gif';
155                    }
156                    if (!@copy($FromImgPath , $ToImgPath.$ToFile)) { // エラー処理
157
158                        return array(0,'ファイルのコピーに失敗しました。');
159                    }
160                    ImageDestroy($ImgNew);
161
162                    return array(1,$ToFile);
163                }
164
165                ImageColorAllocate($ImgNew,255,235,214); //背景色
166                $black = ImageColorAllocate($ImgNew,0,0,0);
167                $red = ImageColorAllocate($ImgNew,255,0,0);
168                Imagestring($ImgNew,4,5,5,"GIF $size[0]x$size[1]", $red);
169                ImageRectangle ($ImgNew,0,0,($re_size[0]-1),($re_size[1]-1),    $black);
170
171                if ($newFileName) {
172                    $ToFile = $newFileName;
173                } elseif ($ext) {
174                    $ToFile .= '.' . $ext;
175                } else {
176                    $ToFile .= '.png';
177                }
178                $TmpPath = $ToImgPath.$ToFile;
179                @Imagepng($ImgNew,$TmpPath);
180                // 画像が作成されていない場合
181                if (!@file_exists($TmpPath)) {
182                    return array(0,'画像の出力に失敗しました。');
183                }
184                ImageDestroy($ImgNew);
185
186                return array(1,$ToFile);
187
188            case '2': //jpg形式
189                $ImgDefault = ImageCreateFromJpeg($FromImgPath);
190                //ImageCopyResized($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
191
192                if ($re_size[0] != $size[0] || $re_size[0] != $size[0]) {
193                    ImageCopyResampled($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
194                }
195
196                GC_Utils_Ex::gfDebugLog($size);
197                GC_Utils_Ex::gfDebugLog($re_size);
198
199                if ($newFileName) {
200                    $ToFile = $newFileName;
201                } elseif ($ext) {
202                    $ToFile .= '.' . $ext;
203                } else {
204                    $ToFile .= '.jpg';
205                }
206                $TmpPath = $ToImgPath.$ToFile;
207                @ImageJpeg($ImgNew,$TmpPath);
208                // 画像が作成されていない場合
209                if (!@file_exists($TmpPath)) {
210                    return array(0,"画像の出力に失敗しました。<br>${ImgNew}<br>${TmpPath}");
211                }
212                $RetVal = $ToFile;
213                break;
214
215            case '3': //png形式
216                $ImgDefault = ImageCreateFromPNG($FromImgPath);
217                //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
218                ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
219
220                if ($newFileName) {
221                    $ToFile = $newFileName;
222                } elseif ($ext) {
223                    $ToFile .= '.' . $ext;
224                } else {
225                    $ToFile .= '.png';
226                }
227                $TmpPath = $ToImgPath.$ToFile;
228                @ImagePNG($ImgNew,$TmpPath);
229                // 画像が作成されていない場合
230                if (!@file_exists($TmpPath)) {
231                    return array(0,'画像の出力に失敗しました。');
232                }
233                $RetVal = $ToFile;
234                break;
235        }
236
237        ImageDestroy($ImgDefault);
238        ImageDestroy($ImgNew);
239
240        return array(1,$RetVal);
241    }
242}
Note: See TracBrowser for help on using the repository browser.