php convert JPG to WebP, PNG to WebP, GIF to WebP, WebP to JPG, WebP to PNG, and WebP to GIF

Convert JPG, PNG, GIF, WebP, and base64 image format in PHP

Converting images to one another is one of the challenges of programmers and even web users such as webmasters. 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) that you can create image format converter or even WordPress plugin. With this method, you can create converters such as JPG, PNG, and GIF to WebP and vice versa in PHP. Sometimes not all browsers can support image formats such as WebP image format. So we have to change it to other supported formats.

There are some websites to convert image 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 converter or custom image conversion plugin for WordPress, then stay with us.

PHP Convert Image Format

Images are essential elements of modern web design and development. They are used to enhance the visual appeal of a website, as well as to convey information. In order to ensure that images are displayed correctly across all platforms, it is important that they are saved in the correct image format. PHP is a powerful scripting language that can be used to convert an image from one format to another like WebP.

Image format conversion is the process of transforming an image from one file format to another. This is done for a variety of reasons, such as to reduce the file size of an image, to make an image compatible with a particular platform, or to improve the quality of an image.

Here is the functions list

Image formats

There are images in different formats, each of which has its advantages and disadvantages. 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. Some are of low size, but their quality is relatively low. Those are not good for SEO.

In this tutorial, we will introduce image formats and explain how to convert them to each other in PHP along with practical examples.

PHP Convert JPG to WebP Image Format

The WebP image format is a new image format known as the next-gen image format and was introduced by Google. The WebP format is a text-based image format that has a lower size than jpg, png, or gif formats.

In this section, we use the PHP imagewebp built-in function to convert the JPG image file format to WebP. This function outputs the file in the form of WebP, which we can view in the browser or save in the file.

You can use the following code to convert a JPG image file to WebP in PHP.

function hs_jpg2webp($source_file, $destination_file, $compression_quality = 100)
{
    $image = imagecreatefromjpeg($source_file);
    $result = imagewebp($image, $destination_file, $compression_quality);
    if (false === $result) {
        return false;
    }
    imagedestroy($image);
    return $destination_file;
}

This function takes three arguments.

  • The source_file parameter is the PHP path of the image that we want to convert to WebP and it must be in jpg format.
  • 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 compression_quality argument is the quality of the final image. This number must be between 0 and 100, which determines the quality of the final image.

Function usage:

echo hs_jpg2webp('img/a.jpg','img/b.webp',100);

Using this function, we first read the image file in JPG format using the path we get from the parameter. Then we convert the image to WebP using the PHP imagewebp function.

Convert PNG to WebP Image Format in PHP

PNG image format, like JPG, is one of the common formats to use on the website. JPG format does not support transparent backgrounds, so wherever we want the background of our image to be transparent, we use PNG format. For example, the logo of the website is one of the images used in PNG. However, the problem that PNG has compared to JPG is the size of the image. PNG image size is slightly larger than JPG.

With the below PHP function, you can convert PNG image format to WebP.

function hs_png2webp($source_file, $destination_file, $compression_quality = 100)
{
    $image = imagecreatefrompng($source_file);
    imagepalettetotruecolor($image);
    imagealphablending($image, true);
    imagesavealpha($image, true);
    $result = imagewebp($image, $destination_file, $compression_quality);
    if (false === $result) {
        return false;
    }
    imagedestroy($image);
    return $destination_file;
}

This PHP function has three arguments for converting PNG

  • The PHP source_file parameter is the path to the PNG image we want to convert to WebP. This file must be in PNG 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.
  • The compression_quality argument is the quality of the final image. This number must be between 0 and 100, which determines the quality of the final image.

Function usage:

echo hs_png2webp('img/a.png','img/b.webp',100);

In this example, the function converts the PNG format file to WebP format. With this PHP method, you can create a simple converter of the PNG image to WebP format.

Convert GIF to WebP Image Format

The GIF format image is a small animated image like a spinner for page loading. It is possible to convert large images to GIFs, but it will not be suitable for the website. GIF image is mostly used to create simple animations.

The following function is used to convert GIF format to WebP.

function hs_gif2webp($source_file, $destination_file, $compression_quality = 100)
{
    $image = imagecreatefromgif($source_file);
    imagepalettetotruecolor($image);
    $result = imagewebp($image, $destination_file, $compression_quality);
    if (false === $result) {
        return false;
    }
    imagedestroy($image);
    return $destination_file;
}

This function has three arguments

  • The source_file parameter is the path to the GIF image we want to convert. This file must be in GIF format.
  • The destination_file parameter is the path of the image that we want to convert from GIF format to WebP, which must have the extension WebP.
  • The compression_quality argument is the quality of the final image. This number must be between 0 and 100, which determines the quality of the final image.

Function usage:

echo hs_gif2webp('img/a.gif','img/b.webp',100);

This function converts the image file in GIF format to WebP in PHP.

Convert WebP to JPG image in PHP

Webp is a next-gen image format created by Google but it has its own disadvantages. For example, some browsers don’t support this format in older versions.

In the example above, we learned how to convert JPG to WebP image format in PHP. Now we want to convert WebP to JPG. For this, we use the following code.

function hs_webp2jpg($source_file, $destination_file, $compression_quality = 100)
{
    $image = imagecreatefromwebp($source_file);
    $result = imagejpeg($image, $destination_file, $compression_quality);
    if (false === $result) {
        return false;
    }
    imagedestroy($image);
    return $destination_file;
}

This function receives three arguments like the above function and converts the file in WebP format to JPG.

  • The source_file parameter is the path to the WebP image we want to convert by PHP. This file must be in WebP format.
  • The destination_file parameter is the path of the image that we want to convert the WebP format to JPG, which must be with the jpg extension.
  • The compression_quality argument is the quality of the final image. This number must be between 0 and 100, which determines the quality of the final image.

Function usage:

echo hs_webp2jpg('img/a.webp','img/b.jpg',100);

With this PHP method, you can create a simple converter of the WebP image to JPG format.

PHP Convert WebP to PNG image

Above we learned how to convert PNG format to WebP in PHP, Now we want to convert WebP image format to PNG using the following function.

function hs_webp2png($source_file, $destination_file, $compression_quality = 100)
{
    $image = imagecreatefromwebp($source_file);
    $result = imagepng($image, $destination_file, $compression_quality);
    if (false === $result) {
        return false;
    }
    imagedestroy($image);
    return $destination_file;
}

This function takes three arguments.

  • The source_file parameter is the PHP path to the WebP image that we want to convert to PNG. This file must be in WebP format.
  • The destination_file parameter is the PHP path of the image that we want to convert from WebP format to PNG, which must be with the PNG extension.
  • The compression_quality argument is the quality of the final image. This number must be between 0 and 9, which determines the quality of the final image.

Function usage:

echo hs_webp2png('img/a.webp','img/b.png',9);

With this PHP method, you can create a simple converter of the WebP image to PNG format.

Convert WebP image to GIF in PHP

As we said, the gif format is used for moving and animated images, but the image in WebP format does not support moving images. So when converting WebP to gif, the final image in gif format will not be animated. We use the following PHP function to convert the image.

function hs_webp2gif($source_file, $destination_file, $colors_count = 16)
{
    $image = imagecreatefromwebp($source_file);
    imagetruecolortopalette($image, false, $colors_count);
    $result = imagegif($image, $destination_file);
    if (false === $result) {
        return false;
    }
    imagedestroy($image);
    return $destination_file;
}

This function takes three arguments.

  • The source_file parameter is the path to the WebP image that we want to convert to PNG. This file must be in WebP format.
  • The destination_file parameter is the path of the image that we want to convert from WebP format to PNG, which must be with the PNG extension.
  • The colors_count argument is to specify the number of colors that should be used in the gif format.

In this function, the colors may be different from the original image because this function selects a color close to the original color in the image. Another thing is that the number of colors in the WebP format is unlimited, while the number of colors in the gif format is limited.

Function usage:

echo hs_webp2png('img/a.webp','img/b.gif',100);

Convert PNG to JPG image with PHP

As we said, PNG format supports transparent backgrounds for images, but this is not possible in JPG format. To PHP convert image PNG to JPG, the parts that are transparent are replaced with white color.

function hs_png2jpg($source, $destination, $quality = 100)
{
    $image = imagecreatefrompng($source);
    $bg = imagecreatetruecolor(imagesx($image), imagesy($image));
    imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
    imagealphablending($bg, true);
    imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
    imagedestroy($image);
    imagejpeg($bg, $destination, $quality);
    imagedestroy($bg);
}

In this function

  • The source_file parameter is the PHP path to the PNG image we want to convert to JPG. This file must be in PNG format.
  • The destination_file parameter is the path of the image that we want to convert the PNG format to JPG, which must be with the jpg extension.
  • The quality argument is the quality of the final image. This number must be between 0 and 100, which determines the quality of the final image.

In general, the size of images in JPG format is less than images in PNG format, but if we set the quality to 100, the size of the JPG file will be larger than that of PNG.

Function usage:

hs_png2jpg('img/a.png','img/b.jpg',100);

This a simple method to convert PNG format to JPG with PHP functions.

Convert JPG, GIF, PNG, and WebP images to base64

Converting images to base64 is used in many cases, such as placing the image as an icon, etc. One of the benefits of this work is that it eliminates the need to read the image from the hard disk. These images are actually text-based and can be placed directly inside the website codes to avoid linking to the image address.

Also, you may want to save the images in the database, in which case you can use both the blob for storage and base64 for saving the image as text.

In this method, there is no guarantee to reduce the size of the image, and its only advantage is to reduce references to memory and increase the loading speed. Although you can convert large images to base64, it is better to use base64 conversion only for small images such as icons.

$file = "./image.jpg";
$path = pathinfo($file);
$ext = mb_strtolower($path['extension']);
 
if (in_array($ext, array('jpeg', 'jpg', 'gif', 'png', 'webp'))) {     
    $size = getimagesize($file);  
    $img = 'data:' . $size['mime'] . ';base64,' . base64_encode(file_get_contents($file));
}
?>
 
<img src="<?php echo $img; ?>">

In this example, we have converted the image in JPG format to base64. The $img variable contains the base64 text of the image, which can be stored in the database.

To use base64 images, just put the existing text ($img variable value) in the src of the img tag in HTML.

Conclusion

You can easily create a tool to convert images to one another using the functions and codes above, or you can even design a plugin for WordPress and provide it to users or a professional image converter for yourself with these PHP methods.

In different articles, we provided tutorials about how to resize images, how to rotate images, and how to compress images in PHP.

Shopping Cart