dongmou9260 2017-12-26 07:14
浏览 81

通过图像迭代替换所有颜色,除了php中的黑色

I need to iterate through all pixels in an image to replace all but one color with black so that I can define all regions of a particular color. The code below is not working, the image remains unchanged. What am I doing wrong?

header('Content-Type: image/jpeg');

// open an image
$im = imagecreatefromjpeg('sunset.jpg');
$x_val = imagesx($im);
$y_val = imagesy($im);
$black = imagecolorallocate($im, 0, 0, 0);
for ($i = 0; $i < $x_val; $i++) {
    $color_index = imagecolorat($im, $i, $i);
    $colourarray = imagecolorsforindex($im, $color_index);
    if ($colourarray[0] != 170 && $colourarray[1] != 0 && $colourarray[2] != 0) {

        $color_index = imagecolorat($im, $i, $i);

        imagecolorset($im, $color_index, 0, 0, 0);
    } else {
        $color_index = imagecolorat($im, $i, $i);
        imagecolorset($im, $color_index, 170, 0, 0);
    }
}


imagejpeg($im);
imagedestroy($im);
  • 写回答

0条回答 默认 最新

    报告相同问题?