Wednesday, January 21, 2026
HomeLanguagesPHP | Imagick setLastIterator() Function

PHP | Imagick setLastIterator() Function

The Imagick::setLastIterator() function is an inbuilt function in PHP which is used to set the Imagick iterator to the last image.

Syntax:

bool Imagick::setLastIterator( 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 programs illustrate the Imagick::setLastIterator() function in PHP:

Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
// Add a new image to same object, this
// will automatically move index to new
// image which is added.
$imagick->addImage(new Imagick(
  
// Move the cursor to first image
$imagick->setFirstIterator();
  
echo 'Index before setLastIterator() is '
          . $imagick->getIteratorIndex() . '<br>';
  
// Set the Imagick iterator to the last image
$imagick->setLastIterator();
  
echo 'Index after setLastIterator() is '
         . $imagick->getIteratorIndex() . '<br>';
?>


Output:

Index before setLastIterator() is 0
Index after setLastIterator() is 1

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick();
   
// Array of images
$images = [
];
   
// Read the images
$imagick->readImages($images);
  
// Move iterator to first image
$imagick->setFirstIterator();
  
echo 'Index before setLastIterator() is ' 
        . $imagick->getIteratorIndex() . '<br>';
  
// Set the Imagick iterator to the last image
$imagick->setLastIterator();
  
echo 'Index after setLastIterator() is ' 
        . $imagick->getIteratorIndex() . '<br>';
?>


Output:

Index before setLastIterator() is 0
Index after setLastIterator() is 4

Reference: https://www.php.net/manual/en/imagick.setlastiterator.php

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS