OSDN Git Service

FIX:変数名の誤記を修正
[nucleus-jp/nucleus-next.git] / build / testcases / NP_ImageCreateThumbnail.php
1 <?php\r
2 \r
3 class NP_ImageCreateThumbnail extends NucleusPlugin {\r
4 \r
5    /* \r
6         * Nucleus Plugin\r
7         *\r
8         * Copyright 2007 by Kai Greve\r
9         * \r
10         * This program is free software and open source software; you can redistribute\r
11         * it and/or modify it under the terms of the GNU General Public License as\r
12         * published by the Free Software Foundation; either version 2 of the License,\r
13         * or (at your option) any later version.\r
14         *\r
15         * This program is distributed in the hope that it will be useful, but WITHOUT\r
16         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         * more details.\r
19         *\r
20         * You should have received a copy of the GNU General Public License along\r
21         * with this program; if not, write to the Free Software Foundation, Inc.,\r
22         * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit\r
23         * http://www.gnu.org/licenses/gpl.html\r
24         * \r
25         *\r
26         * NP_ImageCreateThumbnail creates a thumbnail after an image is uploaded,\r
27         * it demonstrates the uages of the PostMediaUpload event                        \r
28         *\r
29         * History:\r
30         * v0.01: 2007-01-01\r
31         *       - initial release       \r
32         * \r
33         */\r
34 \r
35         function getName()                { return 'NP_ImageCreateThumbnail'; }\r
36         function getAuthor()      { return 'Kai Greve'; }\r
37         function getURL()                 { return 'http://www.nucleuscms.org/'; }\r
38         function getVersion()     { return '0.01'; }\r
39         function getDescription() { return 'Generates Thumbnails after an image is uploaded.';  }\r
40         \r
41         function getMinNucleusVersion() { return 330; }\r
42 \r
43         function supportsFeature($what) {\r
44                 switch($what)\r
45                 { case 'SqlTablePrefix':\r
46                                 return 1;\r
47                         default:\r
48                                 return 0; }\r
49         }\r
50 \r
51         function install() {\r
52                 $this->createOption ('thumbsize', 'Maximal width (landscape format) or height (portrait format) for Thumbnails', 'text', '150');\r
53         }\r
54 \r
55         function unInstall() {\r
56         }\r
57 \r
58         function getEventList() {\r
59                 return array('PostMediaUpload');\r
60         }\r
61 \r
62 \r
63         function event_PostMediaUpload(&$data) {\r
64                 \r
65                 $collection = $data['collection'];\r
66         $mediadir = $data['mediadir'];\r
67         $filename = $data['filename'];\r
68         $fullpath = $mediadir.$filename;\r
69         \r
70         // evaluate the filetype from the filename\r
71                 $filetype = strtolower(substr($filename, strpos($filename, ".")+1));\r
72                 \r
73                 // filetype is jpeg\r
74                 if ($filetype=='jpg' || $filetype=='jpeg') {\r
75                 \r
76                         $size = getimagesize($fullpath);\r
77                         \r
78                         $ratio = $size[1]/$size[0]; // ratio = height / width\r
79                         \r
80                         if ($ratio < 1) {\r
81                                 $new_height = $this->getOption('thumbsize') * $size[1]/$size[0];\r
82                                 $new_width = $this->getOption('thumbsize');\r
83                         }\r
84                         else {\r
85                                 $new_height = $this->getOption('thumbsize');\r
86                                 $new_width = $this->getOption('thumbsize') * $size[0]/$size[1];\r
87                         }                       \r
88 \r
89                 $image_orig = imagecreatefromjpeg($fullpath);\r
90                         $image_new = imagecreatetruecolor($new_width, $new_height);\r
91                 \r
92                         imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);\r
93 \r
94                         $thumbfilename = substr($fullpath, 0, strpos($fullpath, ".")).'_thumb.'.$filetype;\r
95                                 \r
96                         imagejpeg ($image_new , $thumbfilename);\r
97                                 \r
98                         // clear the memory\r
99                         imagedestroy($image_orig);\r
100                         imagedestroy($image_new);\r
101                 }\r
102                 \r
103                 // filetype is png\r
104                 if ($filetype=='png') {\r
105                 \r
106                         $size = getimagesize($fullpath);\r
107                         \r
108                         $ratio = $size[1]/$size[0]; // ratio = height / width\r
109                         \r
110                         if ($ratio < 1) {\r
111                                 $new_height = $this->getOption('thumbsize') * $size[1]/$size[0];\r
112                                 $new_width = $this->getOption('thumbsize');\r
113                         }\r
114                         else {\r
115                                 $new_height = $this->getOption('thumbsize');\r
116                                 $new_width = $this->getOption('thumbsize') * $size[0]/$size[1];\r
117                         }                       \r
118 \r
119                 $image_orig = imagecreatefrompng($fullpath);\r
120                         $image_new = imagecreatetruecolor($new_width, $new_height);\r
121                 \r
122                         imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);\r
123 \r
124                         $thumbfilename = substr($fullpath, 0, strpos($fullpath, ".")).'_thumb.'.$filetype;\r
125                                 \r
126                         imagepng ($image_new , $thumbfilename);\r
127                                 \r
128                         // clear the memory\r
129                         imagedestroy($image_orig);\r
130                         imagedestroy($image_new);\r
131                 }\r
132         }\r
133 }\r
134 \r
135 ?>\r