1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2012 Strangecode, LLC |
---|
6 | * |
---|
7 | * This file is part of The Strangecode Codebase. |
---|
8 | * |
---|
9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License as published by the |
---|
11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
12 | * any later version. |
---|
13 | * |
---|
14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
17 | * details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License along with |
---|
20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * ImageThumb.inc.php |
---|
25 | * |
---|
26 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
27 | * @requires Netpbm <http://sourceforge.net/projects/netpbm/> and libjpeg or GD. |
---|
28 | * @version 2.0 |
---|
29 | */ |
---|
30 | |
---|
31 | // Image proportion options. |
---|
32 | define('IMAGETHUMB_FIT_WIDTH', 1); |
---|
33 | define('IMAGETHUMB_FIT_HEIGHT', 2); |
---|
34 | define('IMAGETHUMB_FIT_LARGER', 3); |
---|
35 | define('IMAGETHUMB_STRETCH', 4); |
---|
36 | define('IMAGETHUMB_NO_SCALE', 5); |
---|
37 | |
---|
38 | // Image resize options. |
---|
39 | define('IMAGETHUMB_METHOD_NETPBM', 6); |
---|
40 | define('IMAGETHUMB_METHOD_GD', 7); |
---|
41 | |
---|
42 | class ImageThumb |
---|
43 | { |
---|
44 | |
---|
45 | // General object parameters. |
---|
46 | protected $_params = array( |
---|
47 | // The location for images to create thumbnails from. |
---|
48 | 'source_dir' => null, |
---|
49 | |
---|
50 | // Existing files will be overwritten when there is a name conflict? |
---|
51 | 'allow_overwriting' => false, |
---|
52 | |
---|
53 | // The file permissions of the uploaded files. Remember, files will be owned by the web server user. |
---|
54 | 'dest_file_perms' => 0600, |
---|
55 | |
---|
56 | // Permissions of auto-created directories. Must be at least 0700 with owner=apache. |
---|
57 | 'dest_dir_perms' => 0700, |
---|
58 | |
---|
59 | // Require file to have one of the following file name extensions. |
---|
60 | 'valid_file_extensions' => array('jpg', 'jpeg', 'gif', 'png'), |
---|
61 | |
---|
62 | // Method to use for resizing. (IMAGETHUMB_METHOD_NETPBM or IMAGETHUMB_METHOD_GD) |
---|
63 | 'resize_method' => IMAGETHUMB_METHOD_NETPBM, |
---|
64 | |
---|
65 | // Netpbm and libjpeg binary locations. |
---|
66 | 'anytopnm_binary' => '/usr/bin/anytopnm', |
---|
67 | 'pnmscale_binary' => '/usr/bin/pnmscale', |
---|
68 | 'cjpeg_binary' => '/usr/bin/cjpeg', |
---|
69 | |
---|
70 | // Which messages do we pass to raiseMsg? Use one of the MSG_* constants or false to disable. |
---|
71 | 'display_messages' => MSG_ALL, |
---|
72 | ); |
---|
73 | |
---|
74 | // Default image size specs. |
---|
75 | protected $_default_image_specs = array( |
---|
76 | // The destination for an image thumbnail size. |
---|
77 | // Use initial / to specify absolute paths, leave off to specify a path relative to source_dir (eg: ../thumbs). |
---|
78 | 'dest_dir' => null, |
---|
79 | |
---|
80 | // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP) |
---|
81 | 'dest_file_type' => IMG_JPG, |
---|
82 | |
---|
83 | // Destination file types. ('jpg', 'png', 'gif', 'wbmp') |
---|
84 | 'dest_file_extension' => 'jpg', |
---|
85 | |
---|
86 | // Type of scaling to perform, and sizes used to calculate max dimensions. |
---|
87 | 'scaling_type' => IMAGETHUMB_FIT_LARGER, |
---|
88 | 'width' => null, |
---|
89 | 'height' => null, |
---|
90 | |
---|
91 | // Percentage quality of image compression output 0-100. |
---|
92 | 'quality' => 65, |
---|
93 | |
---|
94 | // Create progressive jpegs? |
---|
95 | 'progressive' => false, |
---|
96 | |
---|
97 | // If using GD method, apply sharpen filter. Requires PHP > 5.1. |
---|
98 | 'sharpen' => true, |
---|
99 | |
---|
100 | // Integers between 1-100, useful values are 65-85. |
---|
101 | 'sharpen_value' => 75, |
---|
102 | |
---|
103 | // If source image is smaller than thumbnail, allow upscaling? |
---|
104 | 'allow_upscaling' => false, |
---|
105 | |
---|
106 | // If thumb exists and filesize is smaller than this, do not overwrite the thumb. |
---|
107 | 'keep_filesize' => null, |
---|
108 | ); |
---|
109 | |
---|
110 | // Final specifications for image sizes, set with setSpec(). |
---|
111 | protected $_image_specs = array(); |
---|
112 | |
---|
113 | /** |
---|
114 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
115 | * |
---|
116 | * @access public |
---|
117 | * @param array $params Array of parameters (key => val pairs). |
---|
118 | */ |
---|
119 | public function setParam($params) |
---|
120 | { |
---|
121 | $app =& App::getInstance(); |
---|
122 | |
---|
123 | if (isset($params) && is_array($params)) { |
---|
124 | |
---|
125 | // Enforce valid source_dir parameter. |
---|
126 | if (isset($params['source_dir'])) { |
---|
127 | // Source must be directory. |
---|
128 | if (!is_dir($params['source_dir'])) { |
---|
129 | $app->logMsg(sprintf('Attempting to auto-create source directory: %s', $params['source_dir']), LOG_NOTICE, __FILE__, __LINE__); |
---|
130 | if (version_compare(PHP_VERSION, '5.0.0', '>=')) { |
---|
131 | // Recursive. |
---|
132 | mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms'), true); |
---|
133 | } else { |
---|
134 | mkdir($params['source_dir'], isset($params['dest_dir_perms']) ? $params['dest_dir_perms'] : $this->getParam('dest_dir_perms')); |
---|
135 | } |
---|
136 | if (!is_dir($params['source_dir'])) { |
---|
137 | $app->logMsg(sprintf('Source directory invalid: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); |
---|
138 | trigger_error(sprintf('Source directory invalid: %s', $params['source_dir']), E_USER_ERROR); |
---|
139 | } |
---|
140 | } |
---|
141 | // Source must be readable. |
---|
142 | if (!is_readable($params['source_dir'])) { |
---|
143 | $app->logMsg(sprintf('Source directory not readable: %s', $params['source_dir']), LOG_ERR, __FILE__, __LINE__); |
---|
144 | trigger_error(sprintf('Source directory not readable: %s', $params['source_dir']), E_USER_ERROR); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | // Merge new parameters with old overriding only those passed. |
---|
149 | $this->_params = array_merge($this->_params, $params); |
---|
150 | } else { |
---|
151 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Return the value of a parameter, if it exists. |
---|
157 | * |
---|
158 | * @access public |
---|
159 | * @param string $param Which parameter to return. |
---|
160 | * @return mixed Configured parameter value. |
---|
161 | */ |
---|
162 | public function getParam($param) |
---|
163 | { |
---|
164 | $app =& App::getInstance(); |
---|
165 | |
---|
166 | if (array_key_exists($param, $this->_params)) { |
---|
167 | return $this->_params[$param]; |
---|
168 | } else { |
---|
169 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
170 | return null; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * Set the specification of thumbnails. |
---|
176 | * |
---|
177 | * @access public |
---|
178 | * @param array $spec The specifications for a size of output image. |
---|
179 | * @param int $index The position of the specification in the spec array |
---|
180 | * Use to overwrite existing spec array values. |
---|
181 | */ |
---|
182 | public function setSpec($spec, $index=null) |
---|
183 | { |
---|
184 | $app =& App::getInstance(); |
---|
185 | |
---|
186 | // A little sanity checking. |
---|
187 | if (!isset($spec['dest_dir']) || '' == trim($spec['dest_dir'])) { |
---|
188 | $app->logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__); |
---|
189 | } else { |
---|
190 | $spec['dest_dir'] = trim($spec['dest_dir']); |
---|
191 | } |
---|
192 | if (isset($spec['dest_file_type'])) { |
---|
193 | switch ($spec['dest_file_type']) { |
---|
194 | case IMG_JPG : |
---|
195 | if (imagetypes() & IMG_JPG == 0) { |
---|
196 | $app->logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
197 | } |
---|
198 | $spec['dest_file_extension'] = 'jpg'; |
---|
199 | break; |
---|
200 | case IMG_PNG : |
---|
201 | if (imagetypes() & IMG_PNG == 0) { |
---|
202 | $app->logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
203 | } |
---|
204 | $spec['dest_file_extension'] = 'png'; |
---|
205 | break; |
---|
206 | case IMG_GIF : |
---|
207 | if (imagetypes() & IMG_GIF == 0) { |
---|
208 | $app->logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
209 | } |
---|
210 | $spec['dest_file_extension'] = 'gif'; |
---|
211 | break; |
---|
212 | case IMG_WBMP : |
---|
213 | if (imagetypes() & IMG_WBMP == 0) { |
---|
214 | $app->logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); |
---|
215 | } |
---|
216 | $spec['dest_file_extension'] = 'wbmp'; |
---|
217 | break; |
---|
218 | default : |
---|
219 | $app->logMsg(sprintf('Invalid dest_file_type: %s', $spec['dest_file_type']), LOG_ERR, __FILE__, __LINE__); |
---|
220 | break; |
---|
221 | } |
---|
222 | } |
---|
223 | if (IMAGETHUMB_FIT_HEIGHT != $spec['scaling_type'] && (!isset($spec['width']) || !is_numeric($spec['width']))) { |
---|
224 | $app->logMsg(sprintf('setSpec error: invalid width: %s', $spec['width']), LOG_ERR, __FILE__, __LINE__); |
---|
225 | } |
---|
226 | if (IMAGETHUMB_FIT_WIDTH != $spec['scaling_type'] && (!isset($spec['height']) || !is_numeric($spec['height']))) { |
---|
227 | $app->logMsg(sprintf('setSpec error: invalid height: %s', $spec['height']), LOG_ERR, __FILE__, __LINE__); |
---|
228 | } |
---|
229 | if (isset($spec['quality']) && IMG_JPG != $spec['dest_file_type']) { |
---|
230 | $app->logMsg('The "quality" specification is not used unless IMG_JPG is the dest_file_type.', LOG_NOTICE, __FILE__, __LINE__); |
---|
231 | } |
---|
232 | if (isset($spec['progressive']) && IMG_JPG != $spec['dest_file_type']) { |
---|
233 | $app->logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_NOTICE, __FILE__, __LINE__); |
---|
234 | } |
---|
235 | |
---|
236 | // Add to _image_specs array. |
---|
237 | if (isset($index) && isset($this->_image_specs[$index])) { |
---|
238 | // Merge with existing spec if index is provided. |
---|
239 | $final_spec = array_merge($this->_image_specs[$index], $spec); |
---|
240 | $this->_image_specs[$index] = $final_spec; |
---|
241 | } else { |
---|
242 | // Merge with spec defaults. |
---|
243 | $final_spec = array_merge($this->_default_image_specs, $spec); |
---|
244 | $this->_image_specs[] = $final_spec; |
---|
245 | } |
---|
246 | |
---|
247 | return $final_spec; |
---|
248 | } |
---|
249 | |
---|
250 | /* |
---|
251 | * Retrieve a value of a thumb specification. |
---|
252 | * |
---|
253 | * @access public |
---|
254 | * @param string $key Key to return. See _default_image_specs above for a list. |
---|
255 | * @param int $index The index in the spec array of the value to retrieve. The first if not specified. |
---|
256 | * @return mixed Value of requested index. |
---|
257 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
258 | * @version 1.0 |
---|
259 | * @since 08 May 2007 15:26:39 |
---|
260 | */ |
---|
261 | public function getSpec($key, $index=null) |
---|
262 | { |
---|
263 | $index = isset($index) ? $index : 0; |
---|
264 | return $this->_image_specs[$index][$key]; |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * Process an entire directory of images. |
---|
269 | * |
---|
270 | * @access public |
---|
271 | * @return bool true on success, false on failure. |
---|
272 | */ |
---|
273 | public function processAll($runtime_specs=null) |
---|
274 | { |
---|
275 | $app =& App::getInstance(); |
---|
276 | |
---|
277 | // Ensure we have a source. |
---|
278 | if ('' == $this->getParam('source_dir')) { |
---|
279 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
280 | return false; |
---|
281 | } |
---|
282 | |
---|
283 | // Get all files in source directory. |
---|
284 | $dir_handle = opendir($this->getParam('source_dir')); |
---|
285 | while ($dir_handle && ($file = readdir($dir_handle)) !== false) { |
---|
286 | // If the file name does not start with a dot (. or .. or .htaccess). |
---|
287 | if (!preg_match('/^\./', $file) && in_array(mb_strtolower(mb_substr($file, mb_strrpos($file, '.') + 1)), $this->getParam('valid_file_extensions'))) { |
---|
288 | $files[] = $file; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | // Process each found file. |
---|
293 | if (is_array($files) && !empty($files)) { |
---|
294 | $return_val = 0; |
---|
295 | foreach ($files as $file_name) { |
---|
296 | $return_val += (false === $this->processFile($file_name, $runtime_specs) ? 1 : 0); |
---|
297 | } |
---|
298 | $this->_raiseMsg(sprintf(_("Resized %s images."), sizeof($files)), MSG_SUCCESS, __FILE__, __LINE__); |
---|
299 | return 0 === $return_val; |
---|
300 | } else { |
---|
301 | $app->logMsg(sprintf('No source images found in directory: %s', $this->getParam('source_dir')), LOG_NOTICE, __FILE__, __LINE__); |
---|
302 | return false; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | /** |
---|
307 | * Generate thumbnails for the specified file. |
---|
308 | * |
---|
309 | * @access public |
---|
310 | * @param string $file_name Name of file with extension. |
---|
311 | * @param array $runtime_specs Array of specifications that will override all configured specifications. |
---|
312 | * @return bool true on success, false on failure. |
---|
313 | */ |
---|
314 | public function processFile($file_name, $runtime_specs=null) |
---|
315 | { |
---|
316 | $app =& App::getInstance(); |
---|
317 | |
---|
318 | // Source file determined by provided file_name. |
---|
319 | $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); |
---|
320 | |
---|
321 | // Ensure we have a source. |
---|
322 | if (sizeof($this->_image_specs) < 1) { |
---|
323 | if (is_array($runtime_specs)) { |
---|
324 | $this->setSpec($runtime_specs, 0); |
---|
325 | } else { |
---|
326 | $app->logMsg(sprintf('Image specifications not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
327 | return false; |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | // Ensure we have a source. |
---|
332 | if ('' == $this->getParam('source_dir')) { |
---|
333 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
334 | return false; |
---|
335 | } |
---|
336 | |
---|
337 | // Confirm source image exists. |
---|
338 | if (!file_exists($source_file)) { |
---|
339 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> was not found."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
340 | $app->logMsg(sprintf('Source image not found: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
341 | return false; |
---|
342 | } |
---|
343 | |
---|
344 | // Confirm source image is readable. |
---|
345 | if (!is_readable($source_file)) { |
---|
346 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> is not readable."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
347 | $app->logMsg(sprintf('Source image not readable: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
348 | return false; |
---|
349 | } |
---|
350 | |
---|
351 | // Confirm source image contains data. |
---|
352 | if (filesize($source_file) <= 0) { |
---|
353 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> is zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); |
---|
354 | $app->logMsg(sprintf('Source image is zero bytes: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
355 | return false; |
---|
356 | } |
---|
357 | |
---|
358 | // Confirm source image has a valid file extension. |
---|
359 | if (!$this->_validFileExtension($file_name)) { |
---|
360 | $this->_raiseMsg(sprintf(_("Image resizing failed: source image <em>%s</em> not a valid type. It must have one of the following file name extensions: %s"), $file_name, join(', ', $this->getParam('valid_file_extensions'))), MSG_ERR, __FILE__, __LINE__); |
---|
361 | $app->logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
362 | return false; |
---|
363 | } |
---|
364 | |
---|
365 | // Ensure destination directories are created. This will only be called once per page load. |
---|
366 | if (!$this->_createDestDirs()) { |
---|
367 | $app->logMsg('Image resizing failed: unable to create dest dirs.', LOG_WARNING, __FILE__, __LINE__); |
---|
368 | return false; |
---|
369 | } |
---|
370 | |
---|
371 | // To keep this script running even if user tries to stop browser. |
---|
372 | ignore_user_abort(true); |
---|
373 | ini_set('max_execution_time', 300); |
---|
374 | ini_set('max_input_time', 300); |
---|
375 | |
---|
376 | // This remains zero until something goes wrong. |
---|
377 | $return_val = 0; |
---|
378 | |
---|
379 | foreach ($this->_image_specs as $index => $spec) { |
---|
380 | |
---|
381 | if (is_array($runtime_specs)) { |
---|
382 | // Override with runtime specs. |
---|
383 | $spec = $this->setSpec($runtime_specs, $index); |
---|
384 | } |
---|
385 | |
---|
386 | // Destination filename uses the extension defined by dest_file_extension. |
---|
387 | if ('/' == $spec['dest_dir'][0]) { |
---|
388 | // Absolute path. |
---|
389 | $dest_file = sprintf('%s/%s.%s', $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension']); |
---|
390 | } else { |
---|
391 | // Relative path. |
---|
392 | $dest_file = sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension']); |
---|
393 | } |
---|
394 | |
---|
395 | // Ensure destination directory exists and is writable. |
---|
396 | if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { |
---|
397 | $this->_createDestDirs($dest_file); |
---|
398 | if (!is_dir(dirname($dest_file)) || !is_writable(dirname($dest_file))) { |
---|
399 | $app->logMsg(sprintf('Image resizing failed, dest_dir invalid: %s', dirname($dest_file)), LOG_ERR, __FILE__, __LINE__); |
---|
400 | $return_val++; |
---|
401 | continue; |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | // Skip existing thumbnails with file size below $spec['keep_filesize']. |
---|
406 | if (isset($spec['keep_filesize']) && file_exists($dest_file)) { |
---|
407 | $file_size = filesize($dest_file); |
---|
408 | if (false !== $file_size && $file_size < $spec['keep_filesize']) { |
---|
409 | $app->logMsg(sprintf('Skipping thumbnail %s. File already exists and file size is less than %s bytes.', $spec['dest_dir'] . '/' . $file_name, $spec['keep_filesize']), LOG_INFO, __FILE__, __LINE__); |
---|
410 | continue; |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | // Determine if original file size is smaller than specified thumbnail size. Do not scale-up if $spec['allow_upscaling'] config is set to false. |
---|
415 | $image_size = getimagesize($source_file); |
---|
416 | if ($image_size['0'] <= $spec['width'] && $image_size['1'] <= $spec['height'] && !$spec['allow_upscaling']) { |
---|
417 | $spec['scaling_type'] = IMAGETHUMB_NO_SCALE; |
---|
418 | $app->logMsg(sprintf('Image %s smaller than specified %s thumbnail size. Keeping original size.', $file_name, basename($spec['dest_dir'])), LOG_INFO, __FILE__, __LINE__); |
---|
419 | } |
---|
420 | |
---|
421 | // DO IT! Based on available method. |
---|
422 | if (IMAGETHUMB_METHOD_NETPBM === $this->getParam('resize_method') && file_exists($this->getParam('anytopnm_binary')) && file_exists($this->getParam('pnmscale_binary')) && file_exists($this->getParam('cjpeg_binary'))) { |
---|
423 | // Resize using Netpbm binaries. |
---|
424 | $app->logMsg(sprintf('Resizing with Netpbm: %s', $source_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
425 | $return_val += $this->_resizeWithNetpbm($source_file, $dest_file, $spec); |
---|
426 | } else if (IMAGETHUMB_METHOD_GD === $this->getParam('resize_method') && extension_loaded('gd')) { |
---|
427 | // Resize with GD. |
---|
428 | $app->logMsg(sprintf('Resizing with GD: %s', $source_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
429 | $return_val += $this->_resizeWithGD($source_file, $dest_file, $spec); |
---|
430 | } else { |
---|
431 | $app->logMsg(sprintf('Image thumbnailing canceled. Neither Netpbm or GD is available.', null), LOG_ERR, __FILE__, __LINE__); |
---|
432 | return false; |
---|
433 | } |
---|
434 | } |
---|
435 | |
---|
436 | // If > 0, there was a problem thumb-nailing. |
---|
437 | return 0 === $return_val; |
---|
438 | } |
---|
439 | |
---|
440 | /* |
---|
441 | * Use the Netpbm and libjpg cjpeg tools to generate a rescaled compressed image. |
---|
442 | * This is the preferred method over GD which has (supposedly) less quality. |
---|
443 | * |
---|
444 | * @access private |
---|
445 | * @param string $source_file Full path to source image file. |
---|
446 | * @param string $dest_file Full path to destination image file. |
---|
447 | * @param array $spec Array of image size specifications. |
---|
448 | * @return int 0 if no error, n > 0 if errors. |
---|
449 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
450 | * @version 1.0 |
---|
451 | * @since 19 May 2006 13:55:46 |
---|
452 | */ |
---|
453 | protected function _resizeWithNetpbm($source_file, $dest_file, $spec) |
---|
454 | { |
---|
455 | $app =& App::getInstance(); |
---|
456 | |
---|
457 | // Define pnmscale arguments. |
---|
458 | switch ($spec['scaling_type']) { |
---|
459 | case IMAGETHUMB_FIT_WIDTH : |
---|
460 | $pnmscale_args = sprintf(' -width %s ', escapeshellarg($spec['width'])); |
---|
461 | break; |
---|
462 | case IMAGETHUMB_FIT_HEIGHT : |
---|
463 | $pnmscale_args = sprintf(' -height %s ', escapeshellarg($spec['height'])); |
---|
464 | break; |
---|
465 | case IMAGETHUMB_FIT_LARGER : |
---|
466 | $pnmscale_args = sprintf(' -xysize %s %s ', escapeshellarg($spec['width']), escapeshellarg($spec['height'])); |
---|
467 | break; |
---|
468 | case IMAGETHUMB_STRETCH : |
---|
469 | $pnmscale_args = sprintf(' -width %s -height %s ', escapeshellarg($spec['width']), escapeshellarg($spec['height'])); |
---|
470 | break; |
---|
471 | case IMAGETHUMB_NO_SCALE : |
---|
472 | default : |
---|
473 | $pnmscale_args = ' 1 '; |
---|
474 | break; |
---|
475 | } |
---|
476 | |
---|
477 | // Define cjpeg arguments. |
---|
478 | $cjpeg_args = sprintf(' -optimize -quality %s ', escapeshellarg($spec['quality'])); |
---|
479 | $cjpeg_args .= (true === $spec['progressive']) ? ' -progressive ' : ''; |
---|
480 | |
---|
481 | // Format the command that creates the thumbnail. |
---|
482 | $command = sprintf('%s %s | %s %s | %s %s > %s', |
---|
483 | escapeshellcmd($this->getParam('anytopnm_binary')), |
---|
484 | escapeshellcmd($source_file), |
---|
485 | escapeshellcmd($this->getParam('pnmscale_binary')), |
---|
486 | escapeshellcmd($pnmscale_args), |
---|
487 | escapeshellcmd($this->getParam('cjpeg_binary')), |
---|
488 | escapeshellcmd($cjpeg_args), |
---|
489 | escapeshellcmd($dest_file) |
---|
490 | ); |
---|
491 | $app->logMsg(sprintf('ImageThumb Netpbm command: %s', $command), LOG_DEBUG, __FILE__, __LINE__); |
---|
492 | |
---|
493 | // Execute! |
---|
494 | exec($command, $output, $return_val); |
---|
495 | |
---|
496 | if (0 === $return_val) { |
---|
497 | // Success! |
---|
498 | // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. |
---|
499 | chmod($dest_file, $this->getParam('dest_file_perms')); |
---|
500 | $app->logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val), LOG_DEBUG, __FILE__, __LINE__); |
---|
501 | } else { |
---|
502 | // An error occurred. |
---|
503 | $app->logMsg(sprintf('Image %s failed resizing with return value: %s%s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val, empty($output) ? '' : ' (' . truncate(getDump($output, true), 128, 'end') . ')'), LOG_ERR, __FILE__, __LINE__); |
---|
504 | } |
---|
505 | |
---|
506 | // Return from the command will be > 0 if there was an error. |
---|
507 | return $return_val; |
---|
508 | } |
---|
509 | |
---|
510 | /* |
---|
511 | * Use PHP's built-in GD tools to generate a rescaled compressed image. |
---|
512 | * |
---|
513 | * @access private |
---|
514 | * @param string $source_file Full path to source image file. |
---|
515 | * @param string $dest_file Full path to destination image file. |
---|
516 | * @param array $spec Array of image size specifications. |
---|
517 | * @return int 0 if no error, n > 0 if errors. |
---|
518 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
519 | * @version 1.0 |
---|
520 | * @since 19 May 2006 15:46:02 |
---|
521 | */ |
---|
522 | protected function _resizeWithGD($source_file, $dest_file, $spec) |
---|
523 | { |
---|
524 | $app =& App::getInstance(); |
---|
525 | |
---|
526 | // Get original file dimensions and type. |
---|
527 | list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_file); |
---|
528 | |
---|
529 | // Define destination image dimensions. |
---|
530 | switch ($spec['scaling_type']) { |
---|
531 | case IMAGETHUMB_FIT_WIDTH : |
---|
532 | $dest_image_width = $spec['width']; |
---|
533 | $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); |
---|
534 | break; |
---|
535 | case IMAGETHUMB_FIT_HEIGHT : |
---|
536 | $dest_image_height = $spec['height']; |
---|
537 | $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); |
---|
538 | break; |
---|
539 | case IMAGETHUMB_FIT_LARGER : |
---|
540 | if (($source_image_width * ($spec['height'] / $source_image_height)) <= $spec['width']) { |
---|
541 | // Height is larger. |
---|
542 | $dest_image_height = $spec['height']; |
---|
543 | $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); |
---|
544 | } else { |
---|
545 | // Width is larger. |
---|
546 | $dest_image_width = $spec['width']; |
---|
547 | $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); |
---|
548 | } |
---|
549 | break; |
---|
550 | case IMAGETHUMB_STRETCH : |
---|
551 | $dest_image_width = $spec['width']; |
---|
552 | $dest_image_height = $spec['height']; |
---|
553 | break; |
---|
554 | case IMAGETHUMB_NO_SCALE : |
---|
555 | default : |
---|
556 | $dest_image_width = $source_image_width; |
---|
557 | $dest_image_height = $source_image_height; |
---|
558 | break; |
---|
559 | } |
---|
560 | |
---|
561 | // Create source image data in memory. |
---|
562 | switch ($source_image_type) { |
---|
563 | case IMAGETYPE_JPEG : // = 2 |
---|
564 | $source_image_resource = imagecreatefromjpeg($source_file); |
---|
565 | break; |
---|
566 | case IMAGETYPE_PNG : // = 3 |
---|
567 | $source_image_resource = imagecreatefrompng($source_file); |
---|
568 | break; |
---|
569 | case IMAGETYPE_GIF : // = 1 |
---|
570 | $source_image_resource = imagecreatefromgif($source_file); |
---|
571 | break; |
---|
572 | case IMAGETYPE_WBMP : // = 15 |
---|
573 | $source_image_resource = imagecreatefromwbmp($source_file); |
---|
574 | case IMAGETYPE_SWF : // = 4 |
---|
575 | case IMAGETYPE_PSD : // = 5 |
---|
576 | case IMAGETYPE_BMP : // = 6 |
---|
577 | case IMAGETYPE_TIFF_II : // = 7 |
---|
578 | case IMAGETYPE_TIFF_MM : // = 8 |
---|
579 | case IMAGETYPE_JPC : // = 9 |
---|
580 | case IMAGETYPE_JP2 : // = 10 |
---|
581 | case IMAGETYPE_JPX : // = 11 |
---|
582 | case IMAGETYPE_JB2 : // = 12 |
---|
583 | case IMAGETYPE_SWC : // = 13 |
---|
584 | case IMAGETYPE_IFF : // = 14 |
---|
585 | case IMAGETYPE_XBM : // = 16 |
---|
586 | default : |
---|
587 | $app->logMsg(sprintf('Source image type %s not supported.', $source_image_type), LOG_WARNING, __FILE__, __LINE__); |
---|
588 | return 1; |
---|
589 | } |
---|
590 | if (!$source_image_resource) { |
---|
591 | $app->logMsg(sprintf('Error creating %s image in memory from %s', $source_image_type, $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
592 | return 1; |
---|
593 | } |
---|
594 | |
---|
595 | // Create destination image data in memory. |
---|
596 | $dest_image_resource = imagecreatetruecolor($dest_image_width, $dest_image_height); |
---|
597 | |
---|
598 | // Resample! |
---|
599 | if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) { |
---|
600 | // Always cleanup images from memory. |
---|
601 | imagedestroy($source_image_resource); |
---|
602 | imagedestroy($dest_image_resource); |
---|
603 | $app->logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
604 | return 1; |
---|
605 | } |
---|
606 | |
---|
607 | // Sharpen image using a custom filter matrix. |
---|
608 | if (version_compare(PHP_VERSION, '5.1.0', '>=') && true === $spec['sharpen'] && $spec['sharpen_value'] > 0) { |
---|
609 | $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10); // TODO: WTF is this math? |
---|
610 | imageconvolution($dest_image_resource, array(array(-1, -1, -1), array(-1, $sharpen_value, -1),array(-1, -1, -1)), ($sharpen_value - 8), 0); |
---|
611 | } |
---|
612 | |
---|
613 | // Save image. |
---|
614 | $return_val = true; |
---|
615 | switch ($spec['dest_file_type']) { |
---|
616 | case IMG_JPG : |
---|
617 | imageinterlace($dest_image_resource, (true == $spec['progressive'] ? 1 : 0)); |
---|
618 | $return_val = imagejpeg($dest_image_resource, $dest_file, $spec['quality']); |
---|
619 | break; |
---|
620 | case IMG_PNG : |
---|
621 | $return_val = imagepng($dest_image_resource, $dest_file); |
---|
622 | break; |
---|
623 | case IMG_GIF : |
---|
624 | $return_val = imagegif($dest_image_resource, $dest_file); |
---|
625 | break; |
---|
626 | case IMG_WBMP : |
---|
627 | $return_val = imagewbmp($dest_image_resource, $dest_file); |
---|
628 | break; |
---|
629 | default : |
---|
630 | $app->logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__); |
---|
631 | // Always cleanup images from memory. |
---|
632 | imagedestroy($source_image_resource); |
---|
633 | imagedestroy($dest_image_resource); |
---|
634 | return 1; |
---|
635 | break; |
---|
636 | } |
---|
637 | |
---|
638 | // Always cleanup images from memory. |
---|
639 | imagedestroy($source_image_resource); |
---|
640 | imagedestroy($dest_image_resource); |
---|
641 | |
---|
642 | if ($return_val) { |
---|
643 | // Success! |
---|
644 | // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. |
---|
645 | chmod($dest_file, $this->getParam('dest_file_perms')); |
---|
646 | $app->logMsg(sprintf('Successfully resized image: %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
647 | return 0; |
---|
648 | } else { |
---|
649 | // An error occurred. |
---|
650 | $app->logMsg(sprintf('Failed resizing image: %s', $dest_file), LOG_ERR, __FILE__, __LINE__); |
---|
651 | return 1; |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | /** |
---|
656 | * Delete the thumbnails for the specified file name. |
---|
657 | * |
---|
658 | * @access public |
---|
659 | * @param string $file_name The file name to delete, with extension. |
---|
660 | * @param bool $use_glob Set true to use glob to find the filename (using $file_name as a pattern) |
---|
661 | * @return bool true on success, false on failure. |
---|
662 | */ |
---|
663 | public function deleteThumbs($file_name, $use_glob=false) |
---|
664 | { |
---|
665 | $app =& App::getInstance(); |
---|
666 | |
---|
667 | // Ensure we have a source. |
---|
668 | if ('' == $this->getParam('source_dir')) { |
---|
669 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
670 | return false; |
---|
671 | } |
---|
672 | |
---|
673 | $return_val = 0; |
---|
674 | foreach ($this->_image_specs as $spec) { |
---|
675 | // Get base directory depending on if the spec's dest_dir is an absolute path or not. |
---|
676 | $dest_dir = '/' == $spec['dest_dir'][0] ? $spec['dest_dir'] : sprintf('%s/%s', $this->getParam('source_dir'), $spec['dest_dir']); |
---|
677 | if ($use_glob) { |
---|
678 | $dest_file = realpath(sprintf('%s/%s', $dest_dir, $this->_glob($file_name, $dest_dir))); |
---|
679 | } else { |
---|
680 | $dest_file = realpath(sprintf('%s/%s.%s', $dest_dir, mb_substr($file_name, 0, mb_strrpos($file_name, '.')), $spec['dest_file_extension'])); |
---|
681 | } |
---|
682 | |
---|
683 | if (file_exists($dest_file)) { |
---|
684 | if (!unlink($dest_file)) { |
---|
685 | $return_val++; |
---|
686 | $app->logMsg(sprintf('Delete thumbs failed: %s', $dest_file), LOG_WARNING, __FILE__, __LINE__); |
---|
687 | } |
---|
688 | } |
---|
689 | } |
---|
690 | $app->logMsg(sprintf('Thumbnails deleted for file: %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); |
---|
691 | return 0 === $return_val; |
---|
692 | } |
---|
693 | |
---|
694 | /** |
---|
695 | * Delete the source image with the specified file name. |
---|
696 | * |
---|
697 | * @access public |
---|
698 | * @param string $file_name The file name to delete, with extension. |
---|
699 | * @return bool true on success, false on failure. |
---|
700 | */ |
---|
701 | public function deleteOriginal($file_name) |
---|
702 | { |
---|
703 | $app =& App::getInstance(); |
---|
704 | |
---|
705 | // Ensure we have a source. |
---|
706 | if ('' == $this->getParam('source_dir')) { |
---|
707 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
708 | return false; |
---|
709 | } |
---|
710 | |
---|
711 | // Ensure we have a source. |
---|
712 | if ('' == $file_name) { |
---|
713 | $app->logMsg(sprintf('Cannot delete, filename empty.'), LOG_WARNING, __FILE__, __LINE__); |
---|
714 | return false; |
---|
715 | } |
---|
716 | |
---|
717 | $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); |
---|
718 | if (is_file($source_file) && unlink($source_file)) { |
---|
719 | $app->logMsg(sprintf('Original file successfully deleted: %s', $file_name), LOG_INFO, __FILE__, __LINE__); |
---|
720 | return true; |
---|
721 | } else { |
---|
722 | $app->logMsg(sprintf('Delete original failed: %s', $source_file), LOG_WARNING, __FILE__, __LINE__); |
---|
723 | return false; |
---|
724 | } |
---|
725 | } |
---|
726 | |
---|
727 | /** |
---|
728 | * Returns true if file exists. |
---|
729 | * |
---|
730 | * @access public |
---|
731 | * @param string $file_name The file name to test, with extension. |
---|
732 | * @return bool true on success, false on failure. |
---|
733 | */ |
---|
734 | public function exists($file_name) |
---|
735 | { |
---|
736 | $app =& App::getInstance(); |
---|
737 | |
---|
738 | // Ensure we have a source. |
---|
739 | if ('' == $this->getParam('source_dir')) { |
---|
740 | $app->logMsg(sprintf('Source directory not set before processing.'), LOG_ERR, __FILE__, __LINE__); |
---|
741 | return false; |
---|
742 | } |
---|
743 | |
---|
744 | $source_file = realpath(sprintf('%s/%s', $this->getParam('source_dir'), $file_name)); |
---|
745 | return file_exists($source_file); |
---|
746 | } |
---|
747 | |
---|
748 | /** |
---|
749 | * Tests if extension of $file_name is in the array valid_file_extensions. |
---|
750 | * |
---|
751 | * @access public |
---|
752 | * @param string $file_name A file name. |
---|
753 | * @return bool True on success, false on failure. |
---|
754 | */ |
---|
755 | protected function _validFileExtension($file_name) |
---|
756 | { |
---|
757 | preg_match('/.*?\.(\w+)$/i', $file_name, $ext); |
---|
758 | return !empty($ext) && in_array(mb_strtolower($ext[1]), $this->getParam('valid_file_extensions')); |
---|
759 | } |
---|
760 | |
---|
761 | /** |
---|
762 | * Make directory for each specified thumbnail size, if it doesn't exist. |
---|
763 | * |
---|
764 | * @access public |
---|
765 | * @return bool true on success, false on failure. |
---|
766 | */ |
---|
767 | protected function _createDestDirs($filename=null) |
---|
768 | { |
---|
769 | $app =& App::getInstance(); |
---|
770 | |
---|
771 | // Keep track of directories we've already created. |
---|
772 | $dd_hash = md5(isset($filename) ? dirname($filename) : 'none'); |
---|
773 | static $already_checked = array(); |
---|
774 | |
---|
775 | $return_val = 0; |
---|
776 | |
---|
777 | if (!isset($already_checked[$dd_hash])) { |
---|
778 | // Ensure we have a source. |
---|
779 | if ('' == $this->getParam('source_dir')) { |
---|
780 | $app->logMsg(sprintf('Source directory not set before creating destination directories.'), LOG_ERR, __FILE__, __LINE__); |
---|
781 | return false; |
---|
782 | } |
---|
783 | |
---|
784 | // Loop through specs and ensure all dirs are created. |
---|
785 | foreach ($this->_image_specs as $spec) { |
---|
786 | if (isset($filename)) { |
---|
787 | $dest_dir = dirname($filename); |
---|
788 | } else { |
---|
789 | // Get base directory depending on if the spec's dest_dir is an absolute path or not. |
---|
790 | $dest_dir = '/' == $spec['dest_dir'][0] ? $spec['dest_dir'] : sprintf('%s/%s', $this->getParam('source_dir'), $spec['dest_dir']); |
---|
791 | } |
---|
792 | if (!file_exists($dest_dir)) { |
---|
793 | // Recursively create destination directory. |
---|
794 | $app->logMsg(sprintf('Creating dest dir: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); |
---|
795 | if (!file_exists($dest_dir) && !($ret = mkdir($dest_dir, $this->getParam('dest_dir_perms'), true))) { |
---|
796 | $return_val++; |
---|
797 | $app->logMsg(sprintf('mkdir failure: %s', $dest_dir), LOG_ERR, __FILE__, __LINE__); |
---|
798 | } |
---|
799 | if ($ret) { |
---|
800 | $app->logMsg(sprintf('mkdir success: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); |
---|
801 | } |
---|
802 | } else { |
---|
803 | $app->logMsg(sprintf('Dest dir exists: %s', $dest_dir), LOG_DEBUG, __FILE__, __LINE__); |
---|
804 | } |
---|
805 | } |
---|
806 | } |
---|
807 | |
---|
808 | $already_checked[$dd_hash] = true; |
---|
809 | |
---|
810 | // If > 0, there was a problem creating dest dirs. |
---|
811 | return 0 === $return_val; |
---|
812 | } |
---|
813 | |
---|
814 | /** |
---|
815 | * An alias for $app->raiseMsg that only sends messages configured by display_messages. |
---|
816 | * |
---|
817 | * @access public |
---|
818 | * |
---|
819 | * @param string $message The text description of the message. |
---|
820 | * @param int $type The type of message: MSG_NOTICE, |
---|
821 | * MSG_SUCCESS, MSG_WARNING, or MSG_ERR. |
---|
822 | * @param string $file __FILE__. |
---|
823 | * @param string $line __LINE__. |
---|
824 | */ |
---|
825 | protected function _raiseMsg($message, $type, $file, $line) |
---|
826 | { |
---|
827 | $app =& App::getInstance(); |
---|
828 | |
---|
829 | if ($this->getParam('display_messages') === true || (is_int($this->getParam('display_messages')) && $this->getParam('display_messages') & $type > 0)) { |
---|
830 | $app->raiseMsg($message, $type, $file, $line); |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | /** |
---|
835 | * Get filename by glob pattern. Searches a directory for an image that matches the |
---|
836 | * specified glob pattern and returns the filename of the first file found. |
---|
837 | * |
---|
838 | * @access public |
---|
839 | * @param string $pattern Pattern to match filename. |
---|
840 | * @param string $directory Pattern to match containing directory. |
---|
841 | * @return string filename on success, empty string on failure. |
---|
842 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
843 | * @since 15 Nov 2005 20:55:22 |
---|
844 | */ |
---|
845 | protected function _glob($pattern, $directory) |
---|
846 | { |
---|
847 | $file_list = glob(sprintf('%s/%s', $directory, $pattern)); |
---|
848 | if (isset($file_list[0])) { |
---|
849 | return basename($file_list[0]); |
---|
850 | } else { |
---|
851 | return ''; |
---|
852 | } |
---|
853 | } |
---|
854 | |
---|
855 | } // End of class. |
---|