OSDN Git Service

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