数据生成代码
以下是代码片段: <?php // filename: data.php $data = array(); for ($x = 0; $x < 100; $x++) { for ($y = 0; $y < 100; $y++) { $data[$x][$y] = ’#’ . str_repeat($y % 10, 6); } } ?> |
以下是代码片段: <?php // filename: imagick.php 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(’imagick.png’); ?> |
以下是代码片段: <?php // filename: gmagick.php require ’data.php’; $image = new Gmagick(); $image->newimage(100, 100, ’white’, ’png’); for ($row = 0; $row < 100; $row++) { for ($column = 0; $column < 100; $column++) { $draw = new GmagickDraw(); $pixel = new GmagickPixel(); $pixel->setcolor($data[$row][$column]); $draw->setfillcolor($pixel); $draw->point($column, $row); $image->drawimage($draw); } } $image->writeimage(’gmagick.png’); ?> |
比较而言,Imagick的实现更简单些,而Gmagick的实现因为没有PixelIterator的概念而稍显复杂。不过,Gmagick没有PixelIterator的概念并不是Bug,而是为了和GraphicsMagick Wand C API接口保持一致。
BTW:高级例子Precision color searching with Gmagick and Amazon Elastic MapReduce