WordPress.org

Ready to get started?Download WordPress

Forums

Imsanity
Resizing rounds down (4 posts)

  1. mimetic
    Member
    Posted 1 year ago #

    The resizing makes the mistake of rounding down the image dimensions when calculating the size!

    For example, a 3000 x 2000 pixel image (3:2 format) should reduce to a 1024 x 683 pixel image, but WordPress reduces it to a 1024 x 682 pixel image. Note that ImageMagick itself does, too.

    Why does this matter? Because PhotoShop doesn't make this mistake, and if you have a design that requires a good quality image, you probably have a lot of 1024x683 images!

    Please, it would be great to have the option use 'properly' sized images. Here's a piece of PHP code that does it right :

    // Resizes to whichever is larger, width or height
    if ($w>$h) {
    	$mw = $maxWidth;
    	$mh = round( ($maxWidth/$w)*$h );
    } else {
    	$mh = $maxHeight;
    	$mw = round( ($maxHeight/$h)*$w );
    }
    
    // Resize image using the lanczos resampling algorithm based on width
    $image->resizeImage($mw,$mh,Imagick::FILTER_LANCZOS,0.9);
    
    // Set to use jpeg compression
    $image->setImageCompression(Imagick::COMPRESSION_JPEG);
    // Set compression level (1 lowest quality, 100 highest quality)
    $image->setImageCompressionQuality($jpegquality);
    // Strip out unneeded meta data
    //$image->stripImage();
    // Writes resultant image to output directory
    $image->writeImage($f);
    // Destroys Imagick object, freeing allocated resources in the process

    http://wordpress.org/extend/plugins/imsanity/

  2. Jason
    Member
    Plugin Author

    Posted 1 year ago #

    Hmm, this should probably be reported as a WordPress bug. The offending lines are in the wp_constrain_dimensions in the file wp-includes/media.php and look like this:

    $w = intval( $current_width  * $ratio );
    $h = intval( $current_height * $ratio );

    intval always rounds down. Not sure if WordPress has some special reason for doing this. My guess is probably not.

  3. simo78
    Member
    Posted 6 months ago #

    This sounds like the same issue I am having. I have a 1200 x 800 image and a fresh WordPress 3.8 installation with only Imsanity plugin installed. When uploading image files, the medium size thumbnail file becomes 300 x 199 pixels. When the plugin is deactivated and files with same size are uploaded, the medium size thumbnail files are created at 300 x 200 pixels.

    Looking at wp-includes/media.php, the wp_constrain_dimensions seems to contain also this:

    // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
            // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result.
            // Thus we look for dimensions that are one pixel shy of the max value and bump them up
            if ( $did_width && $w == $max_width - 1 )
                    $w = $max_width; // Round it up
            if ( $did_height && $h == $max_height - 1 )
                    $h = $max_height; // Round it up

    I wonder if for some reason this piece of code is not executed when called by Imsanity?

  4. Jason
    Member
    Plugin Author

    Posted 6 months ago #

    Hmm, it looks like they tried to fix the original rounding error by trying to guess what the dimensions should be and then correcting them, not accounting for proper scale. There could be more to it than what I am seeing, but I think the original bug wasn't fixed correctly.

    My guess is that this is happening:

    Without Plugin:

    Image is 1200 x 800 - WordPress then creates optimized media from the 1200 x 800 file and gets lucky because the math rounds nicely.

    With Plugin:

    Image is scaled to 1024 by Imsanity (or whatever your setting) - WordPress then creates optimized media from the scaled 1024 file and the math doesn't round as nicely.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic