I try to get the alpha
of a PNG. I'm doing this with imagepng()
.
My issue is alpha
returns only 0.
My code for making PNG with alpha
$x = 1;
$y = 1;
$gd = imagecreatetruecolor($x, $y);
imagesetpixel($gd, 0,0, imagecolorallocatealpha($gd, 200,200,200,1));
imageAlphaBlending($gd, false);
imageSaveAlpha($gd, true);
imagepng($gd,"test.png");
imagedestroy($gd);
My code for reading PNG alpha
$im = imagecreatefrompng("test.png");
$rgb = imagecolorat($im, 0, 0);
$colors = imagecolorsforindex($im, $rgb);
$red = (int) $colors["red"];
$blue = (int) $colors["blue"];
$green = (int) $colors["green"];
$alpha = (int) $colors["alpha"]; // return only 0
I don't know why it returns only 0 and not 1.
Frank,
Here is your solution with code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<a href="index.php" class="navbar-brand">
<?php
$x = 1;
$y = 1;
$gd = imagecreatetruecolor($x, $y);
imageAlphaBlending($gd, false);
imageSaveAlpha($gd, true);
imagesetpixel($gd, 0, 0, imagecolorallocatealpha($gd, 200, 200, 200, 1));
imagepng($gd, 'img/logo.png" ');
imagedestroy($gd);
$im = imagecreatefrompng('img/logo.png');
$rgb = imagecolorat($im, 0, 0);
$colors = imagecolorsforindex($im, $rgb);
$red = (int) $colors["red"];
$blue = (int) $colors["blue"];
$green = (int) $colors["green"];
echo $alpha = (int) $colors["alpha"]; // return only 0
?>
</a>
</body>
</html>
You should call imageAlphaBlending
and imageSaveAlpha
before calling imagesetpixel
:
imageAlphaBlending($gd, false);
imageSaveAlpha($gd, true);
imagesetpixel($gd, 0,0, imagecolorallocatealpha($gd, 200,200,200,1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With