source: branches/version-2_12-multilang/data/class/SC_Image.php @ 22433

Revision 22433, 8.7 KB checked in by kim, 11 years ago (diff)

#2060 r22372,r22381,r22382,r22386-r22390,r22397-r22400,r22407-r22411,r22416,r22417,r22420,r22421,r22423,r22425,r22426 を差し戻す。

  • 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            $tokens = array(
56                'T_FROM' => $from_path,
57                'T_TO' => $to_path
58            );
59            GC_Utils_Ex::gfDebugLog(t('SC_Image_002', $tokens));
60        }
61    }
62
63    //---- 指定ファイルを削除
64    function deleteImage($filename, $dir) {
65        if (file_exists($dir.'/'.$filename)) {
66            unlink($dir.'/'.$filename);
67        }
68    }
69
70    /**
71     * 指定サイズで画像を出力する.
72     *
73     * @param string $FromImgPath ファイル名までのパス
74     * @param string $ToImgPath 出力先パス
75     * @param integer $tmpMW 最大横幅
76     * @param integer $tmpMH 最大縦幅
77     * @param integer $newFileName 新ファイル名
78     * @param array 新ファイル名を格納した配列
79     */
80    function MakeThumb($FromImgPath , $ToImgPath , $tmpMW , $tmpMH, $newFileName = '') {
81        // 画像の最大横幅(単位:ピクセル)
82        $ThmMaxWidth = LARGE_IMAGE_WIDTH;
83
84        // 画像の最大縦幅(単位:ピクセル)
85        $ThmMaxHeight = LARGE_IMAGE_HEIGHT;
86
87        //サムネイル画像の接頭文字
88        $PreWord = $head;
89
90        //拡張子取得
91        $array_ext = explode('.', $FromImgPath);
92        $ext = $array_ext[count($array_ext) - 1];
93
94        $MW = $ThmMaxWidth;
95        if ($tmpMW) $MW = $tmpMW; // $MWに最大横幅セット
96
97        $MH = $ThmMaxHeight;
98        if ($tmpMH) $MH = $tmpMH; // $MHに最大縦幅セット
99
100        if (empty($FromImgPath) || empty($ToImgPath)) {
101            return array(0,  t('SC_Image_003'));
102        }
103
104        if (!file_exists($FromImgPath)) {
105            return array(0,  t('SC_Image_004'));
106        }
107
108        $size = @GetImageSize($FromImgPath);
109        $re_size = $size;
110
111        // 画像の種類が不明 or swf
112        if (!$size[2] || $size[2] > 3) {
113            return array(0,  t('SC_Image_005'));
114        }
115
116        //アスペクト比固定処理
117        $tmp_w = $size[0] / $MW;
118
119        if ($MH != 0) {
120            $tmp_h = $size[1] / $MH;
121        }
122
123        if ($tmp_w > 1 || $tmp_h > 1) {
124            if ($MH == 0) {
125                if ($tmp_w > 1) {
126                    $re_size[0] = $MW;
127                    $re_size[1] = $size[1] * $MW / $size[0];
128                }
129            } else {
130                if ($tmp_w > $tmp_h) {
131                    $re_size[0] = $MW;
132                    $re_size[1] = $size[1] * $MW / $size[0];
133                } else {
134                    $re_size[1] = $MH;
135                    $re_size[0] = $size[0] * $MH / $size[1];
136                }
137            }
138        }
139
140        // サムネイル画像ファイル名作成処理
141        $tmp = array_pop(explode('/',$FromImgPath)); // /の一番最後を切り出し
142        $FromFileName = array_shift(explode('.',$tmp)); // .で区切られた部分を切り出し
143        $ToFile = $PreWord.$FromFileName; // 拡張子以外の部分までを作成
144
145        $ImgNew = imagecreatetruecolor($re_size[0],$re_size[1]);
146
147        switch ($size[2]) {
148            case '1': //gif形式
149                if ($tmp_w <= 1 && $tmp_h <= 1) {
150                    if ($newFileName) {
151                        $ToFile = $newFileName;
152                    } elseif ($ext) {
153                        $ToFile .= '.' . $ext;
154                    } else {
155                        $ToFile .= '.gif';
156                    }
157                    if (!@copy($FromImgPath , $ToImgPath.$ToFile)) { // エラー処理
158                        return array(0,  t('SC_Image_001'));
159                    }
160                    ImageDestroy($ImgNew);
161                    return array(1,$ToFile);
162                }
163
164                ImageColorAllocate($ImgNew,255,235,214); //背景色
165                $black = ImageColorAllocate($ImgNew,0,0,0);
166                $red = ImageColorAllocate($ImgNew,255,0,0);
167                Imagestring($ImgNew,4,5,5,"GIF $size[0]x$size[1]", $red);
168                ImageRectangle ($ImgNew,0,0,($re_size[0]-1),($re_size[1]-1),    $black);
169
170                if ($newFileName) {
171                    $ToFile = $newFileName;
172                } elseif ($ext) {
173                    $ToFile .= '.' . $ext;
174                } else {
175                    $ToFile .= '.png';
176                }
177                $TmpPath = $ToImgPath.$ToFile;
178                @Imagepng($ImgNew,$TmpPath);
179                // 画像が作成されていない場合
180                if (!@file_exists($TmpPath)) {
181                    return array(0,  t('SC_Image_006'));
182                }
183                ImageDestroy($ImgNew);
184                return array(1,$ToFile);
185
186            case '2': //jpg形式
187                $ImgDefault = ImageCreateFromJpeg($FromImgPath);
188                //ImageCopyResized($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
189
190                if ($re_size[0] != $size[0] || $re_size[0] != $size[0]) {
191                    ImageCopyResampled($ImgNew,$ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
192                }
193
194                GC_Utils_Ex::gfDebugLog($size);
195                GC_Utils_Ex::gfDebugLog($re_size);
196
197                if ($newFileName) {
198                    $ToFile = $newFileName;
199                } elseif ($ext) {
200                    $ToFile .= '.' . $ext;
201                } else {
202                    $ToFile .= '.jpg';
203                }
204                $TmpPath = $ToImgPath.$ToFile;
205                @ImageJpeg($ImgNew,$TmpPath);
206                // 画像が作成されていない場合
207                if (!@file_exists($TmpPath)) {
208                    return array(0,  t('SC_Image_006')."<br>${ImgNew}<br>${TmpPath}");
209                }
210                $RetVal = $ToFile;
211                break;
212
213            case '3': //png形式
214                $ImgDefault = ImageCreateFromPNG($FromImgPath);
215                //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
216                ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
217
218                if ($newFileName) {
219                    $ToFile = $newFileName;
220                } elseif ($ext) {
221                    $ToFile .= '.' . $ext;
222                } else {
223                    $ToFile .= '.png';
224                }
225                $TmpPath = $ToImgPath.$ToFile;
226                @ImagePNG($ImgNew,$TmpPath);
227                // 画像が作成されていない場合
228                if (!@file_exists($TmpPath)) {
229                    return array(0,  t('SC_Image_006'));
230                }
231                $RetVal = $ToFile;
232                break;
233        }
234
235        ImageDestroy($ImgDefault);
236        ImageDestroy($ImgNew);
237
238        return array(1,$RetVal);
239    }
240}
Note: See TracBrowser for help on using the repository browser.