source: branches/version-2_5-dev/data/module/gdthumb.php @ 20617

Revision 20617, 9.3 KB checked in by nanasess, 13 years ago (diff)

#991 (商品規格登録での不具合)

  • r20612 の続き. ファイルアップロードに対応

#595 (不正なファイルアップロードが行われる可能性)

  • move_uploaded_file() を使用するよう修正
  • 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
4◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
5GD自動サムネイル作成 + 中川修正2006/02/03
6
7Copyright 2002- Akihiro Asai. All rights reserved.
8
9http://aki.adam.ne.jp
10aki@mx3.adam.ne.jp
11
12◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
13
14□ 機能概要
15・指定されたイメージのサムネイルを表示します。
16・出力する大きさを指定する事ができます。
17・出力されるイメージのアスペクト比は維持されます。
18
19□ 使用方法
20指定は gdthumb.php?path=xxx/xxx.[ jpg | png | gif ]&mw=xx&mh=xx
21※ passの部分には画像へのパスを指定
22※ mwに表示画像の最大横幅、mhに表示画像の最大横幅を外部より指定可能。
23※ 指定しなかった場合はデフォルトの設定値を採用。
24★クラスとして使用する場合は、「クラスとして使用する場合には・・・」以降をコメントアウトして下さい。
25
26□ 更新履歴
272002/08/19 最大縦幅の部分を一部手直し
282003/01/31 デフォルトでアスペクト比が固定
292003/04/11 最大横幅と最大縦幅を外部より指定可能
302003/04/25 GD2用に関数変更
312003/06/21 GD1/2をバージョンに応じて変更できるように修正
322003/06/25 imageCopyResampledの部分を修正
332004/01/28 スクリプト全体を書き直し。引数「pass」を「path」に変更。
342005/12/08 関数の自動判別 gif形式に対応 透過gif・透過pngに対応(GD2.0.1以降) 
35*/
36
37// クラスとして使用する場合には、以下の6行をコメントアウト
38/*
39$objg = new gdthumb();
40list($Ck, $Msg) = $objg->Main($_GET["path"], $_GET["mw"], $_GET["mh"]);
41if(!$Ck) { // エラーの場合
42    header("Content-Type: text/html; charset=" . CHAR_CODE);
43    print $Msg;
44}
45*/
46
47class gdthumb {
48   
49    var $imgMaxWidth;
50    var $imgMaxHeight;
51    var $gdVer;
52   
53    /*
54    * コンストラクタ
55    */
56    function gdthumb() {
57       
58        // スクリプトのデフォルト設定
59       
60        // 画像の最大横幅
61        $this->imgMaxWidth = 240; // 1以上の値
62       
63        // 画像の最大縦幅
64        $this->imgMaxHeight = 0; // 指定しない場合は0 指定する場合は1以上の値
65       
66    }
67   
68    /*
69    * サムネイル画像の作成
70    * string $path
71    * integer $width
72    * integer $height
73    */
74    function Main($path, $width, $height, $dst_file, $header = false) {
75       
76        if(!isset($path)) {
77            return array(0, "イメージのパスが設定されていません。");
78        }
79       
80        if(!file_exists($path)) {
81            return array(0, "指定されたパスにファイルが見つかりません。");
82        }
83       
84        // 画像の大きさをセット
85        if($width) $this->imgMaxWidth = $width;
86        if($height) $this->imgMaxHeight = $height;
87       
88        $size = @GetimageSize($path);
89        $re_size = $size;
90       
91        //アスペクト比固定処理
92        if($this->imgMaxWidth != 0) {
93            $tmp_w = $size[0] / $this->imgMaxWidth;
94        }
95       
96        if($this->imgMaxHeight != 0) {
97            $tmp_h = $size[1] / $this->imgMaxHeight;
98        }
99       
100        if($tmp_w > 1 || $tmp_h > 1) {
101            if($this->imgMaxHeight == 0) {
102                if($tmp_w > 1) {
103                    $re_size[0] = $this->imgMaxWidth;
104                    $re_size[1] = $size[1] * $this->imgMaxWidth / $size[0];
105                }
106            } else {
107                if($tmp_w > $tmp_h) {
108                    $re_size[0] = $this->imgMaxWidth;
109                    $re_size[1] = $size[1] * $this->imgMaxWidth / $size[0];
110                } else {
111                    $re_size[1] = $this->imgMaxHeight;
112                    $re_size[0] = $size[0] * $this->imgMaxHeight / $size[1];
113                }
114            }
115        }
116       
117        $imagecreate = function_exists("imagecreatetruecolor") ? "imagecreatetruecolor" : "imagecreate";
118        $imageresize = function_exists("imagecopyresampled") ? "imagecopyresampled" : "imagecopyresized";
119
120        switch($size[2]) {
121           
122            // gif形式
123            case "1":
124                if(function_exists("imagecreatefromgif")) {
125                    $src_im = imagecreatefromgif($path);
126                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
127                   
128                    $transparent = imagecolortransparent($src_im);
129                    $colorstotal = imagecolorstotal ($src_im);
130                   
131                    $dst_im = imagecreate($re_size[0], $re_size[1]);
132                    if (0 <= $transparent && $transparent < $colorstotal) {
133                        imagepalettecopy ($dst_im, $src_im);
134                        imagefill ($dst_im, 0, 0, $transparent);
135                        imagecolortransparent ($dst_im, $transparent);
136                    }
137                    $imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
138
139                    if(function_exists("imagegif")) {                       
140                        // 画像出力
141                        if($header){
142                            header("Content-Type: image/gif");
143                            imagegif($dst_im);
144                            return "";
145                        }else{
146                            $dst_file = $dst_file . ".gif";
147                            if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
148                                // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)           
149                                if (!move_uploaded_file($path, $dst_file)) {
150                                    return array(0, "アップロードに失敗しました。");
151                                }
152                            } else {
153                                imagegif($dst_im, $dst_file);
154                            }
155                        }                       
156                        imagedestroy($src_im);
157                        imagedestroy($dst_im);
158                    } else {
159                        // 画像出力
160                        if($header){
161                            header("Content-Type: image/png");
162                            imagepng($dst_im);
163                            return "";
164                        }else{
165                            $dst_file = $dst_file . ".png";
166                            if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
167                                // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)           
168                                if (!move_uploaded_file($path, $dst_file)) {
169                                    return array(0, "アップロードに失敗しました。");
170                                }
171                            } else {
172                                imagepng($dst_im, $dst_file);
173                            }
174                        }
175                        imagedestroy($src_im);
176                        imagedestroy($dst_im);
177                    }
178                } else {
179                    // サムネイル作成不可の場合(旧バージョン対策)
180                    $dst_im = imageCreate($re_size[0], $re_size[1]);
181                    imageColorAllocate($dst_im, 255, 255, 214); //背景色
182                   
183                    // 枠線と文字色の設定
184                    $black = imageColorAllocate($dst_im, 0, 0, 0);
185                    $red = imageColorAllocate($dst_im, 255, 0, 0);
186                   
187                    imagestring($dst_im, 5, 10, 10, "GIF $size[0]x$size[1]", $red);
188                    imageRectangle ($dst_im, 0, 0, ($re_size[0]-1), ($re_size[1]-1), $black);
189                   
190                    // 画像出力
191                    if($header){
192                        header("Content-Type: image/png");
193                        imagepng($dst_im);
194                        return "";
195                    }else{
196                        $dst_file = $dst_file . ".png";
197                        imagepng($dst_im, $dst_file);
198                    }
199                    imagedestroy($src_im);
200                    imagedestroy($dst_im);
201                }
202                break;
203               
204            // jpg形式
205            case "2":
206           
207                $src_im = imageCreateFromJpeg($path);
208                $dst_im = $imagecreate($re_size[0], $re_size[1]);
209               
210                $imageresize( $dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
211
212                // 画像出力
213                if($header){
214                    header("Content-Type: image/jpeg");
215                    imageJpeg($dst_im);
216                    return "";
217                }else{
218                    $dst_file = $dst_file . ".jpg";
219                   
220                    if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
221                        // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)       
222                        if (!move_uploaded_file($path, $dst_file)) {
223                            return array(0, "アップロードに失敗しました。");
224                        }
225                    } else {
226                        imageJpeg($dst_im, $dst_file);
227                    }
228                }
229               
230                imagedestroy($src_im);
231                imagedestroy($dst_im);
232               
233                break;
234   
235            // png形式   
236            case "3":
237
238                $src_im = imageCreateFromPNG($path);
239               
240                $colortransparent = imagecolortransparent($src_im);
241                if ($colortransparent > -1) {
242                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
243                    imagepalettecopy($dst_im, $src_im);
244                    imagefill($dst_im, 0, 0, $colortransparent);
245                    imagecolortransparent($dst_im, $colortransparent);
246                    imagecopyresized($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
247                } else {               
248                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
249                    imagecopyresized($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
250                   
251                    (imagecolorstotal($src_im) == 0) ? $colortotal = 65536 : $colortotal = imagecolorstotal($src_im);
252                   
253                    imagetruecolortopalette($dst_im, true, $colortotal);
254                }
255               
256                // 画像出力
257                if($header){
258                    header("Content-Type: image/png");
259                    imagepng($dst_im);
260                    return "";
261                }else{
262                    $dst_file = $dst_file . ".png";
263                    if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
264                        // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)           
265                        if (!move_uploaded_file($path, $dst_file)) {
266                            return array(0, "アップロードに失敗しました。");
267                        }
268                    } else {
269                        imagepng($dst_im, $dst_file);
270                    }
271                }
272                imagedestroy($src_im);
273                imagedestroy($dst_im);
274               
275                break;
276               
277            default:
278                return array(0, "イメージの形式が不明です。");
279        }
280
281        return array(1, $dst_file);
282    }
283}
284?>
Note: See TracBrowser for help on using the repository browser.