Use the imagesetpixel() function. Since this function needs an color identifier as third parameter you need to use imagecolorallocate() to create one.
Here an example code which halves the color values of each color:
$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);
Now here a simple greyscale filter:
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);
Be careful with using imagecolorallocate
inside of a loop, you can not allocate more colors than imagecolorstotal
returns inside of an single image. If you reached the limit imagecolorallocate
will return false and you can use imagecolorclosest
to get the closet color which already has been allocated.