You could also make use of the SPL to filter the contents of a DirectoryIterator
using your isImage
function by extending the abstract FilterIterator
class.
class ImageIterator extends FilterIterator {
public function __construct($path)
{
parent::__construct(new DirectoryIterator($path));
}
public function accept()
{
return isImage($this->getInnerIterator());
}
}
You could then use iterator_count
(or implement the Countable
interface and use the native count
function) to determine the number of images. For example:
$images = new ImageIterator('/path/to/images');
printf('Found %d images!', iterator_count($images));
Using this approach, depending on how you need to use this code, it might make more sense to move the isImage
function into the ImageIterator
class to have everything neatly wrapped up in one place.