How do you convert an image to black and white in PHP

后端 未结 7 1555
长情又很酷
长情又很酷 2020-12-01 14:34

How does one go about converting an image to black and white in PHP?

Not just turning it into greyscale but every pixel made black or white?

相关标签:
7条回答
  • 2020-12-01 15:35

    This function work like a charm

        public function ImageToBlackAndWhite($im) {
    
        for ($x = imagesx($im); $x--;) {
            for ($y = imagesy($im); $y--;) {
                $rgb = imagecolorat($im, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8 ) & 0xFF;
                $b = $rgb & 0xFF;
                $gray = ($r + $g + $b) / 3;
                if ($gray < 0xFF) {
    
                    imagesetpixel($im, $x, $y, 0xFFFFFF);
                }else
                    imagesetpixel($im, $x, $y, 0x000000);
            }
        }
    
        imagefilter($im, IMG_FILTER_NEGATE);
    
    }
    
    0 讨论(0)
提交回复
热议问题