Plugin Directory

Changeset 612624


Ignore:
Timestamp:
10/15/2012 09:26:38 AM (13 years ago)
Author:
orangelab
Message:

1.5.0 development changes

Location:
imagemagick-engine/trunk
Files:
3 edited

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
    121#regenbar {
    222    position: relative;
  • imagemagick-engine/trunk/imagemagick-engine.php

    r521720 r612624  
    66  Author: Orangelab
    77  Author URI: http://www.orangelab.se
    8   Version: 1.4.0
     8  Version: 1.5.0
    99  Text Domain: imagemagick-engine
    1010
     
    2828*/
    2929
    30 /*
    31  * Current todo list:
    32  * - test and handle negative returns in regen
    33  * - 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 string
    35  * - test php module with required image formats
    36  * - handle errors in resize, fall back to GD
    37  *
    38  * Future todo list:
    39  * - do not iterate through all images if only resizing non-ime images
    40  * - 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 limit
    43  * - unsharp mask, sharpening level options (perhaps on a picture-by-picture basis)
    44  * - handle TIF and other IM formats if possible
    45  * - can we use IM instead of GD in more places?
    46  * - custom crop of images instead of blindly going for the middle
    47  */
    48 
    4930if (!defined('ABSPATH'))
    5031    die('Must not be called directly');
     
    6546                 , 'mode' => null
    6647                 , '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)
    6850                 , 'quality' => ''
    6951                 , 'version' => IME_OPTION_VERSION
     
    7153
    7254// 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' );
    7560
    7661// Current options
     
    245230}
    246231
     232// Get image quality setting for type
     233function 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
     246function 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
    247254
    248255/*
     
    263270
    264271    $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))
    267274            continue;
    268275        $ime_image_sizes[$s] = $sizes[$s];
     
    320327    else
    321328        $new_ext = "jpg";
    322    
     329
    323330    /*
    324331     * Do the actual resize
     
    342349        $new_filename = "{$dir}/{$namebase}-{$suffix}.{$new_ext}";
    343350
    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 ) );
    345352        if (!$resized)
    346353            continue;
     
    367374
    368375// Resize file by calling mode specific resize function
    369 function ime_im_resize($old_file, $new_file, $width, $height, $crop) {
     376function ime_im_resize( $old_file, $new_file, $width, $height, $crop, $resize_mode = 'quality' ) {
    370377    $mode = ime_get_option("mode");
    371378    $fn = 'ime_im_' . $mode . '_valid';
     
    374381
    375382    $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;
    377386}
    378387
     
    394403
    395404// 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())
     405function ime_im_php_resize( $old_file, $new_file, $width, $height, $crop, $resize_mode = 'quality' ) {
     406    $im = new Imagick( $old_file );
     407    if ( ! $im->valid() )
    399408        return false;
    400409
    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        }
    417448       
    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    }
    442458}
    443459
     
    521537
    522538// Resize using ImageMagick executable
    523 function ime_im_cli_resize($old_file, $new_file, $width, $height, $crop) {
     539function ime_im_cli_resize( $old_file, $new_file, $width, $height, $crop, $resize_mode = 'quality' ) {
    524540    $cmd = ime_im_cli_command();
    525541    if (empty($cmd))
     
    539555        $cmd .= "!"; // force these dimensions
    540556
    541     $quality = ime_get_option('quality', '-1');
     557    $quality = ime_get_quality( $resize_mode );
    542558    if (is_numeric($quality) && $quality >= 0 && $quality <= 100 && ime_im_filename_is_jpg($new_file))
    543559        $cmd .= " -quality " . intval($quality);
     560
     561    if ( $resize_mode == 'size' )
     562        $cmd .= ' -strip';
    544563
    545564    $cmd .= ' "' .  $new_file . '"';
     
    700719    $ime_page = add_options_page('ImageMagick Engine', 'ImageMagick Engine', 'manage_options', 'imagemagick-engine', 'ime_option_page');
    701720
    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' );
    703722    foreach ($script_pages as $page) {
    704723        add_action('admin_print_scripts-' . $page, 'ime_admin_print_scripts');
     
    805824/* Plugin admin / status page */
    806825function ime_option_page() {
    807     global $ime_available_modes;
     826    global $ime_available_modes, $ime_available_quality_modes;
    808827
    809828    if (!current_user_can('manage_options'))
     
    829848        if (isset($_POST['cli_path']))
    830849            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;
    836857        }
     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
    837866        $new_handle_sizes = array();
    838867        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;
    841875        }
    842         ime_set_option('handle_sizes', $new_handle_sizes);
     876        ime_set_option( 'handle_sizes', $new_handle_sizes );
    843877
    844878        ime_store_options();
     
    877911
    878912    $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
    879920    $handle_sizes = ime_get_option('handle_sizes');
    880921
     
    906947            <?php
    907948              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 />';
    909950              }
    910951              ?>
     
    9791020          <th scope="row" valign="top"><?php _e('ImageMagick quality','imagemagick-engine'); ?>:</th>
    9801021          <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>
    9821027          </td>
    9831028        </tr>
    9841029        <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
    9881039              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
    9901059              }
    991         ?>
     1060          ?>
     1061        </table>
    9921062          </td>
    9931063        </tr>
  • imagemagick-engine/trunk/readme.txt

    r521720 r612624  
    33Tags: image, images, picture, imagemagick, gd
    44Requires at least: 2.9
    5 Tested up to: 3.3.1
     5Tested up to: 3.5-beta1
    66Stable tag: 1.4.0
    77
     
    7575== Changelog ==
    7676
     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
    7785= 1.4.0 =
    7886* Tested with WP 3.3.1
Note: See TracChangeset for help on using the changeset viewer.