Changeset 120
- Timestamp:
- May 21, 2006 6:59:28 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/ImageThumb.inc.php
r119 r120 33 33 // Permissions of autocreated directories. Must be at least 0700 with owner=apache. 34 34 'dest_dir_perms' => 0777, 35 36 // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP)37 'dest_file_type' => IMG_JPG,38 39 // Destination file types. ('jpg', 'png', 'gif', 'wbmp')40 'dest_file_extention' => 'jpg',41 35 42 36 // Require file to have one of the following file name extentions. … … 59 53 // The destination for an image thumbnail size. Path relative to source_dir (eg: ../thumbs). 60 54 'dest_dir' => null, 55 56 // Destination file types. (IMG_JPG, IMG_PNG, IMG_GIF, IMG_WBMP) 57 'dest_file_type' => IMG_JPG, 58 59 // Destination file types. ('jpg', 'png', 'gif', 'wbmp') 60 'dest_file_extention' => 'jpg', 61 61 62 // Type of scaling to perform, and sizes used to calculate max dimentions. 62 63 'scaling_type' => IMAGETHUMB_FIT_LARGER, 63 64 'width' => null, 64 65 'height' => null, 66 65 67 // Percentage quality of image compression output 0-100. 66 68 'quality' => 65, 69 67 70 // Create progressive jpegs? 68 71 'progressive' => false, 72 73 // If using GD method, apply sharpen filter. Requires PHP > 5.1. 74 'sharpen' => true, 75 76 // Integers between 1-100, useful values are 65-85. 77 'sharpen_value' => 75, 78 69 79 // If source image is smaller than thumbnail, allow upscaling? 70 80 'allow_upscaling' => false, 81 71 82 // If thumb exists and filesize is smaller than this, do not overwrite the thumb. 72 83 'keep_filesize' => null, … … 100 111 } 101 112 } 102 103 if (isset($params['dest_file_type'])) {104 switch ($params['dest_file_type']) {105 case IMG_JPG :106 if (imagetypes() & IMG_JPG == 0) {107 App::logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);108 }109 $params['dest_file_extention'] = 'jpg';110 break;111 case IMG_PNG :112 if (imagetypes() & IMG_PNG == 0) {113 App::logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);114 }115 $params['dest_file_extention'] = 'png';116 break;117 case IMG_GIF :118 if (imagetypes() & IMG_GIF == 0) {119 App::logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);120 }121 $params['dest_file_extention'] = 'gif';122 break;123 case IMG_WBMP :124 if (imagetypes() & IMG_WBMP == 0) {125 App::logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__);126 }127 $params['dest_file_extention'] = 'wbmp';128 break;129 default :130 App::logMsg(sprintf('Invalid dest_file_type: %s', $params['dest_file_type']), LOG_ERR, __FILE__, __LINE__);131 break;132 }133 }134 113 135 114 // Merge new parameters with old overriding only those passed. … … 163 142 * @param array $spec The specifications for a size of output image. 164 143 */ 165 function setSpec($spec )144 function setSpec($spec, $index=null) 166 145 { 167 146 // A little sanity checking. 168 147 if (!isset($spec['dest_dir']) || '' == $spec['dest_dir']) { 169 148 App::logMsg('setSpec error: dest_dir not specified.', LOG_ERR, __FILE__, __LINE__); 170 return false; 149 } 150 if (isset($spec['dest_file_type'])) { 151 switch ($spec['dest_file_type']) { 152 case IMG_JPG : 153 if (imagetypes() & IMG_JPG == 0) { 154 App::logMsg(sprintf('IMG_JPG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 155 } 156 $spec['dest_file_extention'] = 'jpg'; 157 break; 158 case IMG_PNG : 159 if (imagetypes() & IMG_PNG == 0) { 160 App::logMsg(sprintf('IMG_PNG is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 161 } 162 $spec['dest_file_extention'] = 'png'; 163 break; 164 case IMG_GIF : 165 if (imagetypes() & IMG_GIF == 0) { 166 App::logMsg(sprintf('IMG_GIF is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 167 } 168 $spec['dest_file_extention'] = 'gif'; 169 break; 170 case IMG_WBMP : 171 if (imagetypes() & IMG_WBMP == 0) { 172 App::logMsg(sprintf('IMG_WBMP is not supported by this version of PHP GD.', null), LOG_ERR, __FILE__, __LINE__); 173 } 174 $spec['dest_file_extention'] = 'wbmp'; 175 break; 176 default : 177 App::logMsg(sprintf('Invalid dest_file_type: %s', $spec['dest_file_type']), LOG_ERR, __FILE__, __LINE__); 178 break; 179 } 171 180 } 172 181 if (!isset($spec['width']) || !is_int($spec['width'])) { 173 182 App::logMsg('setSpec error: width not specified.', LOG_ERR, __FILE__, __LINE__); 174 return false;175 183 } 176 184 if (!isset($spec['height']) || !is_int($spec['height'])) { 177 185 App::logMsg('setSpec error: height not specified.', LOG_ERR, __FILE__, __LINE__); 178 return false; 179 } 180 if (isset($spec['quality']) && IMG_JPG != $this->getParam('dest_file_type')) { 186 } 187 if (isset($spec['quality']) && IMG_JPG != $spec['dest_file_type']) { 181 188 App::logMsg('The "quality" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); 182 189 } 183 if (isset($spec['progressive']) && IMG_JPG != $ this->getParam('dest_file_type')) {190 if (isset($spec['progressive']) && IMG_JPG != $spec['dest_file_type']) { 184 191 App::logMsg('The "progressive" specification is not used unless IMG_JPG is the dest_file_type.', LOG_INFO, __FILE__, __LINE__); 185 192 } 186 // Merge defaults with provided. 187 $this->image_specs[] = array_merge($this->default_image_specs, $spec); 193 194 if (isset($index) && isset($this->image_specs[$index])) { 195 // Merge with previous. 196 $this->image_specs[$index] = array_merge($this->image_specs[$index], $spec); 197 } else { 198 // Merge with defaults. 199 $this->image_specs[] = array_merge($this->default_image_specs, $spec); 200 } 188 201 } 189 202 … … 217 230 $return_val += $this->processFile($file_name); 218 231 } 232 $this->_raiseMsg(sprintf(_("Resized %s images."), sizeof($files)), MSG_SUCCESS, __FILE__, __LINE__); 219 233 return 0 === $return_val; 220 234 } else { … … 250 264 // Confirm source image exists. 251 265 if (!file_exists($source_file)) { 252 $this->_raiseMsg(sprintf(_("Image resizing failed: source image not found: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);266 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s was not found."), $file_name), MSG_ERR, __FILE__, __LINE__); 253 267 App::logMsg(sprintf('Source image not found: %s', $file_name), LOG_ALERT, __FILE__, __LINE__); 254 268 return false; … … 257 271 // Confirm source image is readable. 258 272 if (!is_readable($source_file)) { 259 $this->_raiseMsg(sprintf(_("Image resizing failed: source image not readable: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);273 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is not readable."), $file_name), MSG_ERR, __FILE__, __LINE__); 260 274 App::logMsg(sprintf('Source image not readable: %s', $file_name), LOG_ALERT, __FILE__, __LINE__); 261 275 return false; … … 264 278 // Confirm source image contains data. 265 279 if (filesize($source_file) <= 0) { 266 $this->_raiseMsg(sprintf(_("Image resizing failed: source image corrupt: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);280 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s is zero bytes."), $file_name), MSG_ERR, __FILE__, __LINE__); 267 281 App::logMsg(sprintf('Source image is zero bytes: %s', $file_name), LOG_ALERT, __FILE__, __LINE__); 268 282 return false; … … 271 285 // Confirm source image has a valid file extension. 272 286 if (!$this->_validFileExtension($file_name)) { 273 $this->_raiseMsg(sprintf(_("Image resizing failed: source image not of valid type: <strong>%s</strong>"), $file_name), MSG_ERR, __FILE__, __LINE__);287 $this->_raiseMsg(sprintf(_("Image resizing failed: source image %s 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__); 274 288 App::logMsg(sprintf('Image resizing failed: source image not of valid type: %s', $file_name), LOG_ERR, __FILE__, __LINE__); 275 289 return false; … … 291 305 292 306 // Destination filename uses the extention defined by dest_file_extention. 293 $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $ this->getParam('dest_file_extention')));307 $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_extention'])); 294 308 295 309 // Skip existing thumbnails with file size below $spec['keep_filesize']. … … 336 350 * @param string $dest_file Full path to destination image file. 337 351 * @param array $spec Array of image size specifications. 338 * @return bool Success value.352 * @return int 0 if no error, n > 0 if errors. 339 353 * @author Quinn Comendant <quinn@strangecode.com> 340 354 * @version 1.0 … … 387 401 // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. 388 402 chmod($dest_file, $this->getParam('dest_file_perms')); 389 App::logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . $file_name, $return_val), LOG_DEBUG, __FILE__, __LINE__);403 App::logMsg(sprintf('Successfully resized image %s', $spec['dest_dir'] . '/' . basename($dest_file), $return_val), LOG_DEBUG, __FILE__, __LINE__); 390 404 } else { 391 405 // An error occurred. 392 App::logMsg(sprintf('Image %s failed resizing with return value: %s%s', $spec['dest_dir'] . '/' . $file_name, $return_val, empty($output) ? '' : ' (' . getDump($output) . ')'), LOG_ERR, __FILE__, __LINE__);406 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__); 393 407 } 394 408 … … 404 418 * @param string $dest_file Full path to destination image file. 405 419 * @param array $spec Array of image size specifications. 406 * @return bool Success value.420 * @return int 0 if no error, n > 0 if errors. 407 421 * @author Quinn Comendant <quinn@strangecode.com> 408 422 * @version 1.0 … … 417 431 switch ($spec['scaling_type']) { 418 432 case IMAGETHUMB_FIT_WIDTH : 433 $dest_image_width = $spec['width']; 419 434 $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); 420 $dest_image_width = $spec['width'];421 435 break; 422 436 case IMAGETHUMB_FIT_HEIGHT : 437 $dest_image_height = $spec['height']; 423 438 $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); 424 $dest_image_height = $spec['height'];425 439 break; 426 440 case IMAGETHUMB_FIT_LARGER : 427 if ($source_image_width < $source_image_height) { 428 $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); 429 $dest_image_height = $source_image_height; 441 if (($source_image_width * ($spec['height'] / $source_image_height)) <= $spec['width']) { 442 // Height is larger. 443 $dest_image_height = $spec['height']; 444 $dest_image_width = $source_image_width * ($spec['height'] / $source_image_height); 430 445 } else { 431 $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); 432 $dest_image_width = $source_image_width; 446 // Width is larger. 447 $dest_image_width = $spec['width']; 448 $dest_image_height = $source_image_height * ($spec['width'] / $source_image_width); 433 449 } 434 450 break; … … 443 459 break; 444 460 } 445 446 echo '<li> sourceWidth: ' . $source_image_width;447 echo '<li> sourceHeight: ' . $source_image_height;448 echo '<li> Width: ' . $dest_image_width;449 echo '<li> Height: ' . $dest_image_height;450 die;///451 461 452 462 // Create source image data in memory. 453 463 switch ($source_image_type) { 454 464 case IMAGETYPE_JPEG : 455 $source_image_ data= imagecreatefromjpeg($source_file);465 $source_image_resource = imagecreatefromjpeg($source_file); 456 466 break; 457 467 case IMAGETYPE_PNG : 458 $source_image_ data= imagecreatefrompng($source_file);468 $source_image_resource = imagecreatefrompng($source_file); 459 469 break; 460 470 case IMAGETYPE_GIF : 461 $source_image_ data= imagecreatefromgif($source_file);471 $source_image_resource = imagecreatefromgif($source_file); 462 472 break; 463 473 case IMAGETYPE_WBMP : 464 $source_image_ data= imagecreatefromwbmp($source_file);474 $source_image_resource = imagecreatefromwbmp($source_file); 465 475 default : 466 476 App::logMsg(sprintf('Source image type %s not supported.', $source_image_type), LOG_WARNING, __FILE__, __LINE__); 467 return false;468 break; 469 } 470 if (!$source_image_ data) {477 return 1; 478 break; 479 } 480 if (!$source_image_resource) { 471 481 App::logMsg(sprintf('Error creating %s image in memory from %s', $source_image_type, $source_file), LOG_WARNING, __FILE__, __LINE__); 472 return false;482 return 1; 473 483 } 474 484 475 485 // Create destination image data in memory. 476 $dest_image_ data= imagecreatetruecolor($dest_image_width, $dest_image_height);486 $dest_image_resource = imagecreatetruecolor($dest_image_width, $dest_image_height); 477 487 478 488 // Resample! 479 if (!imagecopyresampled($dest_image_ data, $source_image_data, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) {489 if (!imagecopyresampled($dest_image_resource, $source_image_resource, 0, 0, 0, 0, $dest_image_width, $dest_image_height, $source_image_width, $source_image_height)) { 480 490 App::logMsg(sprintf('Error resampling image %s', $source_file), LOG_WARNING, __FILE__, __LINE__); 481 return false; 491 return 1; 492 } 493 494 // Sharpen image using a custom filter matrix. 495 if (phpversion() > '5.1' && true === $spec['sharpen'] && $spec['sharpen_value'] > 0) { 496 $sharpen_value = round((((48 - 10) / (100 - 1)) * (100 - $spec['sharpen_value'])) + 10); 497 imageconvolution($dest_image_resource, array(array(-1,-1,-1),array(-1,$sharpen_value,-1),array(-1,-1,-1)), ($sharpen_value - 8), 0); 482 498 } 483 499 484 500 // Save image. 485 501 $return_val = true; 486 switch ($ this->getParam('dest_file_type')) {502 switch ($spec['dest_file_type']) { 487 503 case IMG_JPG : 488 imageinterlace($dest_image_ data, (true == $spec['progressive'] ? 1 : 0));489 $return_val = imagejpeg($dest_image_ data, $dest_file, $spec['quality']);504 imageinterlace($dest_image_resource, (true == $spec['progressive'] ? 1 : 0)); 505 $return_val = imagejpeg($dest_image_resource, $dest_file, $spec['quality']); 490 506 break; 491 507 case IMG_PNG : 492 $return_val = imagepng($dest_image_ data, $dest_file);508 $return_val = imagepng($dest_image_resource, $dest_file); 493 509 break; 494 510 case IMG_GIF : 495 $return_val = imagegif($dest_image_ data, $dest_file);511 $return_val = imagegif($dest_image_resource, $dest_file); 496 512 break; 497 513 case IMG_WBMP : 498 $return_val = imagewbmp($dest_image_ data, $dest_file);514 $return_val = imagewbmp($dest_image_resource, $dest_file); 499 515 break; 500 516 default : 501 App::logMsg(sprintf('Destination image type %s not supported for image %s.', $ this->getParam('dest_file_type'), $dest_file), LOG_WARNING, __FILE__, __LINE__);502 return false;517 App::logMsg(sprintf('Destination image type %s not supported for image %s.', $spec['dest_file_type'], $dest_file), LOG_WARNING, __FILE__, __LINE__); 518 return 1; 503 519 break; 504 520 } … … 507 523 // Success! 508 524 // Make the thumbnail writable so the user can delete it over ftp without being 'apache'. 509 chmod($ source_file, $this->getParam('dest_file_perms'));525 chmod($dest_file, $this->getParam('dest_file_perms')); 510 526 App::logMsg(sprintf('Successfully resized image %s', $dest_file), LOG_DEBUG, __FILE__, __LINE__); 511 return true;527 return 0; 512 528 } else { 513 529 // An error occurred. 514 530 App::logMsg(sprintf('Image %s failed resizing.', $dest_file), LOG_ERR, __FILE__, __LINE__); 515 return false;531 return 1; 516 532 } 517 533 } … … 534 550 $return_val = 0; 535 551 foreach ($this->image_specs as $spec) { 536 $dest_file = realpath(sprintf('%s/%s/%s.%s', $this->getParam('source_dir'), $spec['dest_dir'], substr($file_name, 0, strrpos($file_name, '.')), $ this->getParam('dest_file_extention')));552 $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_extention'])); 537 553 if (file_exists($dest_file)) { 538 554 if (!unlink($dest_file)) {
Note: See TracChangeset
for help on using the changeset viewer.