Changeset 612624
- Timestamp:
- 10/15/2012 09:26:38 AM (13 years ago)
- Location:
- imagemagick-engine/trunk
- Files:
-
- 3 edited
-
css/ime-admin.css (modified) (1 diff)
-
imagemagick-engine.php (modified) (19 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
imagemagick-engine/trunk/css/ime-admin.css
r521720 r612624 1 .form-table td.ime-handle-table-wrapper { 2 padding: 0; 3 } 4 5 #ime-handle-table th { 6 padding: 5px 10px 0 10px; 7 } 8 9 #ime-handle-table td { 10 padding: 5px 0 0 10px; 11 } 12 13 #ime-handle-table .ime-headline { 14 font-weight: bold; 15 } 16 17 #ime-handle-table .ime-fixed-width { 18 width: 60px; 19 } 20 1 21 #regenbar { 2 22 position: relative; -
imagemagick-engine/trunk/imagemagick-engine.php
r521720 r612624 6 6 Author: Orangelab 7 7 Author URI: http://www.orangelab.se 8 Version: 1. 4.08 Version: 1.5.0 9 9 Text Domain: imagemagick-engine 10 10 … … 28 28 */ 29 29 30 /*31 * Current todo list:32 * - test and handle negative returns in regen33 * - can we use --strip (or similar) without loosing color profile? (http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=14168)34 * - test command line version string35 * - test php module with required image formats36 * - handle errors in resize, fall back to GD37 *38 * Future todo list:39 * - do not iterate through all images if only resizing non-ime images40 * - edit post insert image: add custom sizes?41 * - admin: smarter find path to executable (maybe try 'which' or package handler?)42 * - allow customization of command line / class functions (safely!), check memory limit43 * - unsharp mask, sharpening level options (perhaps on a picture-by-picture basis)44 * - handle TIF and other IM formats if possible45 * - can we use IM instead of GD in more places?46 * - custom crop of images instead of blindly going for the middle47 */48 49 30 if (!defined('ABSPATH')) 50 31 die('Must not be called directly'); … … 65 46 , 'mode' => null 66 47 , 'cli_path' => null 67 , 'handle_sizes' => array('thumbnail' => true, 'medium' => true, 'large' => true) 48 , 'handle_sizes' => array('thumbnail' => 'size', 'medium' => 'quality', 'large' => 'quality') 49 , 'quality' => array('quality' => -1, 'size' => 70) 68 50 , 'quality' => '' 69 51 , 'version' => IME_OPTION_VERSION … … 71 53 72 54 // Available modes 73 $ime_available_modes = array('php' => "Imagick PHP module" 74 , 'cli' => "ImageMagick command-line"); 55 $ime_available_modes = array( 'php' => __( 'Imagick PHP module', 'imagemagick-engine' ) 56 , 'cli' => __( 'ImageMagick command-line', 'imagemagick-engine' ) ); 57 58 // Available quality modes 59 $ime_available_quality_modes = array( 'quality', 'size', 'skip' ); 75 60 76 61 // Current options … … 245 230 } 246 231 232 // Get image quality setting for type 233 function ime_get_quality($resize_mode = 'quality') { 234 $quality = ime_get_option('quality', '-1'); 235 if (!$quality) 236 return -1; 237 if (!is_array($quality)) 238 return $quality; 239 if (isset($quality[$resize_mode])) 240 return $quality[$resize_mode]; 241 242 return -1; 243 } 244 245 // Get resize mode for size 246 function ime_get_resize_mode( $size ) { 247 $handle_sizes = ime_get_option('handle_sizes'); 248 if ( isset( $handle_sizes[ $size ] ) && is_string( $handle_sizes[ $size ] ) ) 249 return $handle_sizes[ $size ]; 250 else 251 return 'quality'; // default to quality 252 } 253 247 254 248 255 /* … … 263 270 264 271 $handle_sizes = ime_get_option('handle_sizes'); 265 foreach ($handle_sizes AS$s => $handle) {266 if (!$handle || !array_key_exists($s, $sizes))272 foreach ($handle_sizes as $s => $handle) { 273 if (!$handle || $handle == 'skip' || !array_key_exists($s, $sizes)) 267 274 continue; 268 275 $ime_image_sizes[$s] = $sizes[$s]; … … 320 327 else 321 328 $new_ext = "jpg"; 322 329 323 330 /* 324 331 * Do the actual resize … … 342 349 $new_filename = "{$dir}/{$namebase}-{$suffix}.{$new_ext}"; 343 350 344 $resized = ime_im_resize( $ime_image_file, $new_filename, $dst_w, $dst_h, $crop);351 $resized = ime_im_resize( $ime_image_file, $new_filename, $dst_w, $dst_h, $crop, ime_get_resize_mode( $size ) ); 345 352 if (!$resized) 346 353 continue; … … 367 374 368 375 // Resize file by calling mode specific resize function 369 function ime_im_resize( $old_file, $new_file, $width, $height, $crop) {376 function ime_im_resize( $old_file, $new_file, $width, $height, $crop, $resize_mode = 'quality' ) { 370 377 $mode = ime_get_option("mode"); 371 378 $fn = 'ime_im_' . $mode . '_valid'; … … 374 381 375 382 $fn = 'ime_im_' . $mode . '_resize'; 376 return (function_exists($fn) && call_user_func($fn, $old_file, $new_file, $width, $height, $crop)); 383 $success = ( function_exists( $fn ) && call_user_func( $fn, $old_file, $new_file, $width, $height, $crop, $resize_mode ) ); 384 do_action( 'ime_after_resize', $success, $old_file, $new_file, $width, $height, $crop, $resize_mode ); 385 return $success; 377 386 } 378 387 … … 394 403 395 404 // Resize file using PHP Imagick class 396 function ime_im_php_resize( $old_file, $new_file, $width, $height, $crop) {397 $im = new Imagick( $old_file);398 if ( !$im->valid())405 function ime_im_php_resize( $old_file, $new_file, $width, $height, $crop, $resize_mode = 'quality' ) { 406 $im = new Imagick( $old_file ); 407 if ( ! $im->valid() ) 399 408 return false; 400 409 401 $quality = ime_get_option('quality', '-1'); 402 if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file)) { 403 $im->setImageCompression(Imagick::COMPRESSION_JPEG); 404 $im->setImageCompressionQuality($quality); 405 } 406 if (method_exists($im, 'setImageOpacity')) 407 $im->setImageOpacity(1.0); 408 409 if ($crop) { 410 /* 411 * Unfortunately we cannot use the PHP module 412 * cropThumbnailImage() function as it strips profile data. 413 * 414 * Crop an area proportional to target $width and $height and 415 * fall through to scaleImage() below. 416 */ 410 try { 411 $quality = ime_get_quality( $resize_mode ); 412 if ( is_numeric( $quality ) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg( $new_file ) ) { 413 $im->setImageCompression( Imagick::COMPRESSION_JPEG ); 414 $im->setImageCompressionQuality( $quality ); 415 } 416 if ( method_exists( $im, 'setImageOpacity' ) ) 417 $im->setImageOpacity( 1.0 ); 418 419 if ( $resize_mode == 'size' ) 420 $im->stripImage(); 421 422 if ( $crop ) { 423 /* 424 * Unfortunately we cannot use the PHP module 425 * cropThumbnailImage() function as it strips profile data. 426 * 427 * Crop an area proportional to target $width and $height and 428 * fall through to scaleImage() below. 429 */ 430 431 $geo = $im->getImageGeometry(); 432 $orig_width = $geo[ 'width' ]; 433 $orig_height = $geo[ 'height' ]; 434 435 if( ( $orig_width / $width ) < ( $orig_height / $height ) ) { 436 $crop_width = $orig_width; 437 $crop_height = ceil( ( $height * $orig_width ) / $width ); 438 $off_x = 0; 439 $off_y = ceil( ( $orig_height - $crop_height ) / 2 ); 440 } else { 441 $crop_width = ceil( ( $width * $orig_height ) / $height ); 442 $crop_height = $orig_height; 443 $off_x = ceil( ( $orig_width - $crop_width ) / 2 ); 444 $off_y = 0; 445 } 446 $im->cropImage( $crop_width, $crop_height, $off_x, $off_y ); 447 } 417 448 418 $geo = $im->getImageGeometry(); 419 $orig_width = $geo['width']; 420 $orig_height = $geo['height']; 421 422 if(($orig_width / $width) < ($orig_height / $height)) { 423 $crop_width = $orig_width; 424 $crop_height = ceil(($height * $orig_width) / $width); 425 $off_x = 0; 426 $off_y = ceil(($orig_height - $crop_height) / 2); 427 } else { 428 $crop_width = ceil(($width * $orig_height) / $height); 429 $crop_height = $orig_height; 430 $off_x = ceil(($orig_width - $crop_width) / 2); 431 $off_y = 0; 432 } 433 $im->cropImage($crop_width, $crop_height, $off_x, $off_y); 434 } 435 436 $im->scaleImage($width, $height, true); 437 438 $im->setImagePage($width, $height, 0, 0); // to make sure canvas is correct 439 $im->writeImage($new_file); 440 441 return file_exists($new_file); 449 $im->scaleImage( $width, $height, true ); 450 451 $im->setImagePage( $width, $height, 0, 0 ); // to make sure canvas is correct 452 $im->writeImage( $new_file ); 453 454 return file_exists( $new_file ); 455 } catch ( ImagickException $ie ) { 456 return false; 457 } 442 458 } 443 459 … … 521 537 522 538 // Resize using ImageMagick executable 523 function ime_im_cli_resize( $old_file, $new_file, $width, $height, $crop) {539 function ime_im_cli_resize( $old_file, $new_file, $width, $height, $crop, $resize_mode = 'quality' ) { 524 540 $cmd = ime_im_cli_command(); 525 541 if (empty($cmd)) … … 539 555 $cmd .= "!"; // force these dimensions 540 556 541 $quality = ime_get_ option('quality', '-1');557 $quality = ime_get_quality( $resize_mode ); 542 558 if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file)) 543 559 $cmd .= " -quality " . intval($quality); 560 561 if ( $resize_mode == 'size' ) 562 $cmd .= ' -strip'; 544 563 545 564 $cmd .= ' "' . $new_file . '"'; … … 700 719 $ime_page = add_options_page('ImageMagick Engine', 'ImageMagick Engine', 'manage_options', 'imagemagick-engine', 'ime_option_page'); 701 720 702 $script_pages = array( $ime_page, 'media.php', 'media-new.php', 'media-upload.php');721 $script_pages = array( $ime_page, 'media.php', 'media-new.php', 'media-upload.php', 'media-upload-popup' ); 703 722 foreach ($script_pages as $page) { 704 723 add_action('admin_print_scripts-' . $page, 'ime_admin_print_scripts'); … … 805 824 /* Plugin admin / status page */ 806 825 function ime_option_page() { 807 global $ime_available_modes ;826 global $ime_available_modes, $ime_available_quality_modes; 808 827 809 828 if (!current_user_can('manage_options')) … … 829 848 if (isset($_POST['cli_path'])) 830 849 ime_set_option('cli_path', ime_try_realpath(trim($_POST['cli_path']))); 831 if (isset($_POST['quality'])) { 832 if (is_numeric($_POST['quality'])) 833 ime_set_option('quality', min(100, max(0, intval($_POST['quality'])))); 834 else if (empty($_POST['quality'])) 835 ime_set_option('quality', ''); 850 851 $new_quality = array( 'quality' => -1, 'size' => 70 ); 852 if ( isset( $_POST[ 'quality-quality' ] ) ) { 853 if ( is_numeric( $_POST[ 'quality-quality' ] ) ) 854 $new_quality[ 'quality' ] = min( 100, max( 0, intval( $_POST[ 'quality-quality' ] ) ) ); 855 else if ( empty( $_POST[ 'quality-quality' ] ) ) 856 $new_quality[ 'quality' ] = -1; 836 857 } 858 if ( isset( $_POST[ 'quality-size' ] ) ) { 859 if ( is_numeric( $_POST[ 'quality-size' ] ) ) 860 $new_quality[ 'quality-size' ] = min( 100, max( 0, intval( $_POST[ 'quality-size' ] ) ) ); 861 else if ( empty( $_POST[ 'quality-size' ] ) ) 862 $new_quality[ 'quality-size' ] = -1; 863 } 864 ime_set_option( 'quality', $new_quality ); 865 837 866 $new_handle_sizes = array(); 838 867 foreach ($sizes AS $s => $name) { 839 $f = 'handle-' . $s; 840 $new_handle_sizes[$s] = isset($_POST[$f]) && !! $_POST[$f]; 868 $new_mode = isset( $_POST[ 'handle-mode-' . $s ] ) ? $_POST[ 'handle-mode-' . $s ] : 'skip'; 869 if ( in_array( $new_mode, $ime_available_quality_modes ) ) 870 $mode = $new_mode; 871 else 872 $mode = 'quality'; 873 874 $new_handle_sizes[ $s ] = $mode; 841 875 } 842 ime_set_option( 'handle_sizes', $new_handle_sizes);876 ime_set_option( 'handle_sizes', $new_handle_sizes ); 843 877 844 878 ime_store_options(); … … 877 911 878 912 $quality = ime_get_option('quality'); 913 if ( ! is_array( $quality ) ) { 914 $n = array( 'quality' => -1, 'size' => 70 ); 915 if ( is_numeric( $quality ) && $quality > 0 ) 916 $n[ 'quality' ] = $quality; 917 $quality = $n; 918 } 919 879 920 $handle_sizes = ime_get_option('handle_sizes'); 880 921 … … 906 947 <?php 907 948 foreach($sizes AS $s => $name) { 908 echo '<input type="checkbox" name="regen-size-' . $s . '" value="1" ' . ( isset($handle_sizes[$s]) && $handle_sizes[$s] ? ' CHECKED ' : '') . ' /> ' . $name . '<br />';949 echo '<input type="checkbox" name="regen-size-' . $s . '" value="1" ' . ( ( isset( $handle_sizes[ $s ] ) && $handle_sizes[ $s ] != 'skip' && $handle_sizes[ $s ] != false ) ? ' checked="checked" ' : '' ) . ' /> ' . $name . '<br />'; 909 950 } 910 951 ?> … … 979 1020 <th scope="row" valign="top"><?php _e('ImageMagick quality','imagemagick-engine'); ?>:</th> 980 1021 <td> 981 <input id="quality" type="text" name="quality" size="3" value="<?php echo $quality; ?>" /> <?php _e('(0-100, leave empty for default value, computed dynamically)', 'imagemagick-engine'); ?> 1022 <input id="quality-quality" type="text" name="quality-quality" size="3" value="<?php echo ( ( isset( $quality[ 'quality' ] ) && $quality[ 'quality' ] > 0 ) ? $quality[ 'quality' ] : '' ); ?>" /> <?php _e( 'Optimize for quality','imagemagick-engine' ); ?><br /> 1023 <input id="quality-size" type="text" name="quality-size" size="3" value="<?php echo ( ( isset( $quality[ 'size' ] ) && $quality[ 'size' ] > 0 ) ? $quality[ 'size' ] : '' ); ?>" /> <?php _e( 'Optimize for size','imagemagick-engine' ); ?><br /> 1024 <p class="ime-description"> 1025 <?php _e( 'Set to 0-100. Higher value gives better image quality but larger file size. Leave empty for default value, computed dynamically.', 'imagemagick-engine' ); ?> 1026 </p> 982 1027 </td> 983 1028 </tr> 984 1029 <tr> 985 <th scope="row" valign="top"><?php _e('Handle sizes','imagemagick-engine'); ?>:</th> 986 <td> 987 <?php 1030 <td colspan="2" class="ime-handle-table-wrapper"> 1031 <table border='0' class="ime-handle-table" id="ime-handle-table"> 1032 <tr> 1033 <th scope="row" class="ime-headline" valign="top"><?php _e( 'Optimize for','imagemagick-engine' ); ?></th> 1034 <td class="ime-headline ime-fixed-width"><?php _e( 'Quality','imagemagick-engine' ); ?></td> 1035 <td class="ime-headline ime-fixed-width"><?php _e( 'Size','imagemagick-engine' ); ?></td> 1036 <td class="ime-headline"><?php _e( 'Skip (use WP instead)','imagemagick-engine' ); ?></td> 1037 </tr> 1038 <?php 988 1039 foreach($sizes AS $s => $name) { 989 echo '<input type="checkbox" name="handle-' . $s . '" value="1" ' . (isset($handle_sizes[$s]) && $handle_sizes[$s] ? ' CHECKED ' : '') . ' /> ' . $name . '<br />'; 1040 // fixup for old (pre 1.5.0) options 1041 if ( ! isset( $handle_sizes[ $s] ) || ! $handle_sizes[ $s ] ) 1042 $handle_sizes[ $s ] = 'skip'; 1043 elseif ( $handle_sizes[ $s ] === true ) 1044 $handle_sizes[ $s ] = 'quality'; 1045 ?> 1046 <tr> 1047 <th scope="row" valign="top"><?php echo $name; ?></th> 1048 <td class="ime-fixed-width"> 1049 <input type="radio" name="handle-mode-<?php echo $s; ?>" value="quality" <?php checked( 'quality', $handle_sizes[ $s ] ); ?> /> 1050 </td> 1051 <td class="ime-fixed-width"> 1052 <input type="radio" name="handle-mode-<?php echo $s; ?>" value="size" <?php checked( 'size', $handle_sizes[ $s ] ); ?> /> 1053 </td> 1054 <td> 1055 <input type="radio" name="handle-mode-<?php echo $s; ?>" value="skip" <?php checked( 'skip', $handle_sizes[ $s ] ); ?> /> 1056 </td> 1057 </tr> 1058 <?php 990 1059 } 991 ?> 1060 ?> 1061 </table> 992 1062 </td> 993 1063 </tr> -
imagemagick-engine/trunk/readme.txt
r521720 r612624 3 3 Tags: image, images, picture, imagemagick, gd 4 4 Requires at least: 2.9 5 Tested up to: 3. 3.15 Tested up to: 3.5-beta1 6 6 Stable tag: 1.4.0 7 7 … … 75 75 == Changelog == 76 76 77 = 1.5.0 = 78 * Tested with WP 3.5-beta1 79 * Allow choosing between optimize for quality & size for each image size 80 * Fix resize UI bug in media pop-up 81 * Add "ime_after_resize" action after resize 82 * Catch Imagick exceptions 83 * Modified code now uses more of WP standard coding style 84 77 85 = 1.4.0 = 78 86 * Tested with WP 3.3.1
Note: See TracChangeset
for help on using the changeset viewer.