I have get the image pixel of an image at the particular point using getImagePixelColor.
$pixel = $image -> getImagePixelColor($x,$y);
Now I have modified that pixel's color using some method and now I want to set the new color of that pixel.
How can I do ?
There is a setColor function. But I got the pixel from the Imagick class. But the setColor function is in the ImagickPixel class. So how can I do it ?
->getImagePixelColor()
returns an ImagickPixel object anyways, so $pixel->setColor(...);
is all you need:
Ref: http://php.net/manual/en/imagick.getimagepixelcolor.php
ImagickPixel::setColor()
is the correct function but it is also necessary to sync the pixel Iterator so your manipulations are written back to the image.
Here is a short yet (almost) complete example that reads an image file, manipulates each pixel, and dumps it to a browser:
$img = new Imagick('your_image.png');
$iterator = $img->getPixelIterator();
foreach ($iterator as $row=>$pixels) {
foreach ( $pixels as $col=>$pixel ){
$color = $pixel->getColor(); // values are 0-255
$alpha = $pixel->getColor(true); // values are 0.0-1.0
$r = $color['r'];
$g = $color['g'];
$b = $color['b'];
$a = $alpha['a'];
// manipulate r, g, b and a as necessary
//
// you could also read arbitrary pixels from
// another image with similar dimensions like so:
// $otherimg_pixel = $other_img->getImagePixelColor($col,$row);
// $other_color = $otherimg_pixel->getColor();
//
// then write them back into the iterator
// and sync it
$pixel->setColor("rgba($r,$g,$b,$a)");
}
$iterator->syncIterator();
}
header('Content-type: '.$img->getFormat());
echo $img->getimageblob();