OSDN Git Service

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