createOption('maxwidth', 'Maximal width for images', 'text', '450'); } function unInstall() { } function getEventList() { return array('PreMediaUpload'); } function event_PreMediaUpload(&$data) { $collection = $data['collection']; $uploadfile = $data['uploadfile']; $filename = $data['filename']; // evaluate the filetype from the filename $filetype = strtolower(substr($filename, strpos($filename, ".")+1)); // filetype is jpeg if ($filetype=='jpg' || $filetype=='jpeg') { $size=getimagesize($data['uploadfile']); // size[0] is the image width if ($size[0]>$this->getOption('maxwidth')) { $newheight = $this->getOption('maxwidth') * $size[1]/$size[0]; $image_orig = imagecreatefromjpeg($uploadfile); $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight); imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]); imagejpeg ($image_new , $uploadfile); // clear the memory imagedestroy($image_orig); imagedestroy($image_new); } } // filetype is png if ($filetype=='png') { $size=getimagesize($data['uploadfile']); // size[0] is the image width if ($size[0]>$this->getOption('maxwidth')) { $newheight = $this->getOption('maxwidth') * $size[1]/$size[0]; $image_orig = imagecreatefrompng($uploadfile); $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight); imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]); imagepng ( $image_new , $uploadfile); // clear the memory imagedestroy($image_orig); imagedestroy($image_new); } } } } ?>