2024 日本 PHP 研討會

Imagick::getPixelIterator

(PECL imagick 2, PECL imagick 3)

Imagick::getPixelIterator傳回 MagickPixelIterator

描述

public Imagick::getPixelIterator(): ImagickPixelIterator

傳回 MagickPixelIterator。

參數

此函式沒有參數。

回傳值

成功時傳回 ImagickPixelIterator。

錯誤/例外

發生錯誤時拋出 ImagickException。

範例

範例 #1 Imagick::getPixelIterator()

<?php
function getPixelIterator($imagePath) {
$imagick = new \Imagick(realpath($imagePath));
$imageIterator = $imagick->getPixelIterator();

foreach (
$imageIterator as $row => $pixels) { /* 迴圈處理每一列像素 */
foreach ($pixels as $column => $pixel) { /* 迴圈處理每一行中的像素(欄) */
/** @var $pixel \ImagickPixel */
if ($column % 2) {
$pixel->setColor("rgba(0, 0, 0, 0)"); /* 將每隔一個像素塗黑 */
}
}
$imageIterator->syncIterator(); /* 同步迭代器,每次迭代都必須執行 */
}

header("Content-Type: image/jpg");
echo
$imagick;
}

?>

新增註解

使用者貢獻的註解 1 則註解

1
Sebastian Herold
16 年前
閱讀 Mikko 部落格上的一篇文章對我很有幫助。他展示了如果定期同步 PixelIterator,它就不是唯讀的。

<?php
/* 建立一個新的影像物件 */
$im = new Imagick( "strawberry.png" );

/* 取得像素迭代器 */
$it = $im->getPixelIterator();

/* 迴圈遍歷每一列像素 */
foreach( $it as $row => $pixels )
{
/* 處理每隔一列 */
if ( $row % 2 )
{
/* 迴圈遍歷該列中的每個像素(欄) */
foreach ( $pixels as $column => $pixel )
{
/* 將每隔一個像素塗黑 */
if ( $column % 2 )
{
$pixel->setColor( "black" );
}
}

}

/* 同步迭代器,每次迭代都必須執行 */
$it->syncIterator();
}

/* 顯示影像 */
header( "Content-Type: image/png" );
echo
$im;

?>
To Top