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

transparent background image bug #22

Open
fefescript84 opened this issue Jun 7, 2023 · 1 comment
Open

transparent background image bug #22

fefescript84 opened this issue Jun 7, 2023 · 1 comment

Comments

@fefescript84
Copy link

If you have a transparent background png the alpha channel will failing...bugfix:
class GDLuminanceSource ...
...
public function GDLuminanceSource($gdImage, $width, $height)
{
...
for ($j = 0; $j < $height; $j++) {
for ($i = 0; $i < $width; $i++) {
$argb = imagecolorat($this->$gdImage, $i, $j);
$pixel = imagecolorsforindex($this->$gdImage, $argb);
$r = $pixel['red'];
$g = $pixel['green'];
$b = $pixel['blue'];
$a = $pixel['alpha']; //0..127
if ( $a >= 63 ) {
$this->luminances[] = 255;
} else {
if ($r == $g && $g == $b) {
// Image is already greyscale, so pick any channel.
$this->luminances[] = $r;//(($r + 128) % 256) - 128;
} else {
// Calculate luminance cheaply, favoring green.
$this->luminances[] = ($r + 2 * $g + $b) / 4;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
}
}
}
}

@fefescript84
Copy link
Author

Please somebody test it...
class IMagickLuminanceSource
...
public function _IMagickLuminanceSource(\Imagick $image, $width, $height)
{
...
$map = 'RGBA';
$mapLen = strlen($map);
// $image->newPseudoImage(0, 0, "magick:rose");
//$pixels = $image->exportImagePixels(1, 1, $width, $height, "RGB", \Imagick::PIXEL_CHAR);
$pixels = $image->exportImagePixels(1, 1, $width, $height, $map, \Imagick::PIXEL_CHAR);

    $array = [];
    $rgb   = [];

    $countPixels = count($pixels);
    for ($i = 0; $i < $countPixels; $i += $mapLen) {
        $r = $pixels[$i] & 0xff;
        $g = $pixels[$i + 1] & 0xff;
        $b = $pixels[$i + 2] & 0xff;
        $a = $pixels[$i + 3] & 0xff; //0: transparent
        if ( $a <= 63 ) { 
            $this->luminances[] = 255;
        } else {

            if ($r == $g && $g == $b) {
// Image is already greyscale, so pick any channel.

                $this->luminances[] = $r;//(($r + 128) % 256) - 128;
            } else {
// Calculate luminance cheaply, favoring green.
                $this->luminances[] = ($r + 2 * $g + $b) / 4;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
            }
        }
    }

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