3 class NP_ImageLimitSize extends NucleusPlugin {
9 - release for testing purposes (demonstrates the usage of the PreMediaUpload event)
12 function getName() { return 'NP_ImageLimitSize'; }
13 function getAuthor() { return 'Kai Greve'; }
14 function getURL() { return 'http://www.nucleuscms.org/'; }
15 function getVersion() { return '0.01'; }
16 function getDescription() { return 'Rescales an image (jpg/png) during the upload if it is bigger then a maximum with.'; }
18 function getMinNucleusVersion() { return 330; }
20 function supportsFeature($what) {
22 { case 'SqlTablePrefix':
29 $this->createOption('maxwidth', 'Maximal width for images', 'text', '450');
32 function unInstall() {
35 function getEventList() {
36 return array('PreMediaUpload');
39 function event_PreMediaUpload(&$data) {
41 $collection = $data['collection'];
42 $uploadfile = $data['uploadfile'];
43 $filename = $data['filename'];
45 // evaluate the filetype from the filename
46 $filetype = strtolower(substr($filename, strpos($filename, ".")+1));
49 if ($filetype=='jpg' || $filetype=='jpeg') {
51 $size=getimagesize($data['uploadfile']);
53 // size[0] is the image width
54 if ($size[0]>$this->getOption('maxwidth')) {
56 $newheight = $this->getOption('maxwidth') * $size[1]/$size[0];
57 $image_orig = imagecreatefromjpeg($uploadfile);
58 $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight);
60 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]);
62 imagejpeg ($image_new , $uploadfile);
65 imagedestroy($image_orig);
66 imagedestroy($image_new);
72 if ($filetype=='png') {
74 $size=getimagesize($data['uploadfile']);
76 // size[0] is the image width
77 if ($size[0]>$this->getOption('maxwidth')) {
79 $newheight = $this->getOption('maxwidth') * $size[1]/$size[0];
80 $image_orig = imagecreatefrompng($uploadfile);
81 $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight);
83 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]);
85 imagepng ( $image_new , $uploadfile);
88 imagedestroy($image_orig);
89 imagedestroy($image_new);