Search code examples
phpgd

deinterlace png in php


I try to deinterlace a png picture using php functions.

I found somewhere tips which leaded to this solution:

$img = imagecreatefrompng("interlaced.png");

imageinterlace($img, 0);
$black = imagecolorallocate($img, 0,0,0);
imagecolortransparent($img, $black);
imagepng($img, "deinterlaced.png");

Unfortunately, this will not only keep the transparent areas, but extend them when the picture uses somewhere real black.

Is there another possibility to deinterlace without using imagecolorallocate?

I already tried using imagesavealpha, but it didn't work, or I used it wrong:

$img = imagecreatefrompng("interlaced.png");

imagealphablending($png, false);
imagesavealpha($png, true);

imageinterlace($img, 0);
imagepng($img, "deinterlaced.png");

This causes all transparent areas to be black (this is probably the reason, why I chose back then rgb0,0,0 in imagecolortransparent)


Solution

  • Your second code block would work fine but for a small mistake; imagealphablending and imagesavealpha are passed the incorrect resource, i.e., $png instead of $img.

    Corrected code:

    $img = imagecreatefrompng("interlaced.png");
    
    imagealphablending($img, false);
    imagesavealpha($img, true);
    
    imageinterlace($img, 0);
    imagepng($img, "deinterlaced.png");