3 * MediaUtils plugin for Nucleus CMS
4 * Version 1.0.0 for PHP5
5 * Written By Mocchi, Oct. 20, 2011
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.
21 public $filename = '';
26 public $resampledwidth = 0;
27 public $resampledheight = 0;
30 * construct instance of Medium
31 * @access Public $this->__construnct
33 * @param String $relativepath
34 * @param Boolean $prefix
35 * @return Object/FALSE
37 public function __construct($root, $relativepath, $prefix)
42 if ( $root == '' || $relativepath == '' )
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) )
58 $this->name = basename($fullpath);
59 $this->path = str_replace(array($this->root.'/', '/'.$this->name), '', $fullpath);
61 if ( $this->path === $this->name )
66 if ( FALSE === ($info = @getimagesize($fullpath)) )
68 $this->mime = 'application/octet-stream';
74 $this->mime = $info['mime'];
75 $this->width = $info[0];
76 $this->height = $info[1];
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))) )
84 $this->mime = finfo_file($info, $fullpath);
87 $this->update = date("Y/m/d", @filemtime($fullpath));
88 $this->size = ceil(filesize($fullpath) / 1000);
90 if ( preg_match('#^(.*)\.([a-zA-Z0-9]{2,})$#', $this->name, $info) === 1 )
92 $this->filename = $info[1];
93 $this->suffix = $info[2];
94 if ( $prefix && preg_match('#^([0-9]{8})\-(.*)$#', $this->filename, $info) == 1 )
96 $this->prefix = preg_replace('#^([0-9]{4})([0-9]{2})([0-9]{2})$#', '$1/$2/$3', $info[1]);
97 $this->filename = $info[2];
104 public function __destruct()
111 * @access Public $this->setResampledSize
112 * @param Integer $maxwidth
113 * @param Integer $maxheight
116 public function setResampledSize($maxwidth=0, $maxheight=0)
118 if ( ($maxwidth == 0) && ($maxheight == 0) )
122 else if ( $this->width == 0 || $this->height == 0 )
126 else if ( $this->width < $maxwidth && $this->height < $maxheight )
128 $this->resampledwidth = $this->width;
129 $this->resampledheight = $this->height;
131 else if ( $maxheight == 0 || $this->width > $this->height )
133 $this->resampledheight = intval($this->height * $maxwidth / $this->width);
134 $this->resampledwidth = $maxwidth;
136 else if ( $maxwidth == 0 || $this->width <= $this->height )
138 $this->resampledwidth = intval($this->width * $maxheight / $this->height);
139 $this->resampledheight = $maxheight;
145 * Return resampled image binary
146 * @access Public $this->getResampledSize
147 * @param Integer $maxwidth
148 * @param Integer $maxheight
151 public function getResampledBinary($image_mime)
155 static $resampledimage;
159 if ( $this->path !== '' )
161 $fullpath = "{$this->root}/{$this->path}/{$this->name}";
165 $fullpath = "{$this->root}/{$this->name}";
167 if ( !file_exists($fullpath) )
172 if ( !array_key_exists($this->mime, $image_mime)
174 || $this->height == 0
175 || $this->resampledwidth == 0
176 || $this->resampledheight == 0 )
181 // check current available memory
182 $memorymax = trim(ini_get("memory_limit"));
183 switch ( strtolower($memorymax[strlen($memorymax)-1]) )
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 )
200 switch ( $this->mime )
203 if ( !$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support'] )
207 $function = 'imagecreatefromgif';
210 if ( (array_key_exists('JPEG Support', $gdinfo) && !$gdinfo['JPEG Support'])
211 && (array_key_exists('JPG Support', $gdinfo) && $gdinfo['JPG Support']) )
215 $function = 'imagecreatefromjpeg';
218 if ( !$gdinfo['PNG Support'] )
222 $function = 'imagecreatefrompng';
228 if ( !is_callable($function) )
233 if ( FALSE === ($original = call_user_func_array($function,array(&$fullpath))) )
238 if ( FALSE === ($resampledimage = imagecreatetruecolor($this->resampledwidth, $this->resampledheight)) )
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) )
249 imagedestroy($original);
253 switch ( $this->mime )
256 imagegif($resampledimage);
259 imagejpeg($resampledimage);
262 imagepng($resampledimage);
268 imagedestroy($resampledimage);
270 return ob_get_clean();
273 public function getHashedName($algorism)
275 return (string) hash($algorism, "{$this->path}/{$this->name}", FALSE);