[resolved] PNG colour palette help (6 posts)

  1. tdsm
    Posted 4 years ago #

    Hi all,

    I'm uploading PNGs into my gallery. They are 8-bit with 256 colours. They look fine but when I generate thumbnails and other custom image sizes, they look awful. If I download the small image files and open them in Photoshop the colour palette is down from 256 to something like 150 colours.

    I think this is the piece of code in the core which creates the resized PNG and sets the number of colours;

    // convert from full colors to index colors, like original PNG.
    if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )
    imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );

    Any help would be appreciated.

    Thank you!

  2. tdsm
    Posted 4 years ago #

    Aaaaaah!! Inspiration. This might help a lot of you - far better quality PNGs thanks to a snippet of code which I've adapted and inserted into wp-includes/media.php at line 431

    if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) ) {
    		$colors_handle = imagecreatetruecolor( $dst_w, $dst_h );
    		imagecopymerge( $colors_handle, $newimage, 0, 0, 0, 0, $dst_w, $dst_h, 100 );
    		imagetruecolortopalette( $newimage, true, 256 );
    		imagecolormatch( $colors_handle, $newimage );
    		imagedestroy( $colors_handle );

    Not sure why this isn't in the core if the results are so much better??

  3. esmi
    Forum Moderator
    Posted 4 years ago #

    No! Never, ever, edit WordPress core scripts. And do not encourage others to do so. Editing core scripts can bring down your entire site and/or open security holes for hackers to use.

  4. tdsm
    Posted 4 years ago #

    Hi Esmi,

    In all previous instances, I agree with what you're saying and have never touched the core, but what's the alternative? Unless I can hook into the function then I'm left with horrible looking images. In this instance there are no security/stability issues.

    If you can think of a solution then I'd be delighted to avoid changes to the core.

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    Start by ensuring that your hosts are using the most up-to-date version of GD Library.

  6. juner
    Posted 3 years ago #

    Thank you so much, tdsm.
    I've tried a lot to improve my thumbnails quality, even bought a paid plugin, without success. Thanks for sharing this solution, it worked perfectly well.

Topic Closed

This topic has been closed to new replies.

About this Topic