Support » Fixing WordPress » PNG colour palette help

  • Resolved tdsm


    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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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??



    Forum Moderator

    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.

    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.



    Forum Moderator

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

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PNG colour palette help’ is closed to new replies.