3 class NP_ImageCreateThumbnail extends NucleusPlugin {
8 * Copyright 2007 by Kai Greve
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.
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
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
26 * NP_ImageCreateThumbnail creates a thumbnail after an image is uploaded,
27 * it demonstrates the uages of the PostMediaUpload event
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.'; }
41 function getMinNucleusVersion() { return 330; }
43 function supportsFeature($what) {
45 { case 'SqlTablePrefix':
52 $this->createOption ('thumbsize', 'Maximal width (landscape format) or height (portrait format) for Thumbnails', 'text', '150');
55 function unInstall() {
58 function getEventList() {
59 return array('PostMediaUpload');
63 function event_PostMediaUpload(&$data) {
65 $collection = $data['collection'];
66 $mediadir = $data['mediadir'];
67 $filename = $data['filename'];
68 $fullpath = $mediadir.$filename;
70 // evaluate the filetype from the filename
71 $filetype = strtolower(substr($filename, strpos($filename, ".")+1));
74 if ($filetype=='jpg' || $filetype=='jpeg') {
76 $size = getimagesize($fullpath);
78 $ratio = $size[1]/$size[0]; // ratio = height / width
81 $new_height = $this->getOption('thumbsize') * $size[1]/$size[0];
82 $new_width = $this->getOption('thumbsize');
85 $new_height = $this->getOption('thumbsize');
86 $new_width = $this->getOption('thumbsize') * $size[0]/$size[1];
89 $image_orig = imagecreatefromjpeg($fullpath);
90 $image_new = imagecreatetruecolor($new_width, $new_height);
92 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
94 $thumbfilename = substr($fullpath, 0, strpos($fullpath, ".")).'_thumb.'.$filetype;
96 imagejpeg ($image_new , $thumbfilename);
99 imagedestroy($image_orig);
100 imagedestroy($image_new);
104 if ($filetype=='png') {
106 $size = getimagesize($fullpath);
108 $ratio = $size[1]/$size[0]; // ratio = height / width
111 $new_height = $this->getOption('thumbsize') * $size[1]/$size[0];
112 $new_width = $this->getOption('thumbsize');
115 $new_height = $this->getOption('thumbsize');
116 $new_width = $this->getOption('thumbsize') * $size[0]/$size[1];
119 $image_orig = imagecreatefrompng($fullpath);
120 $image_new = imagecreatetruecolor($new_width, $new_height);
122 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
124 $thumbfilename = substr($fullpath, 0, strpos($fullpath, ".")).'_thumb.'.$filetype;
126 imagepng ($image_new , $thumbfilename);
129 imagedestroy($image_orig);
130 imagedestroy($image_new);