OSDN Git Service

FIX: NP_MediaUtilsをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / NP_MediaUtils / mediautils / Medium.php
1 <?php
2 /**
3  * MediaUtils plugin for Nucleus CMS
4  * Version 1.0.0 for PHP5
5  * Written By Mocchi, Oct. 20, 2011
6  * 
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  */
12
13 class Medium
14 {
15         public $root = '';
16         public $path = '';
17         public $name = '';
18         public $update = '';
19         public $prefix = '';
20         public $suffix = '';
21         public $filename = '';
22         public $size = 0;
23         public $mime = '';
24         public $width = 0;
25         public $height = 0;
26         public $resampledwidth = 0;
27         public $resampledheight = 0;
28         
29         /**
30          * construct instance of Medium
31          * @access      Public  $this->__construnct
32          * @param       String  $root
33          * @param       String  $relativepath
34          * @param       Boolean $prefix
35          * @return      Object/FALSE
36          */
37         public function __construct($root, $relativepath, $prefix)
38         {
39                 static $fullpath;
40                 static $info;
41                 
42                 if ( $root == '' || $relativepath == '' )
43                 {
44                         return FALSE;
45                 }
46                 
47                 $root = preg_replace('#/*$#', '', $root);
48                 if ( $root == '' || $relativepath == ''
49                  || !file_exists($root)
50                  || FALSE === ($fullpath = realpath(rtrim($root . '/' . ltrim($relativepath, '/'), '/')))
51                  || strpos($fullpath, $root) !== 0
52                  || !file_exists($fullpath) )
53                  {
54                         return FALSE;
55                 }
56                 
57                 $this->root = $root;
58                 $this->name = basename($fullpath);
59                 $this->path = str_replace(array($this->root.'/', '/'.$this->name), '', $fullpath);
60                 
61                 if ( $this->path === $this->name )
62                 {
63                         $this->path = ''; 
64                 }
65                 
66                 if ( FALSE === ($info = @getimagesize($fullpath)) )
67                 {
68                         $this->mime = 'application/octet-stream';
69                         $this->width = 0;
70                         $this->height = 0;
71                 }
72                 else
73                 {
74                         $this->mime = $info['mime'];
75                         $this->width = $info[0];
76                         $this->height = $info[1];
77                 }
78                 
79                 set_time_limit(ini_get('max_execution_time'));
80                 if ( defined('FILEINFO_MIME_TYPE')
81                  && function_exists('finfo_open')
82                  && (FALSE !== ($info = finfo_open(FILEINFO_MIME_TYPE))) )
83                  {
84                         $this->mime = finfo_file($info, $fullpath);
85                 }
86                 
87                 $this->update = date("Y/m/d", @filemtime($fullpath));
88                 $this->size = ceil(filesize($fullpath) / 1000);
89                 
90                 if ( preg_match('#^(.*)\.([a-zA-Z0-9]{2,})$#', $this->name, $info) === 1 )
91                 {
92                         $this->filename = $info[1];
93                         $this->suffix = $info[2];
94                         if ( $prefix && preg_match('#^([0-9]{8})\-(.*)$#', $this->filename, $info) == 1 )
95                         {
96                                 $this->prefix = preg_replace('#^([0-9]{4})([0-9]{2})([0-9]{2})$#', '$1/$2/$3', $info[1]);
97                                 $this->filename = $info[2];
98                         }
99                 }
100                 
101                 return $this;
102         }
103         
104         public function __destruct()
105         {
106                 return;
107         }
108         
109         /**
110          * Set resampled size
111          * @access      Public  $this->setResampledSize
112          * @param       Integer $maxwidth
113          * @param       Integer $maxheight
114          * @return      Boolean
115          */
116         public function setResampledSize($maxwidth=0, $maxheight=0)
117         {
118                 if ( ($maxwidth == 0) && ($maxheight == 0) )
119                 {
120                         return FALSE;
121                 }
122                 else if ( $this->width == 0 || $this->height  == 0 )
123                 {
124                         return FALSE;
125                 }
126                 else if ( $this->width < $maxwidth && $this->height < $maxheight )
127                 {
128                         $this->resampledwidth = $this->width;
129                         $this->resampledheight = $this->height;
130                 }
131                 else if ( $maxheight == 0 || $this->width > $this->height )
132                 {
133                         $this->resampledheight = intval($this->height * $maxwidth / $this->width);
134                         $this->resampledwidth = $maxwidth;
135                 }
136                 else if ( $maxwidth == 0 || $this->width <= $this->height )
137                 {
138                         $this->resampledwidth = intval($this->width * $maxheight / $this->height);
139                         $this->resampledheight = $maxheight;
140                 }
141                 return TRUE;
142         }
143         
144 /**
145  * Return resampled image binary
146  * @access      Public  $this->getResampledSize
147  * @param       Integer $maxwidth
148  * @param       Integer $maxheight
149  * @return      Boolean
150  */
151         public function getResampledBinary($image_mime)
152         {
153                 static $gdinfo;
154                 static $original;
155                 static $resampledimage;
156                 
157                 $gdinfo = gd_info();
158                 
159                 if ( $this->path !== '' )
160                 {
161                         $fullpath = "{$this->root}/{$this->path}/{$this->name}";
162                 }
163                 else
164                 {
165                         $fullpath = "{$this->root}/{$this->name}";
166                 }
167                 if ( !file_exists($fullpath) )
168                 {
169                         return FALSE;
170                 }
171                 
172                 if ( !array_key_exists($this->mime, $image_mime)
173                  || $this->width == 0
174                  || $this->height == 0
175                  || $this->resampledwidth == 0
176                  || $this->resampledheight == 0 )
177                 {
178                         return FALSE;
179                 }
180                 
181                 // check current available memory
182                 $memorymax = trim(ini_get("memory_limit"));
183                 switch ( strtolower($memorymax[strlen($memorymax)-1]) )
184                 {
185                         case 'g':
186                                 $memorymax *= 1024;
187                         case 'm':
188                                 $memorymax *= 1024;
189                         case 'k':
190                                 $memorymax *= 1024;
191                 }
192                 
193                 // this code is based on analyze if gd.c in php source code
194                 // if you can read C/C++, please check these elements and notify us if you have some ideas
195                 if ( (memory_get_usage() + ($this->resampledwidth * $this->resampledheight * 5 + $this->resampledheight * 24 + 10000) + ($this->width * $this->height * 5 + $this->height * 24 + 10000)) > $memorymax )
196                 {
197                         return FALSE;
198                 }
199                 
200                 switch ( $this->mime )
201                 {
202                         case 'image/gif':
203                                 if ( !$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support'] )
204                                 {
205                                         return FALSE;
206                                 }
207                                 $function = 'imagecreatefromgif';
208                                 break;
209                         case 'image/jpeg':
210                                 if ( (array_key_exists('JPEG Support', $gdinfo) && !$gdinfo['JPEG Support'])
211                                  && (array_key_exists('JPG Support', $gdinfo) && $gdinfo['JPG Support']) )
212                                  {
213                                         return FALSE;
214                                 }
215                                 $function = 'imagecreatefromjpeg';
216                                 break;
217                         case 'image/png':
218                                 if ( !$gdinfo['PNG Support'] )
219                                 {
220                                         return FALSE;
221                                 }
222                                 $function = 'imagecreatefrompng';
223                                 break;
224                         default:
225                                 return FALSE;
226                 }
227                 
228                 if ( !is_callable($function) )
229                 {
230                         return FALSE;
231                 }
232                 
233                 if ( FALSE === ($original = call_user_func_array($function,array(&$fullpath))) )
234                 {
235                         return FALSE;
236                 }
237                 
238                 if ( FALSE === ($resampledimage = imagecreatetruecolor($this->resampledwidth, $this->resampledheight)) )
239                 {
240                         return FALSE;
241                 }
242                 
243                 @set_time_limit(ini_get('max_execution_time'));
244                 if ( !ImageCopyResampled($resampledimage, $original, 0, 0, 0, 0, $this->resampledwidth, $this->resampledheight, $this->width, $this->height) )
245                 {
246                         return FALSE;
247                 }
248                 
249                 imagedestroy($original);
250                 
251                 ob_start();
252                 
253                 switch ( $this->mime )
254                 {
255                         case 'image/gif':
256                                 imagegif($resampledimage);
257                                 break;
258                         case 'image/jpeg':
259                                 imagejpeg($resampledimage);
260                                 break;
261                         case 'image/png':
262                                 imagepng($resampledimage);
263                                 break;
264                         default:
265                                 return FALSE;
266                 }
267                 
268                 imagedestroy($resampledimage);
269                 
270                 return ob_get_clean();
271         }
272         
273         public function getHashedName($algorism)
274         {
275                 return (string) hash($algorism, "{$this->path}/{$this->name}", FALSE);
276         }
277 }