Share
Preview
How do you create WebP images from JPEG, PNG or GIF files?
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
How To Create WebP Images With PHP.
by Alex
WebP


Hey,
Alex here.

WebP is an image format specifically designed for the web.

JPEGs, PNGs and GIFs are still popular, but WebP is spreading rapidly.
In fact, many websites and services (including WordPress and Google itself) are choosing it as their standard format.

So, how can you create WebP images with PHP, if you have JPEG, PNG or GIF images?
Let's find out.



You can use the imagewebp() function to create a WebP image with PHP.

This function takes three arguments:
  1. An image resource.
  2. Optionally, the path where to save the WebP file.
  3. Optionally, the image quality value from 0 to 100.


First of all, you need an image resource.
You need to use one of the PHP image* functions to get this resource.
For example:
  • imagecreatefromjpeg() to create a resource from a JPEG file.
  • imagecreatefrompng() to create a resource from a PNG file.
  • imagecreatetruecolor() to create an empty image from scratch.
  • ...and so on.

You can find the complete list of these functions here.

So, let's say that you have the image.jpg file, and you want to create a WebP image from it.
The first step is to use
imagecreatefromjpeg() to get the image resource:

$file = 'image.jpg';
$resource = imagecreatefromjpeg($file);


Next, you can use imagewebp() to save the WebP image into a new file:

$dest = 'new_image.webp';
imagewebp($resource, $dest);


And that's it: you have your
new_image.webp image in the WebP format.



Now, what if you want to automatically convert all your images into WebP in real-time, without having to save each file first?
To do this, you need to create a special PHP script that works as an automatic image converter.

So, let's say that you have this HTML code:

<div>
<img src="myImage.jpg">
</div>



You want all images such as the one above automatically converted into the WebP format.

What you need to do is to replace all the images with the PHP image converter script, passing the image name as a request parameter.
Like this:

<div>
<img src="webp.php?img=myImage.jpg">
</div>



The webp.php script does the following:
  • Reads the image file having the same name as the img request parameter.
  • Converts the image into the WebP format.
  • Streams the WebP image data back to the browser.

Here's how it works:

$img = $_REQUEST['img'];
$resource = imagecreatefromjpeg($img);
header('Content-Type: image/webp');
echo imagewebp($resource);



And that's it!
Remember to properly validate the
$img variable and make sure the image file exists.



That's all for today.
Now send me a reply and let me know what you think. I would love to hear from you.

Until next time,
Alex



Share the knowledge

Did you like this email? Share it with your friends!
Click here to share it



Product Offers (links you should visit)

PHP courses

How do you learn PHP fast, even if you have no experience?
Here's how to go from zero to PHP developer in just 9 days.
Take a look if you are a PHP beginner.



How do you make your PHP apps secure from attacks?
Learn how to secure your code by using the right defense techniques (and stop worrying).
See how to make your code secure.



Resources

Alex Web Develop - My tutorials.
Alex PHP café
- My Facebook group where to talk with me and other developers.

Photographer vector created by storyset - www.freepik.com




You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign