OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / build / testcases / NP_ImageLimitSize.php
1 <?php
2
3 class NP_ImageLimitSize extends NucleusPlugin {
4 /*
5  Nucleus Plugin
6  
7  History:
8         v0.01 (2006-12-30):
9                 - release for testing purposes (demonstrates the usage of the PreMediaUpload event)
10 */
11
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.'; }
17
18         function getMinNucleusVersion() { return 330; }
19
20         function supportsFeature($what) {
21                 switch($what)
22                 { case 'SqlTablePrefix':
23                                 return 1;
24                         default:
25                                 return 0; }
26         }
27
28         function install() {
29                 $this->createOption('maxwidth', 'Maximal width for images', 'text', '450');     
30         }
31
32         function unInstall() {
33         }
34
35         function getEventList() {
36                 return array('PreMediaUpload');
37         }
38
39         function event_PreMediaUpload(&$data) {
40
41                 $collection = $data['collection'];
42                 $uploadfile = $data['uploadfile'];
43                 $filename = $data['filename'];
44                 
45                 // evaluate the filetype from the filename
46                 $filetype = strtolower(substr($filename, strpos($filename, ".")+1));
47                 
48                 // filetype is jpeg
49                 if ($filetype=='jpg' || $filetype=='jpeg') {
50                 
51                         $size=getimagesize($data['uploadfile']);
52                         
53                         // size[0] is the image width           
54                         if ($size[0]>$this->getOption('maxwidth')) {
55
56                                 $newheight =  $this->getOption('maxwidth') * $size[1]/$size[0];
57                         $image_orig = imagecreatefromjpeg($uploadfile);
58                                 $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight);
59                 
60                                 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]);
61
62                                 imagejpeg ($image_new , $uploadfile);
63                                 
64                                 // clear the memory
65                                 imagedestroy($image_orig);
66                                 imagedestroy($image_new);
67                 
68                         }
69                 }
70                 
71                 // filetype is png
72                 if ($filetype=='png') {
73                 
74                         $size=getimagesize($data['uploadfile']);
75                 
76                         // size[0] is the image width
77                         if ($size[0]>$this->getOption('maxwidth')) {
78                 
79                                 $newheight =  $this->getOption('maxwidth') * $size[1]/$size[0];
80                         $image_orig = imagecreatefrompng($uploadfile);
81                                 $image_new = imagecreatetruecolor($this->getOption('maxwidth'), $newheight);
82
83                                 imagecopyresampled($image_new, $image_orig, 0, 0, 0, 0, $this->getOption('maxwidth'), $newheight, $size[0], $size[1]);
84                 
85                                 imagepng ( $image_new , $uploadfile);
86                                 
87                                 // clear the memory
88                                 imagedestroy($image_orig);
89                                 imagedestroy($image_new);
90
91                         }
92                 }
93         }
94 }
95
96 ?>