libern / qr-code-reader

Simple PHP QR Code Reader / Decoder
92 stars 28 forks source link

transparent background image bug #22

Open fefescript84 opened 1 year ago

fefescript84 commented 1 year ago

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 commented 1 year ago

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;
            }
        }
    }