Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImagePngAlpha() different outputs with imagick and gd #689

Open
SoulFliktioN opened this issue Feb 25, 2024 · 0 comments
Open

ImagePngAlpha() different outputs with imagick and gd #689

SoulFliktioN opened this issue Feb 25, 2024 · 0 comments

Comments

@SoulFliktioN
Copy link

I encountered a problem with pngs with semi transparent areas. In my case I got a glow around a logo. Local everything was ok but not online. I figured out that local Imagick is enabled and online not. As I do not have the possibility to activate it online i was looking for an solution. I think I got it but would need some help!

So what I did was to change the try block for gd extension to:

// extracting alpha channel
$img = imagecreatefrompng($file);
imagesavealpha($img, false);
imagepng($img, $tempfile_plain);
imagedestroy($img);
			
// creating the alpha image	
$img = imagecreatefrompng($file);
$imgalpha = imagecreate($wpx, $hpx);
				
for ($c = 0; $c < 256; ++$c) {
 ImageColorAllocate($imgalpha, $c, $c, $c);
}

for ($xpx = 0; $xpx < $wpx; ++$xpx) {
 for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  $index = imagecolorat($img, $xpx, $ypx);
  $rgba = imagecolorsforindex($img, $index);
  $alpha = abs($rgba["alpha"] - 127) / 127 * 255;
  // not sure about the gamma correction
  //$alpha = (int) (pow(($alpha / 255), 2.2) * 255);
  imagesetpixel($imgalpha, (int) $xpx, (int) $ypx, (int) $alpha);
 }
}
							
imagepng($imgalpha, $tempfile_alpha);
imagedestroy($imgalpha);
$parsed = true;

In fact I just try to reproduce the result of the Imagick version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant