PHP | imagewebp() Function

The imagewebp() function is an inbuilt function in PHP which is used to display image to browser or file. The main use of this function is to view an image in the browser, convert any other image type to WebP and alter the quality of the image.
Syntax: 

bool imagewebp( resource $image, int $to, int $quality)

Parameters: This function accepts three parameters as mentioned above and described below: 
 

  • $image: It specifies the image resource to work on.
  • $to (Optional): It specifies the path to save the file to.
  • $quality (Optional): It specifies the quality of the image.

Return Value: This function returns TRUE on success or FALSE on error.
Below examples illustrate the imagewebp() function in PHP:
Example 1: In this example we will be viewing an image in browser. 
 

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    
// Load an image from local webp file
// Images can be converted into webp 
// using imagewebp() function or other
// online convertors
$im = imagecreatefromwebp('./geeksforgeeks.webp');
  
// View the loaded image in browser
// using imagewebp() function
header('Content-type: image/webp');  
imagewebp($im);
imagedestroy($im);
?>

chevron_right


Output: 
 



Example 2: In this example we will convert PNG into WEBP. 
 

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
    
// Load an image from local webp file
// Images can be converted into webp 
// using imagewebp() function or other
// online convertors
$im = imagecreatefromwebp('./geeksforgeeks.webp');
  
// Convert the image into webp
// using imagewebp() function
imagewebp($im, 'converted.webp');
imagedestroy($im);
?>

chevron_right


Output: 

This will save the WEBP version of image in the same folder where your PHP script is.

Program 3: In this example we will alter the image quality. 
 

php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Load an image from local webp file
// Images can be converted into webp 
// using imagewebp() function or other
// online convertors
$im = imagecreatefromwebp('./geeksforgeeks.webp');
  
// Convert the image into webp using imagewebp()
// function and view in the browser
header('Content-type: image/webp');
imagewebp($im, null, 0);
imagedestroy($im);
?>

chevron_right


Output: 
 

Reference:  https://www.php.net/manual/en/function.imagewebp.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.