How to create a new image from a WEBP file or URL using imagecreatefromwebp() function in PHP?

In PHP, imagecreatefromwebp() is an inbuilt PHP function that is used to create a new image from a WEBP file or URL. imagecreatefromwebp() returns an image identifier representing the image obtained from the given filename.

imagecreatefromwebp() can be used whenever we want to edit the images after loading them from a WEBP file. Using imagewebp(), an image can be converted into WBMP.


resource imagecreatefromwebp(string $filename)


imagecreatefromwebp() takes only one parameter, $filename. It holds the name of the image.

Return Values

imagecreatefromwebp() returns an image resource identifier on success, and it gives an error on false.

Example 1

   // Loading the WEBP image from the local drive folder
   $img = imagecreatefromwebp('C:\xampp\htdocs\Images\img31.webp');
   // View the loaded image in the browser


Note − The above PHP code will load the content into the browser in an
unsupported form of text because the browser does not support the WEBP image format.

Example 2

   // Load a WEBP image from the local drive folder
   //We can convert the image to WEBP using the online converter
   //or using the imagewebp() function
   $img = imagecreatefromwebp('C:\xampp\htdocs\Images\img31.webp');

   // Save the GIF image into the given local drive folder path.


Published on 09-Aug-2021 10:00:13