source: branches/version-2_5-dev/data/class/SC_Image.php @ 20764

Revision 20764, 8.6 KB checked in by nanasess, 13 years ago (diff)

#601 (コピーライトの更新)

  • 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-2011 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 SC_Image($tmp_dir) {
30        // ヘッダファイル読込
31        if (!ereg("/$", $tmp_dir)) {
32            $this->tmp_dir = $tmp_dir . "/";
33        } else {
34            $this->tmp_dir = $tmp_dir;
35        }
36    }
37
38    //--- 一時ファイル生成(サムネイル画像生成用)
39    function makeTempImage($keyname, $max_width, $max_height) {
40        // 一意なIDを取得する。
41        $mainname = uniqid("").".";
42        // 拡張子以外を置き換える。
43        $newFileName = ereg_replace("^.*\.",$mainname, $_FILES[$keyname]['name']);
44        $result  = $this->MakeThumb($_FILES[$keyname]['tmp_name'], $this->tmp_dir , $max_width, $max_height, $newFileName);
45        GC_Utils_Ex::gfDebugLog($result);
46        return $newFileName;
47    }
48
49    //--- ファイルを指定保存DIRへ移動
50    function moveTempImage($filename, $save_dir) {
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    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        if (!$ext) {
92            $array_ext = explode(".", $FromImgPath);
93            $ext = $array_ext[count($array_ext) - 1];
94        }
95
96        $MW = $ThmMaxWidth;
97        if ($tmpMW) $MW = $tmpMW; // $MWに最大横幅セット
98
99        $MH = $ThmMaxHeight;
100        if ($tmpMH) $MH = $tmpMH; // $MHに最大縦幅セット
101
102        if (empty($FromImgPath) || empty($ToImgPath)){
103            return array(0,"出力元画像パス、または出力先フォルダが指定されていません。");
104        }
105
106        if (!file_exists($FromImgPath)){
107            return array(0,"出力元画像が見つかりません。");
108        }
109
110        $size = @GetImageSize($FromImgPath);
111        $re_size = $size;
112
113        if (!$size[2] || $size[2] > 3){ // 画像の種類が不明 or swf
114            return array(0,"画像形式がサポートされていません。");
115        }
116
117        //アスペクト比固定処理
118        $tmp_w = $size[0] / $MW;
119
120        if ($MH != 0){
121            $tmp_h = $size[1] / $MH;
122        }
123
124        if ($tmp_w > 1 || $tmp_h > 1){
125            if ($MH == 0){
126                if ($tmp_w > 1){
127                    $re_size[0] = $MW;
128                    $re_size[1] = $size[1] * $MW / $size[0];
129                }
130            } else {
131                if ($tmp_w > $tmp_h){
132                    $re_size[0] = $MW;
133                    $re_size[1] = $size[1] * $MW / $size[0];
134                } else {
135                    $re_size[1] = $MH;
136                    $re_size[0] = $size[0] * $MH / $size[1];
137                }
138            }
139        }
140
141        // サムネイル画像ファイル名作成処理
142        $tmp = array_pop(explode("/",$FromImgPath)); // /の一番最後を切り出し
143        $FromFileName = array_shift(explode(".",$tmp)); // .で区切られた部分を切り出し
144        $ToFile = $PreWord.$FromFileName; // 拡張子以外の部分までを作成
145
146        $ImgNew = imagecreatetruecolor($re_size[0],$re_size[1]);
147
148        switch($size[2]) {
149        case "1": //gif形式
150            if ($tmp_w <= 1 && $tmp_h <= 1){
151                if ($newFileName) {
152                    $ToFile = $newFileName;
153                } elseif  ($ext) {
154                    $ToFile .= "." . $ext;
155                } else {
156                    $ToFile .= ".gif";
157                }
158                if (!@copy($FromImgPath , $ToImgPath.$ToFile)) { // エラー処理
159                    return array(0,"ファイルのコピーに失敗しました。");
160                }
161                ImageDestroy($ImgNew);
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            if (!@file_exists($TmpPath)){ // 画像が作成されていない場合
181                return array(0,"画像の出力に失敗しました。");
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            if (!@file_exists($TmpPath)){ // 画像が作成されていない場合
207                return array(0,"画像の出力に失敗しました。<br>${ImgNew}<br>${TmpPath}");
208            }
209            $RetVal = $ToFile;
210            break;
211
212        case "3": //png形式
213            $ImgDefault = ImageCreateFromPNG($FromImgPath);
214            //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
215            ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0,$re_size[0], $re_size[1],$size[0], $size[1]);
216
217            if ($newFileName) {
218                $ToFile = $newFileName;
219            } elseif ($ext) {
220                $ToFile .= "." . $ext;
221            } else {
222                $ToFile .= ".png";
223            }
224            $TmpPath = $ToImgPath.$ToFile;
225            @ImagePNG($ImgNew,$TmpPath );
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}
239?>
Note: See TracBrowser for help on using the repository browser.