![]() ![]() The source_file parameter is the path to the GIF image we want to convert.$image = imagecreatefromgif($source_file) function hs_gif2webp($source_file, $destination_file, $compression_quality = 100) The following function is used to convert GIF format to WebP. GIF image is mostly used to create simple animations. It is possible to convert large images to GIFs, but it will not be suitable for the website. ![]() The GIF format image is a small animated image like a spinner for page loading. In this example, the function converts the PNG format file to WebP format. The destination_file parameter is the path of the image that we want to convert from PNG format to WebP, which must have the extension WebP.įunction usage: echo hs_png2webp('img/a.png','img/b.webp',100).The source_file parameter is the path to the PNG image we want to convert.$image = imagecreatefrompng($source_file) function hs_png2webp($source_file, $destination_file, $compression_quality = 100) PNG image size is slightly larger than JPG. But the problem that PNG has compared to JPG is the size of the image. For example, the logo of the website is one of the images used in PNG. JPG format does not support transparent backgrounds, so wherever we want the background of our image to be transparent, we use PNG format. PNG image format, like JPG, is one of the common formats to use on the website. Then we convert it to WebP using the imagewebp function. Using this function, we first read the image file in JPG format using the path we get from the parameter. This number must be between 0 and 100, which determines the quality of the final image.įunction usage: echo hs_jpg2webp('img/a.jpg','img/b.webp',100) The compression_quality argument is the quality of the final image.The destination_file parameter is the path of the image that we have converted from JPG format to WebP, which must be with the extension WebP.The source_file parameter is the path of the image that we want to convert to WebP and it must be in jpg format.$result = imagewebp($image, $destination_file, $compression_quality) $image = imagecreatefromjpeg($source_file) function hs_jpg2webp($source_file, $destination_file, $compression_quality = 100) You can use the following code to convert a JPG file to WebP in PHP. This function outputs the file in the form of WebP, which we can view in the browser or save in the file. In this section, we use the imagewebp function to convert the JPG file format to WebP. The WebP format is a text-based image format that has low size than jpg, png, or gif formats. The WebP image format is a new image format known as the next-gen image format. In this tutorial, we will introduce image formats and explain how to convert them to each other in PHP along with practical examples. Some are of low size, but their quality is relatively low. For example, some are of good quality, but their size is not suitable for placing on the website because it increases the page size and the loading speed of the website decreases. There are images in different formats, each of which has its advantages and disadvantages. There are some websites for converting formats to each other, but if you want to get behind the scenes of these conversions in PHP, or for example, if you want to develop a custom image conversion plugin for WordPress, then stay with us. So we have to change it to other supported formats. Sometimes not all browsers can support image formats such as WebP image format. In this tutorial, we are going to convert image formats with PHP (converting JPG to WebP, PNG to WebP, GIF to WebP, WebP to JPG, WebP to PNG, WebP to GIF, and image to base64 in PHP). Converting images to one another is one of the challenges of programmers and even web users such as webmasters. ![]()
0 Comments
Leave a Reply. |