开发者

Setting the grayscale value of a pixel in php

开发者 https://www.devze.com 2023-03-15 14:20 出处:网络
I have a jpeg image resource loaded into a variable in php. Given a grayscale value, like 6, how can I set a single, particular pixel to that grayscale value? My client has made it exceedingly clear t

I have a jpeg image resource loaded into a variable in php. Given a grayscale value, like 6, how can I set a single, particular pixel to that grayscale value? My client has made it exceedingly clear that there is a big difference between grayscale and rgb.

Is this even doable in php with the GD library? If so, how?

Note: The script does indeed grayscale the entire image, but using an obscure algorithm. My script obtains the RGB for each pixel, and obtains the grayscale value corresponding to the algorithm. It just needs to now transform that pixel to that parti开发者_开发知识库cular grayscale value.


You will probably find the the imagesetpixel function handy. The following is an example of how you could use it:

// $image = <GD image resource>

for($x = 0; $x < $width; x++)
{
    for($y = 0; $y < $height; $y++)
    {
        $value = specialFunction($rgb_value);
        // Depending on what the above function returns, the call below
        // might have to be changed
        $color = imagecolorallocate($image, $value, $value, $value);
        imagesetpixel($image, $x, $y, $color);
    }
}


Do you have access to the terminal or something along those lines to install Image Magick. If so, then you can could use the following

convert image.jpg -colorspace Gray image-bw.jpg

I'm not sure how to do it with the PHP GD Library sorry.

0

精彩评论

暂无评论...
验证码 换一张
取 消