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

Revision 21481, 8.6 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

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