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

Revision 21864, 8.8 KB checked in by h_yoshimoto, 12 years ago (diff)

#1831 Copyrightを更新

  • 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-2012 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        if (!$ext) {
88            $array_ext = explode('.', $FromImgPath);
89            $ext = $array_ext[count($array_ext) - 1];
90        }
91
92        $MW = $ThmMaxWidth;
93        if ($tmpMW) $MW = $tmpMW; // $MWに最大横幅セット
94
95        $MH = $ThmMaxHeight;
96        if ($tmpMH) $MH = $tmpMH; // $MHに最大縦幅セット
97
98        if (empty($FromImgPath) || empty($ToImgPath)) {
99            return array(0,'出力元画像パス、または出力先フォルダが指定されていません。');
100        }
101
102        if (!file_exists($FromImgPath)) {
103            return array(0,'出力元画像が見つかりません。');
104        }
105
106        $size = @GetImageSize($FromImgPath);
107        $re_size = $size;
108
109        // 画像の種類が不明 or swf
110        if (!$size[2] || $size[2] > 3) {
111            return array(0,'画像形式がサポートされていません。');
112        }
113
114        //アスペクト比固定処理
115        $tmp_w = $size[0] / $MW;
116
117        if ($MH != 0) {
118            $tmp_h = $size[1] / $MH;
119        }
120
121        if ($tmp_w > 1 || $tmp_h > 1) {
122            if ($MH == 0) {
123                if ($tmp_w > 1) {
124                    $re_size[0] = $MW;
125                    $re_size[1] = $size[1] * $MW / $size[0];
126                }
127            } else {
128                if ($tmp_w > $tmp_h) {
129                    $re_size[0] = $MW;
130                    $re_size[1] = $size[1] * $MW / $size[0];
131                } else {
132                    $re_size[1] = $MH;
133                    $re_size[0] = $size[0] * $MH / $size[1];
134                }
135            }
136        }
137
138        // サムネイル画像ファイル名作成処理
139        $tmp = array_pop(explode('/',$FromImgPath)); // /の一番最後を切り出し
140        $FromFileName = array_shift(explode('.',$tmp)); // .で区切られた部分を切り出し
141        $ToFile = $PreWord.$FromFileName; // 拡張子以外の部分までを作成
142
143        $ImgNew = imagecreatetruecolor($re_size[0],$re_size[1]);
144
145        switch ($size[2]) {
146            case '1': //gif形式
147                if ($tmp_w <= 1 && $tmp_h <= 1) {
148                    if ($newFileName) {
149                        $ToFile = $newFileName;
150                    } elseif ($ext) {
151                        $ToFile .= '.' . $ext;
152                    } else {
153                        $ToFile .= '.gif';
154                    }
155                    if (!@copy($FromImgPath , $ToImgPath.$ToFile)) { // エラー処理
156                        return array(0,'ファイルのコピーに失敗しました。');
157                    }
158                    ImageDestroy($ImgNew);
159                    return array(1,$ToFile);
160                }
161
162                ImageColorAllocate($ImgNew,255,235,214); //背景色
163                $black = ImageColorAllocate($ImgNew,0,0,0);
164                $red = ImageColorAllocate($ImgNew,255,0,0);
165                Imagestring($ImgNew,4,5,5,"GIF $size[0]x$size[1]", $red);
166                ImageRectangle ($ImgNew,0,0,($re_size[0]-1),($re_size[1]-1),    $black);
167
168                if ($newFileName) {
169                    $ToFile = $newFileName;
170                } elseif ($ext) {
171                    $ToFile .= '.' . $ext;
172                } else {
173                    $ToFile .= '.png';
174                }
175                $TmpPath = $ToImgPath.$ToFile;
176                @Imagepng($ImgNew,$TmpPath);
177                // 画像が作成されていない場合
178                if (!@file_exists($TmpPath)) {
179                    return array(0,'画像の出力に失敗しました。');
180                }
181                ImageDestroy($ImgNew);
182                return array(1,$ToFile);
183
184            case '2': //jpg形式
185                $ImgDefault = ImageCreateFromJpeg($FromImgPath);
186                //ImageCopyResized($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
187
188                if ($re_size[0] != $size[0] || $re_size[0] != $size[0]) {
189                    ImageCopyResampled($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
190                }
191
192                GC_Utils_Ex::gfDebugLog($size);
193                GC_Utils_Ex::gfDebugLog($re_size);
194
195                if ($newFileName) {
196                    $ToFile = $newFileName;
197                } elseif ($ext) {
198                    $ToFile .= '.' . $ext;
199                } else {
200                    $ToFile .= '.jpg';
201                }
202                $TmpPath = $ToImgPath.$ToFile;
203                @ImageJpeg($ImgNew,$TmpPath);
204                // 画像が作成されていない場合
205                if (!@file_exists($TmpPath)) {
206                    return array(0,"画像の出力に失敗しました。<br>${ImgNew}<br>${TmpPath}");
207                }
208                $RetVal = $ToFile;
209                break;
210
211            case '3': //png形式
212                $ImgDefault = ImageCreateFromPNG($FromImgPath);
213                //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
214                ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
215
216                if ($newFileName) {
217                    $ToFile = $newFileName;
218                } elseif ($ext) {
219                    $ToFile .= '.' . $ext;
220                } else {
221                    $ToFile .= '.png';
222                }
223                $TmpPath = $ToImgPath.$ToFile;
224                @ImagePNG($ImgNew,$TmpPath);
225                // 画像が作成されていない場合
226                if (!@file_exists($TmpPath)) {
227                    return array(0,'画像の出力に失敗しました。');
228                }
229                $RetVal = $ToFile;
230                break;
231        }
232
233        ImageDestroy($ImgDefault);
234        ImageDestroy($ImgNew);
235
236        return array(1,$RetVal);
237    }
238}
Note: See TracBrowser for help on using the repository browser.