How to get GD2 generate PNG8 when resizing PNG8 images I have an image field which accepts jpg, jpeg, png. I've chosen the Image style: Medium on the display of that field. That style is configured at /admin/config/media/image-styles/manage/medium and it simply scales the image to 600px width.

On /admin/config/media/image-toolkit I can see that GD2 is being used.

My problem is that if the original image on that field is PNG8, the rendered thumbnail (by GD2) is PNG24. This results in much larger file for no reason.

Shouldn't GD2 respect the fact that the source is PNG8? Is there a way to fix this?
GD2 indeed doesn't check what palette size is the source image. From GDToolkit.php:

// Always save PNG images with full transparency.
if ($this-&gt;getType() == IMAGETYPE_PNG) {
  imagealphablending($this-&gt;getResource(), FALSE);
  imagesavealpha($this-&gt;getResource(), TRUE);
}


The only way I could hack PNG8 in there was:

// Always save PNG images with full transparency.
if ($this-&gt;getType() == IMAGETYPE_PNG) {
  imagealphablending($this-&gt;getResource(), FALSE);
  imagesavealpha($this-&gt;getResource(), TRUE);
  imagetruecolortopalette($this-&gt;getResource(), TRUE, 256); // ← hardcoded 256 hack for PNG8
}


I couldn't find a way of determining the palette size of the source image. So by leaving that hack of mine in there the risk is that PNG24 source images will be saved in the styles/ directory as PNG8 files.