source: branches/feature-module-update/data/class/SC_Image.php @ 16582

Revision 16582, 8.6 KB checked in by nanasess, 16 years ago (diff)

ライセンス表記変更

  • 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-2007 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        if(file_exists($this->tmp_dir.$filename) && file_exists($save_dir)) {
53            if(copy($this->tmp_dir . $filename , $save_dir."/".$filename)) {
54                unlink( $this->tmp_dir . $filename );
55            }
56        } else {
57            GC_Utils_Ex::gfDebugLog($this->tmp_dir.$filename."の移動に失敗しました。");
58        }
59    }
60
61    //---- 指定ファイルを削除
62    function deleteImage($filename, $dir) {
63        if(file_exists($dir."/".$filename)) {
64            unlink($dir."/".$filename);
65        }
66    }
67
68    /**
69     * 指定サイズで画像を出力する.
70     *
71     * @param string $FromImgPath ファイル名までのパス
72     * @param string $ToImgPath 出力先パス
73     * @param integer $tmpMW 最大横幅
74     * @param integer $tmpMH 最大縦幅
75     * @param integer $newFileName 新ファイル名
76     * @param array 新ファイル名を格納した配列
77     */
78    function MakeThumb($FromImgPath , $ToImgPath , $tmpMW , $tmpMH, $newFileName = ''){
79        // 画像の最大横幅(単位:ピクセル)
80        $ThmMaxWidth = LARGE_IMAGE_WIDTH;
81
82        // 画像の最大縦幅(単位:ピクセル)
83        $ThmMaxHeight = LARGE_IMAGE_HEIGHT;
84
85        //サムネイル画像の接頭文字
86        $PreWord = $head;
87
88            //拡張子取得
89            if (!$ext) {
90                $array_ext = explode(".", $FromImgPath);
91                $ext = $array_ext[count($array_ext) - 1];
92            }
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,"出力元画像パス、または出力先フォルダが指定されていません。");
102        }
103
104        if(!file_exists($FromImgPath)){
105            return array(0,"出力元画像が見つかりません。");
106        }
107
108        $size = @GetImageSize($FromImgPath);
109        $re_size = $size;
110
111        if(!$size[2] || $size[2] > 3){ // 画像の種類が不明 or swf
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 = $PreWord.$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                    return array(0,"ファイルのコピーに失敗しました。");
158                }
159                ImageDestroy($ImgNew);
160                return array(1,$ToFile);
161            }
162
163            ImageColorAllocate($ImgNew,255,235,214); //背景色
164            $black = ImageColorAllocate($ImgNew,0,0,0);
165            $red = ImageColorAllocate($ImgNew,255,0,0);
166            Imagestring($ImgNew,4,5,5,"GIF $size[0]x$size[1]", $red);
167            ImageRectangle ($ImgNew,0,0,($re_size[0]-1),($re_size[1]-1),    $black);
168
169            if ( $newFileName ) {
170                $ToFile = $newFileName;
171            } elseif($ext) {
172                $ToFile .= "." . $ext;
173            } else {
174                $ToFile .= ".png";
175            }
176            $TmpPath = $ToImgPath.$ToFile;
177            @Imagepng($ImgNew,$TmpPath);
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
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.