3 * MediaUtils plugin for Nucleus CMS
4 * Version 0.9.6 (1.0 RC2) for PHP5
5 * Written By Mocchi, Apr. 04, 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.
20 public $filename = '';
25 public $resampledwidth = 0;
26 public $resampledheight = 0;
29 * Return MEDIUM Object
30 * @access Public $this->__construnct
32 * @param String $relativepath
33 * @param Boolean $prefix
34 * @return Object/FALSE
36 public function __construct ($root, $relativepath, $prefix) {
40 if ($root == '' || $relativepath == '') {
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)) {
54 $this->name = basename($fullpath);
55 $this->path = str_replace(array($this->root.'/', '/'.$this->name), '', $fullpath);
57 if ($this->path === $this->name) {
61 if (FALSE === ($info = @getimagesize ($fullpath))) {
62 $this->mime = 'application/octet-stream';
66 $this->mime = $info['mime'];
67 $this->width = $info[0];
68 $this->height = $info[1];
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);
78 $this->update = date("Y/m/d", @filemtime($fullpath));
79 $this->size = ceil(filesize($fullpath) / 1000);
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];
93 public function __destruct () {
99 * @access Public $this->setResampledSize
100 * @param Integer $maxwidth
101 * @param Integer $maxheight
104 public function setResampledSize($maxwidth=0, $maxheight=0) {
105 if (($maxwidth == 0) && ($maxheight == 0)) {
107 } else if ($this->width == 0 || $this->height == 0) {
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;
123 * Return resampled image binary
124 * @access Public $this->getResampledSize
125 * @param Integer $maxwidth
126 * @param Integer $maxheight
129 public function getResampledBinary ($image_mime) {
132 static $resampledimage;
136 if ($this->path !== '') {
137 $fullpath = "{$this->root}/{$this->path}/{$this->name}";
139 $fullpath = "{$this->root}/{$this->name}";
141 if (!file_exists($fullpath)) {
145 if (!array_key_exists($this->mime, $image_mime)
147 || $this->height == 0
148 || $this->resampledwidth == 0
149 || $this->resampledheight == 0) {
153 // check current available memory
154 $memorymax = trim(ini_get("memory_limit"));
155 switch (strtolower ($memorymax[strlen($memorymax)-1])) {
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) {
170 switch ($this->mime) {
172 if (!$gdinfo['GIF Read Support'] && !$gdinfo['GIF Create Support']) {
175 $original = imagecreatefromgif ($fullpath);
178 if ((array_key_exists('JPEG Support', $gdinfo) && !$gdinfo['JPEG Support']) && (array_key_exists('JPG Support', $gdinfo) && $gdinfo['JPG Support'])) {
181 $original = imagecreatefromjpeg ($fullpath);
184 if (!$gdinfo['PNG Support']) {
187 $original = imagecreatefrompng ($fullpath);
193 $resampledimage = imagecreatetruecolor ($this->resampledwidth, $this->resampledheight);
195 if (!$resampledimage) {
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)) {
204 imagedestroy ($original);
208 switch ($this->mime) {
210 imagegif ($resampledimage);
213 imagejpeg ($resampledimage);
216 imagepng ($resampledimage);
221 imagedestroy ($resampledimage);
223 return ob_get_clean();
226 public function getHashedName($algorism) {
227 return (string) hash($algorism, "{$this->path}/{$this->name}", FALSE);