PHP: how to convert non-truecolor image to truecolor image?

How to do it with GD?

+3
source share
3 answers

Create a new truecolor (t) image resource with the same dimensions as the original image, and then copy (s) to (t).

eg. (without error handling):

$imgSource = imagecreatefromgif('xyz.gif');
$width = imagesx($imgSource);
$height = imagesy($imgSource);
$imgTC = imagecreatetruecolor($width, $height);
imagecopy($imgTC, $imgSource, 0, 0, 0, 0, $width, $height);
// save or send $imgTC

You will have both images in memory in gd2 format (4 bytes per pixel? 5?), So you better check memory_limit before trying this with large images.

+7
source
+1

Since in PHP 5.5 there is a quick, simple and less hungry solution: just use the imagepalettetotruecolorPHP Function:

$imgSource = imagecreatefromgif('xyz.gif');
if (!imageistruecolor($imgSource)) {
    imagepalettetotruecolor($imgSource);
}
0
source

Source: https://habr.com/ru/post/1720295/


All Articles