WordPress.org

Forums

wp_get_image_editor makes PNG filesize larger instead of smaller (5 posts)

  1. wpprogrammeurs.nl
    Member
    Posted 2 years ago #

    Hi, I'm working on a form upload image resizer based on the new wp_get_image_editor, however when I try to resize PNG images down the resulting filesize gets LARGER <?> , the resize function works fine for other mime types (gif,jpg).

    Any ideas how to prevent the resized PNG's from blowing up disproportionally ?? (going from a 960x800px 117 Kb -> 680x~560px -> 262 Kb )

    Here's my code snippet :

    function resize_680($imfile = '') {
    	if (strlen($imfile) < 6) return false;
    	if (!is_writable($imfile)) return false;
    	list($width, $height, $type, $attr) = @getimagesize($imfile);
    	if ($width < 680) return false;
    	$newsize = wp_constrain_dimensions( $width, $height, 680, 680 );
    	$image = wp_get_image_editor( $imfile );
    	if ( ! is_wp_error( $image ) )
    		{
    		$image->resize( $newsize[0],$newsize[1], true );
    		$image->save( $imfile );
    		}
    }
  2. bcworkz
    Member
    Posted 2 years ago #

    The default image quality is 90, your source image probably has a much lower setting. Use $image->set_quality() to set it to something between 1 and 100.

  3. wpprogrammeurs.nl
    Member
    Posted 2 years ago #

    @bcworkz I agree that set_quality() Would logically be the solution, however ... I have tried that.
    Even with ridiculously low settings PNG images still swell up like a parched sponge thrown into a hottub.

  4. jcapen
    Member
    Posted 2 years ago #

    I'm having a similar problem. I'm uploading an optimize 8 bit png and all the generated sizes are 24 bit. For example, an original 720x720 png was 19K, however, the resized 220x220 was 29K, 330x330 51K, and 460x460 111K. Not only are the thumbnails significantly larger than the original but they are not optimized with lossless compression. I ran them through a compression utility which cut the size by about 40%! I've searched the internet for hours but have only found one reference to the problem and no solution.

  5. bcworkz
    Member
    Posted 2 years ago #

    Good catch noticing the 24 bit images! (wtf?) It appears the GD image editor is the only one that knows enough to convert 24bit to indexed. Not clear if it always does this, and if not, how to force it, nor the index size. So it may not help much.

    However, I do know how to force the GD editor to be used. Hook the 'wp_image_editors' filter. Return array( 'WP_Image_Editor_GD' ) . See if that helps any. There's a chance you will just get an error, but worth a try. Good luck.

Topic Closed

This topic has been closed to new replies.

About this Topic