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