doumei1926 2015-06-15 17:54
浏览 38

PHP想象迭代器可以修改一个单独的imagick修饰符吗?

I want to modify 2 imagick objects from the same iterator eg using the docs example

$imagick = new \Imagick(realpath($imagePath));
$imagick2 = clone $imagick;
$imageIterator = new \ImagickPixelIterator($imagick);

/* Loop through pixel rows */
foreach ($imageIterator as $pixels) { 
    /* Loop through the pixels in the row (columns) */
    foreach ($pixels as $column => $pixel) { 
        /** @var $pixel \ImagickPixel */
        if ($column % 2) {
            /* Paint every second pixel black*/
            $pixel->setColor("rgba(0, 0, 0, 0)");
        } else {
            //do something to $imagick2 here 
            $pixel->setColor("rgba(255, 255, 255, 0)");
        }
    }

    /* Sync the iterator, this is important to do on each iteration */
    $imageIterator->syncIterator();

is this possible and what would the syntax look like?

  • 写回答

1条回答 默认 最新

  • dongmeiba6151 2015-06-15 21:46
    关注

    The iterators are standard PHP iterators, so the can be iterated through manually using the function next() and current() and checking that it is still valid with valid().

    So something like this 'should' work.

    $imagick = new \Imagick(realpath($imagePath));
    $imagick2 = clone $imagick;
    $imageIterator1 = new \ImagickPixelIterator($imagick);
    $imageIterator2 = new \ImagickPixelIterator($imagick2);
    
    $imageIterator2->reset(); //make sure iterator is at the start.
    
    /* Loop through pixel rows */
    foreach ($imageIterator1 as $pixelRow1) { 
    
        if (!$imageIterator2->valid()) {
            // need to check validity when iterating manually
            break;
        }
    
        $pixelRow2 = $imageIterator2->current();
        /* Loop through the pixels in the row (columns) */
    
        foreach ($pixelRow1 as $column => $pixel1) { 
            /** @var $pixel \ImagickPixel */
            if ($column % 2) {
                /* Paint every second pixel black*/
                $pixel1->setColor("rgba(0, 0, 0, 0)");
            }
            else{
                $pixel2 = $pixelRow2[$column];
                //do something to $imagick2 here 
                $pixel2->setColor("rgba(255, 255, 255, 0)");
            }
    
            $count++;
        }
    
        $imageIterator2->next();
        /* Sync the iterator, this is important to do on each iteration */
        $imageIterator1->syncIterator();
        $imageIterator2->syncIterator();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)