Thursday, October 9, 2025
HomeLanguagesPHP | ImagickPixelIterator syncIterator() Function

PHP | ImagickPixelIterator syncIterator() Function

The ImagickPixelIterator::syncIterator() function is an inbuilt function in PHP which is used to sync the pixel iterator.

Syntax:

bool ImagickPixelIterator::syncIterator( void )

Parameters:This function doesn’t accepts any parameter.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickPixelIterator::syncIterator() function in PHP:

Program 1:




<?php
// Create a new imagick object
$imagick = new Imagick();
  
// Create a image on imagick object
$imagick->newImage(800, 250, 'black');
  
$imageIterator = $imagick->getPixelIterator();
  
// Loop through rows pixel value
foreach ($imageIterator as $pixels) {
  
    // Loop through the pixels in the row value
    foreach ($pixels as $column => $pixel) {
  
        if ($column % 11) {
            $pixel->setColor('yellow');
        }
    }
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
  
// Loop through pixel rows again for new color
foreach ($imageIterator as $pixels) {
    // Loop through the pixels in the row
    foreach ($pixels as $column => $pixel) {
        if ($column % 20) {
            $pixel->setColor('blue');
        }
    }
  
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
// Create a new imagick object
$imagick = new Imagick(
  
$imageIterator = $imagick->getPixelIterator();
  
// Loop through pixel rows
foreach ($imageIterator as $pixels) {
    // Loop through the pixels in the row
    foreach ($pixels as $column => $pixel) {
  
        // Get the current HSL
        $HSL = $pixel->getHSL();
   
        // Set the HSL and change only saturation
        $pixel->setHSL($HSL['hue'], 2, $HSL['luminosity']);
    }
    // Sync the iterator after each iteration
    $imageIterator->syncIterator();
}
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagickpixeliterator.synciterator.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11876 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS