閱讀 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;
?>