Tuesday, March 12, 2024

PHP: Checking if an image is WebP

Let's say you have a WebP image:

$this->image = imagecreatefromwebp('path/to/your/image.webp');

If you want to check if an image is a resource, In PHP 7.3 the following returns true:

is_resource($this->image)

In PHP 8.0, the behavior changes because the GD library's image resources are replaced with GdImage objects. This change means that image resources created with GD functions, like imagecreatefromwebp(), no longer return a resource type. Instead, they return an instance of the GdImage class. In other words, for a WebP image, is_resource($this->image) returns false and for PHP 8, you should update your code as follows:

is_resource($this->image) || $this->image instanceof GdImage

No comments:

Post a Comment