• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

218
Views
Get Alpha from PNG created with imagepng()

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.

almost 3 years ago · Santiago Trujillo
2 answers
Answer question

0

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));
almost 3 years ago · Santiago Trujillo Report

0

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>
almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error