3 class NP_ImageLimitSize extends NucleusPlugin {
\r
9 - release for testing purposes (demonstrates the usage of the PreMediaUpload event)
\r
13 function getName() { return 'NP_ImageLimitSize'; }
\r
14 function getAuthor() { return 'Kai Greve'; }
\r
15 function getURL() { return 'http://www.nucleuscms.org/'; }
\r
16 function getVersion() { return '0.01'; }
\r
17 function getDescription() { return 'Rescales an image (jpg/png) during the upload if it is bigger then a maximum with.'; }
\r
19 function getMinNucleusVersion() { return 330; }
\r
21 function supportsFeature($what) {
\r
23 { case 'SqlTablePrefix':
\r
29 function install() {
\r
30 $this->createOption('maxwidth', 'Maximal width for images', 'text', '450');
\r
33 function unInstall() {
\r
36 function getEventList() {
\r
37 return array('PreMediaUpload');
\r
40 function event_PreMediaUpload(&$data) {
\r
42 $collection = $data['collection'];
\r
43 $uploadfile = $data['uploadfile'];
\r
44 $filename = $data['filename'];
\r
46 // evaluate the filetype from the filename
\r
47 $filetype = strtolower(substr($filename, strpos($filename, ".")+1));
\r
50 if ($filetype=='jpg' || $filetype=='jpeg') {
\r
52 $size=getimagesize($data['uploadfile']);
\r
54 // size[0] is the image width
\r
55 if ($size[0]>$this->getOption('maxwidth')) {
\r
57 $newheight = $this->getOption('maxwidth') * $size[1]/$size[0];
\r
58 $image_orig = imagecreatefromjpeg($uploadfile);
\r
59 $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight);
\r
61 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]);
\r
63 imagejpeg ($image_new , $uploadfile);
\r
66 imagedestroy($image_orig);
\r
67 imagedestroy($image_new);
\r
73 if ($filetype=='png') {
\r
75 $size=getimagesize($data['uploadfile']);
\r
77 // size[0] is the image width
\r
78 if ($size[0]>$this->getOption('maxwidth')) {
\r
80 $newheight = $this->getOption('maxwidth') * $size[1]/$size[0];
\r
81 $image_orig = imagecreatefrompng($uploadfile);
\r
82 $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight);
\r
84 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]);
\r
86 imagepng ( $image_new , $uploadfile);
\r
89 imagedestroy($image_orig);
\r
90 imagedestroy($image_new);
\r