用GD改变图像颜色
问题描述:
我想了解PHP中的图像操作并编写一些代码来编辑照片。用GD改变图像颜色
所以,我正在阅读关于imagefilter()函数,但我想手动编辑collors。
我有一小片与ImageFilter的代码做一个形象深褐色
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagefilter($image, IMG_FILTER_COLORIZE, 55, 25, -10);
imagefilter($image, IMG_FILTER_CONTRAST, -10);
我想这样做,但没有ImageFilter中的();有可能的?
我明白它可能会获取图像中的颜色,然后更改它们并重新绘制它;
为了让图像色彩我有这样的:
$rgb = imagecolorat($out, 10, 15);
$colors = imagecolorsforindex($out, $rgb);
而这种打印:
array(4) {
["red"]=> int(150)
["green"]=> int(100)
["blue"]=> int(15)
["alpha"]=> int(0)
}
,我可以编辑这些值,并将其融入图片?
我将不胜感激任何形式的帮助:书籍,教程,代码片断。
答
使用imagesetpixel()函数。由于此功能需要使用颜色标识符作为第三个参数,因此您需要使用imagecolorallocate()来创建一个。
下面这减半每种颜色的色值的示例代码:现在,这里
$rgb = imagecolorat($out, 10, 15);
$colors = imagecolorsforindex($out, $rgb);
$new_color = imagecolorallocate($out, $colors['red']/2, $colors['green']/2, $colors['blue']/2);
imagesetpixel($out, 10, 15, $new_color);
一个简单的灰度滤镜:
list($width, $height) = getimagesize($filename);
$image = imagecreatefrompng($filename);
$out = imagecreatetruecolor($width, $height);
for($y = 0; $y < $height; $y++) {
for($x = 0; $x < $width; $x++) {
list($red, $green, $blue) = array_values(imagecolorsforindex($image, imagecolorat($image, $x, $y)));
$greyscale = $red + $green + $blue;
$greyscale /= 3;
$new_color = imagecolorallocate($out, $greyscale, $greyscale, $greyscale);
imagesetpixel($out, $x, $y, $new_color);
}
}
imagedestroy($image);
header('Content-Type: image/png');
imagepng($out);
imagedestroy($out);
小心使用imagecolorallocate
一个循环内,你可以不会分配比imagecolorstotal
更多的颜色返回到单个图像中。如果达到极限imagecolorallocate
将返回false,您可以使用imagecolorclosest
获取已分配的壁橱颜色。
不起作用! – 2013-03-21 07:38:24
我测试了它,它适用于我,你是如何测试它的? – MarcDefiant 2013-03-21 07:43:26
$ image = imagecreatefromjpeg($ _ photo); $ rgb = imagecolorat($ image,10,15); $ colors = imagecolorsforindex($ image,$ rgb); var_dump($ colors); (color)['red']/4,$ colors ['green']/4,$ colors ['blue']/4); $ color = ['blue']/4); imagesetpixel($ image,10,15,$ new_color); imagejpeg($ image,'grayscale-sun.jpg'); imagedestroy($ image); echo''; – 2013-03-21 07:45:30