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