Generally, image processing is simple, such as thumbnails and watermarks, but sometimes it is more complicated, such as pixel iteration. in this article, we use an example to compare the pixel iteration functions of Imagick and Gmagick: pixel data generation code & lt ;? Php $ data = array ()... "> <LINKhref =" http://www.php100.com//st
Generally, image processing is simple, such as thumbnails and watermarks, but sometimes it is more complicated, such as pixel iteration. in this article, we use an example to compare the pixel iteration functions of Imagick and Gmagick:
Pixel data generation code
$ Data = array ();
For ($ row = 0; $ row <100; $ row ++ ){
For ($ column = 0; $ column <100; $ column ++ ){
$ Data [$ row] [$ column] = '#'. str_repeat ($ column % 10, 6 );
}
}
?>
Imagick iterative write pixel
Require 'data. php ';
$ Image = new Imagick ();
$ Image-> newimage (100,100, '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: When PixelIterator is used in Imagick to write pixels, you need to call the syncIterator operation (read pixels are not required ).
Gmagick iterative write pixel
Require 'data. php ';
$ Image = new Gmagick ();
$ Image-> newimage (100,100, 'white', 'PNG ');
$ Pixel = new GmagickPixel ();
$ Draw = new GmagickDraw ();
For ($ row = 0; $ row <100; $ row ++ ){
For ($ column = 0; $ column <100; $ 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 generated image is as follows:
Pixel.png
Previously, I demonstrated how to write a script during the workshop and read it again (the generated pixel.png is used ):
Imagick iterative read pixel
$ 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 pixel
$ 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: The color obtained by reading pixels in Imagick and Gmagick is in RGB format, but the data format is different.
In general, the implementation of Imagick is simpler, while the implementation of Gmagick is a little complicated because it does not have the concept of PixelIterator. However, Gmagick does not have the PixelIterator concept and is not a Bug, but to be consistent with the GraphicsMagick Wand c api.