2015-06-15 58 views
0

我想用文檔例如PHP imagick迭代器可以修改單獨的imagick修飾符嗎?

$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(); 

這是可能的,並會語法是什麼樣子修改從相同的迭代器如2個imagick對象?

回答

0

迭代器是標準的PHP迭代器,所以可以通過手動使用函數next()current()來迭代,並檢查它是否仍然有效,請使用valid()

所以像這樣'應該'的工作。

$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(); 
}