source: temp/trunk/data/lib/gdthumb.php @ 9598

Revision 9598, 6.7 KB checked in by kakinaka, 20 years ago (diff)

blank

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2/*
3
4¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡¢¡
5GD¼«Æ°¥µ¥à¥Í¥¤¥ëºîÀ® + ÃæÀÀµ2006/02/03
6
7Copyright 2002- Akihiro Asai. All rights reserved.
8
9http://aki.adam.ne.jp
10[email protected]
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                            sfprintr(imagecolorstotal(imageCreateFromGIF($path)));
120
121        switch($size[2]) {
122           
123            // gif·Á¼°
124            case "1":
125                if(function_exists("imagecreatefromgif")) {
126                    $src_im = imagecreatefromgif($path);
127                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
128                   
129                    $transparent = imagecolortransparent($src_im);
130                    $colorstotal = imagecolorstotal ($src_im);
131                   
132                    $dst_im = imagecreate($re_size[0], $re_size[1]);
133                    if (0 <= $transparent && $transparent < $colorstotal) {
134                        imagepalettecopy ($dst_im, $src_im);
135                        imagefill ($dst_im, 0, 0, $transparent);
136                        imagecolortransparent ($dst_im, $transparent);
137                    }
138                    $imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
139
140                    if(function_exists("imagegif")) {
141                       
142                        // ²èÁü½ÐÎÏ
143                        if($header){
144                            header("Content-Type: image/gif");
145                            imagegif($dst_im);
146                            return "";
147                        }else{
148                            $dst_file = $dst_file . ".gif";
149                            imagegif($dst_im, $dst_file);
150                        }
151                       
152                        imagedestroy($src_im);
153                        imagedestroy($dst_im);
154                    } else {
155                        // ²èÁü½ÐÎÏ
156                        if($header){
157                            header("Content-Type: image/png");
158                            imagepng($dst_im);
159                            return "";
160                        }else{
161                            $dst_file = $dst_file . ".png";                     
162                            imagepng($dst_im, $dst_file);
163                        }
164                        imagedestroy($src_im);
165                        imagedestroy($dst_im);
166                    }
167                } else {
168                    // ¥µ¥à¥Í¥¤¥ëºîÀ®ÉԲĤξì¹ç¡Êµì¥Ð¡¼¥¸¥ç¥óÂкö¡Ë
169                    $dst_im = imageCreate($re_size[0], $re_size[1]);
170                    imageColorAllocate($dst_im, 255, 255, 214); //ÇØ·Ê¿§
171                   
172                    // ÏÈÀþ¤Èʸ»ú¿§¤ÎÀßÄê
173                    $black = imageColorAllocate($dst_im, 0, 0, 0);
174                    $red = imageColorAllocate($dst_im, 255, 0, 0);
175                   
176                    imagestring($dst_im, 5, 10, 10, "GIF $size[0]x$size[1]", $red);
177                    imageRectangle ($dst_im, 0, 0, ($re_size[0]-1), ($re_size[1]-1), $black);
178                   
179                    // ²èÁü½ÐÎÏ
180                    if($header){
181                        header("Content-Type: image/png");
182                        imagepng($dst_im);
183                        return "";
184                    }else{
185                        $dst_file = $dst_file . ".png";
186                        imagepng($dst_im, $dst_file);
187                    }
188                    imagedestroy($src_im);
189                    imagedestroy($dst_im);
190                }
191                break;
192               
193            // jpg·Á¼°
194            case "2":
195           
196                $src_im = imageCreateFromJpeg($path);
197                $dst_im = $imagecreate($re_size[0], $re_size[1]);
198                $imageresize( $dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
199               
200                // ²èÁü½ÐÎÏ
201                if($header){
202                    header("Content-Type: image/jpeg");
203                    imageJpeg($dst_im);
204                    return "";
205                }else{
206                    $dst_file = $dst_file . ".jpg";
207                    imageJpeg($dst_im, $dst_file);
208                }
209               
210                imagedestroy($src_im);
211                imagedestroy($dst_im);
212               
213                break;
214   
215            // png·Á¼°   
216            case "3":
217
218                $src_im = imageCreateFromPNG($path);
219               
220                $colortransparent = imagecolortransparent($src_im);
221                if ($colortransparent > -1) {
222                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
223                    imagepalettecopy($dst_im, $src_im);
224                    imagefill($dst_im, 0, 0, $colortransparent);
225                    imagecolortransparent($dst_im, $colortransparent);
226                    imagecopyresized($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
227                } else {               
228                    $dst_im = $imagecreate($re_size[0], $re_size[1]);
229                    imagecopyresized($dst_im,$src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
230                   
231                    imagetruecolortopalette($dst_im, false, imagecolorstotal($src_im));
232                }
233               
234                // ²èÁü½ÐÎÏ
235                if($header){
236                    header("Content-Type: image/png");
237                    imagepng($dst_im);
238                    return "";
239                }else{
240                    $dst_file = $dst_file . ".png";
241                    imagepng($dst_im, $dst_file);
242                }
243                imagedestroy($src_im);
244                imagedestroy($dst_im);
245               
246                break;
247               
248            default:
249                return array(0, "¥¤¥á¡¼¥¸¤Î·Á¼°¤¬ÉÔÌÀ¤Ç¤¹¡£");
250        }
251
252        return array(1, $dst_file);
253    }
254}
255?>
Note: See TracBrowser for help on using the repository browser.