Usually, image processing work is such as thumbnails, watermarks and other simple work, but sometimes more complex, such as pixel iterations, this article through an example of Imagick and Gmagick pixel iterative function:
Pixel Data Generation Code
<?php
$data = Array ();
for ($row = 0; $row < $row + +) {
for ($column = 0; $column < $column + +) {
$data [$row] [$column] = ' # '. Str_repeat ($column% 10, 6);
}
}
?>
Imagick iterative Write pixels
<?php
Require ' data.php ';
$image = new Imagick ();
$image->newimage (+, ' white ', ' png ');
$iterator = $image->getpixeliterator ();
foreach ($iterator as $row => $pixels) {
foreach ($pixels as $column => $pixel) {
$pixel->setcolor ($data [$row] [$column]);
}
$iterator->synciterator ();
}
$image->writeimage (' pixel.png ');
?>
Note: You need to invoke the Synciterator action (read pixels not) When you use Pixeliterator to write pixels in Imagick.
Gmagick iterative Write pixels
<?php
Require ' data.php ';
$image = new Gmagick ();
$image->newimage (+, ' white ', ' png ');
$pixel = new Gmagickpixel ();
$draw = new Gmagickdraw ();
for ($row = 0; $row < $row + +) {
for ($column = 0; $column < $column + +) {
$pixel->setcolor ($data [$row] [$column]);
$draw->setfillcolor ($pixel);
$draw->point ($column, $row);
$image->drawimage ($draw);
$pixel->clear ();
$draw->clear ();
}
}
$image->writeimage (' pixel.png ');
?>
The resulting picture looks like this:
Pixel.png
The preceding shows how to write pixels in an iteration, and then see how to read pixels (using the generated pixel.png):
Imagick iterative Read pixels
<?php
$data = Array ();
$image = new Imagick (' pixel.png ');
$iterator = $image->getpixeliterator ();
foreach ($iterator as $row => $pixels) {
foreach ($pixels as $column => $pixel) {
$data [$row] [$column] = $pixel->getcolor ();
}
}
Print_r ($data);
?>
Gmagick iterative Read pixels
<?php
$data = Array ();
$image = new Gmagick (' pixel.png ');
$width = $image->getimagewidth ();
$height = $image->getimageheight ();
for ($row = 0; $row < $width; $row + +) {
for ($column = 0; $column < $height; $column + +) {
$cropped = Clone $image;
$histogram = $cropped->cropimage (1, 1, $column, $row)
->quantizeimage (1, Gmagick::colorspace_rgb, 0, False, false)
->getimagehistogram ();
$data [$row] [$column] = $histogram [0]->getcolor ();
}
}
Print_r ($data);
?>
Note: In Imagick and Gmagick, the color of the pixel is RGB, but the data format is different.
Overall, the implementation of Imagick is simpler, and the implementation of Gmagick is slightly more complex because there is no pixeliterator concept. However, the concept of Gmagick without pixeliterator is not a bug, but to be consistent with the GraphicsMagick Wand C API interface.