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

Revision 15669, 7.8 KB checked in by nanasess, 17 years ago (diff)

thumb.php を SC_Image 内に移動

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