source: branches/comu-ver2/data/module/gdthumb.php @ 15668

Revision 15668, 8.7 KB checked in by nanasess, 17 years ago (diff)

moved

  • 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                                copy($path, $dst_file);
150                            } else {
151                                imagegif($dst_im, $dst_file);
152                            }
153                        }                       
154                        imagedestroy($src_im);
155                        imagedestroy($dst_im);
156                    } else {
157                        // 画像出力
158                        if($header){
159                            header("Content-Type: image/png");
160                            imagepng($dst_im);
161                            return "";
162                        }else{
163                            $dst_file = $dst_file . ".png";
164                            if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
165                                // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)           
166                                copy($path, $dst_file);
167                            } else {
168                                imagepng($dst_im, $dst_file);
169                            }
170                        }
171                        imagedestroy($src_im);
172                        imagedestroy($dst_im);
173                    }
174                } else {
175                    // サムネイル作成不可の場合(旧バージョン対策)
176                    $dst_im = imageCreate($re_size[0], $re_size[1]);
177                    imageColorAllocate($dst_im, 255, 255, 214); //背景色
178                   
179                    // 枠線と文字色の設定
180                    $black = imageColorAllocate($dst_im, 0, 0, 0);
181                    $red = imageColorAllocate($dst_im, 255, 0, 0);
182                   
183                    imagestring($dst_im, 5, 10, 10, "GIF $size[0]x$size[1]", $red);
184                    imageRectangle ($dst_im, 0, 0, ($re_size[0]-1), ($re_size[1]-1), $black);
185                   
186                    // 画像出力
187                    if($header){
188                        header("Content-Type: image/png");
189                        imagepng($dst_im);
190                        return "";
191                    }else{
192                        $dst_file = $dst_file . ".png";
193                        imagepng($dst_im, $dst_file);
194                    }
195                    imagedestroy($src_im);
196                    imagedestroy($dst_im);
197                }
198                break;
199               
200            // jpg形式
201            case "2":
202           
203                $src_im = imageCreateFromJpeg($path);
204                $dst_im = $imagecreate($re_size[0], $re_size[1]);
205               
206                $imageresize( $dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
207
208                // 画像出力
209                if($header){
210                    header("Content-Type: image/jpeg");
211                    imageJpeg($dst_im);
212                    return "";
213                }else{
214                    $dst_file = $dst_file . ".jpg";
215                   
216                    if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
217                        // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)       
218                        copy($path, $dst_file);
219                    } else {
220                        imageJpeg($dst_im, $dst_file);
221                    }
222                }
223               
224                imagedestroy($src_im);
225                imagedestroy($dst_im);
226               
227                break;
228   
229            // png形式   
230            case "3":
231
232                $src_im = imageCreateFromPNG($path);
233               
234                $colortransparent = imagecolortransparent($src_im);
235                if ($colortransparent > -1) {
236                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
237                    imagepalettecopy($dst_im, $src_im);
238                    imagefill($dst_im, 0, 0, $colortransparent);
239                    imagecolortransparent($dst_im, $colortransparent);
240                    imagecopyresized($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
241                } else {               
242                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
243                    imagecopyresized($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
244                   
245                    (imagecolorstotal($src_im) == 0) ? $colortotal = 65536 : $colortotal = imagecolorstotal($src_im);
246                   
247                    imagetruecolortopalette($dst_im, true, $colortotal);
248                }
249               
250                // 画像出力
251                if($header){
252                    header("Content-Type: image/png");
253                    imagepng($dst_im);
254                    return "";
255                }else{
256                    $dst_file = $dst_file . ".png";
257                    if($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
258                        // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ)           
259                        copy($path, $dst_file);
260                    } else {
261                        imagepng($dst_im, $dst_file);
262                    }
263                }
264                imagedestroy($src_im);
265                imagedestroy($dst_im);
266               
267                break;
268               
269            default:
270                return array(0, "イメージの形式が不明です。");
271        }
272
273        return array(1, $dst_file);
274    }
275}
276?>
Note: See TracBrowser for help on using the repository browser.