IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

对比Imagick和Gmagick的像素迭代功能

火丁笔记 2010-10-17 22:21:01 累计浏览 3,539 次
本机暂存

本文对比ImagickGmagick的像素迭代功能:

数据生成代码

以下是代码片段:
<?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);
    }
}

?>
Imagick实现代码

以下是代码片段:
<?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’);

?>
Gmagick实现代码
以下是代码片段:
<?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

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 使用gettext来支持PHP的多语言 (累计阅读 39,270)
  2. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,164)
  3. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,408)
  4. 我的PHP,Python和Ruby之路 (累计阅读 13,147)
  5. include(“./file.php”)和include(“file.php”)区别 (累计阅读 12,789)
  6. 15个最好的免费开源电子商务平台 (累计阅读 12,541)
  7. Redis消息队列的若干实现方式 (累计阅读 12,088)
  8. 到底什么是MVC? (累计阅读 11,866)
  9. 整理了一份招PHP高级工程师的面试题 (累计阅读 11,708)
  10. Rolling cURL: PHP并发最佳实践 (累计阅读 11,488)