OSDN Git Service

2ab43f0a2aee16c26a99031db2c7aa695787c252
[nucleus-jp/nucleus-plugins.git] / NP_MediaUtils / mediautils / Medium.php
1 <?php
2 /**
3  * MediaUtils plugin for Nucleus CMS
4  * Version 0.9.6 (1.0 RC2) for PHP5
5  * Written By Mocchi, Apr. 04, 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         public $root = '';
15         public $path = '';
16         public $name = '';
17         public $update = '';
18         public $prefix = '';
19         public $suffix = '';
20         public $filename = '';
21         public $size = 0;
22         public $mime = '';
23         public $width = 0;
24         public $height = 0;
25         public $resampledwidth = 0;
26         public $resampledheight = 0;
27         
28 /**
29  * Return MEDIUM Object
30  * @access      Public  $this->__construnct
31  * @param       String  $root
32  * @param       String  $relativepath
33  * @param       Boolean $prefix
34  * @return      Object/FALSE
35  */
36         public function __construct ($root, $relativepath, $prefix) {
37                 static $fullpath;
38                 static $info;
39                 
40                 if ($root == '' || $relativepath == '') {
41                         return FALSE;
42                 }
43                 
44                 $root = preg_replace('#/*$#', '', $root);
45                 if ($root == '' || $relativepath == ''
46                  || !file_exists($root)
47                  || FALSE === ($fullpath = realpath(rtrim($root . '/' . ltrim($relativepath, '/'), '/')))
48                  || strpos($fullpath, $root) !== 0
49                  || !file_exists($fullpath)) {
50                         return FALSE;
51                 }
52                 
53                 $this->root = $root;
54                 $this->name = basename($fullpath);
55                 $this->path = str_replace(array($this->root.'/', '/'.$this->name), '', $fullpath);
56                 
57                 if ($this->path === $this->name) {
58                         $this->path = ''; 
59                 }
60                 
61                 if (FALSE === ($info = @getimagesize ($fullpath))) {
62                         $this->mime = 'application/octet-stream';
63                         $this->width = 0;
64                         $this->height = 0;
65                 } else {
66                         $this->mime = $info['mime'];
67                         $this->width = $info[0];
68                         $this->height = $info[1];
69                 }
70                 
71                 set_time_limit(ini_get('max_execution_time'));
72                 if (defined('FILEINFO_MIME_TYPE')
73                  && function_exists ('finfo_open')
74                  && (FALSE !== ($info = finfo_open(FILEINFO_MIME_TYPE)))) {
75                         $this->mime = finfo_file($info, $fullpath);
76                 }
77                 
78                 $this->update = date("Y/m/d", @filemtime($fullpath));
79                 $this->size = ceil(filesize($fullpath) / 1000);
80                 
81                 if (preg_match('#^(.*)\.([a-zA-Z0-9]{2,})$#', $this->name, $info) === 1) {
82                         $this->filename = $info[1];
83                         $this->suffix = $info[2];
84                         if ($prefix && preg_match('#^([0-9]{8})\-(.*)$#', $this->filename, $info) == 1 ) {
85                                 $this->prefix = preg_replace('#^([0-9]{4})([0-9]{2})([0-9]{2})$#', '$1/$2/$3', $info[1]);
86                                 $this->filename = $info[2];
87                         }
88                 }
89                 
90                 return $this;
91         }
92         
93         public function __destruct () {
94                 return;
95         }
96         
97 /**
98  * Set resampled size
99  * @access      Public  $this->setResampledSize
100  * @param       Integer $maxwidth
101  * @param       Integer $maxheight
102  * @return      Boolean
103  */
104         public function setResampledSize($maxwidth=0, $maxheight=0) {
105                 if (($maxwidth == 0) && ($maxheight == 0)) {
106                         return FALSE;
107                 } else if ($this->width == 0 || $this->height  == 0) {
108                         return FALSE;
109                 } else if ($this->width < $maxwidth && $this->height < $maxheight) {
110                         $this->resampledwidth = $this->width;
111                         $this->resampledheight = $this->height;
112                 } else if ($maxheight == 0 || $this->width > $this->height) {
113                         $this->resampledheight = intval ($this->height * $maxwidth / $this->width);
114                         $this->resampledwidth = $maxwidth;
115                 } else if ($maxwidth == 0 || $this->width <= $this->height) {
116                         $this->resampledwidth = intval ($this->width * $maxheight / $this->height);
117                         $this->resampledheight = $maxheight;
118                 }
119                 return TRUE;
120         }
121         
122 /**
123  * Return resampled image binary
124  * @access      Public  $this->getResampledSize
125  * @param       Integer $maxwidth
126  * @param       Integer $maxheight
127  * @return      Boolean
128  */
129         public function getResampledBinary ($image_mime) {
130                 static $gdinfo;
131                 static $original;
132                 static $resampledimage;
133                 
134                 $gdinfo = gd_info();
135                 
136                 if ($this->path !== '') {
137                         $fullpath = "{$this->root}/{$this->path}/{$this->name}";
138                 } else {
139                         $fullpath = "{$this->root}/{$this->name}";
140                 }
141                 if (!file_exists($fullpath)) {
142                         return FALSE;
143                 }
144                 
145                 if (!array_key_exists($this->mime, $image_mime)
146                  || $this->width == 0
147                  || $this->height == 0
148                  || $this->resampledwidth == 0
149                  || $this->resampledheight == 0) {
150                         return FALSE;
151                 }
152                 
153                 // check current available memory
154                 $memorymax = trim(ini_get("memory_limit"));
155                 switch (strtolower ($memorymax[strlen($memorymax)-1])) {
156                  case 'g':
157                  $memorymax *= 1024;
158                  case 'm':
159                  $memorymax *= 1024;
160                  case 'k':
161                  $memorymax *= 1024;
162                 }
163                 
164                 // this code is based on analyze if gd.c in php source code
165                 // if you can read C/C++, please check these elements and notify us if you have some ideas
166                 if ((memory_get_usage() + ($this->resampledwidth * $this->resampledheight * 5 + $this->resampledheight * 24 + 10000) + ($this->width * $this->height * 5 + $this->height * 24 + 10000)) > $memorymax) {
167                         return FALSE;
168                 }
169                 
170                 switch ($this->mime) {
171                         case 'image/gif':
172                                 if (!$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support']) {
173                                         return FALSE;
174                                 }
175                                 $original = imagecreatefromgif ($fullpath);
176                                 break;
177                         case 'image/jpeg':
178                                 if ((array_key_exists('JPEG Support', $gdinfo) && !$gdinfo['JPEG Support']) && (array_key_exists('JPG Support', $gdinfo) && $gdinfo['JPG Support'])) {
179                                         return FALSE;
180                                 }
181                                 $original = imagecreatefromjpeg ($fullpath);
182                                 break;
183                         case 'image/png':
184                                 if (!$gdinfo['PNG Support']) {
185                                         return FALSE;
186                                 }
187                                 $original = imagecreatefrompng ($fullpath);
188                                 break;
189                         default:
190                                 return FALSE;
191                 }
192                 
193                 $resampledimage = imagecreatetruecolor ($this->resampledwidth, $this->resampledheight);
194                 
195                 if (!$resampledimage) {
196                         return FALSE;
197                 }
198                 
199                 set_time_limit(ini_get('max_execution_time'));
200                 if (!ImageCopyResampled ($resampledimage, $original, 0, 0, 0, 0, $this->resampledwidth, $this->resampledheight, $this->width, $this->height)) {
201                         return FALSE;
202                 }
203                 
204                 imagedestroy ($original);
205                 
206                 ob_start();
207                 
208                 switch ($this->mime) {
209                  case 'image/gif':
210                   imagegif ($resampledimage);
211                   break;
212                  case 'image/jpeg':
213                   imagejpeg ($resampledimage);
214                   break;
215                  case 'image/png':
216                   imagepng ($resampledimage);
217                   break;
218                  default:
219                 }
220                 
221                 imagedestroy ($resampledimage);
222                 
223                 return ob_get_clean();
224         }
225         
226         public function getHashedName($algorism) {
227                 return (string) hash($algorism, "{$this->path}/{$this->name}", FALSE);
228         }
229 }