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

How To Convert JPG, PNG, GIF, and WebP in PHP With Practical Examples

Used before category names. Blog 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). 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 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.

Convert Image Format

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.

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. The WebP format is a text-based image format that has low size than jpg, png, or gif formats.

In this section, we use the imagewebp function to convert the JPG 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 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 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 it to WebP using the imagewebp function.

Convert PNG to WebP Image Format

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. But the problem that PNG has compared to JPG is the size of the image. PNG image size is slightly larger than JPG.

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 function has three arguments

  • The source_file parameter is the path to the PNG image we want to convert. 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.

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 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 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. 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);

PHP Convert WebP to PNG

Above we learned how to convert PNG format to WebP, 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 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 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);

Convert WebP 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 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 with PHP

As we said, PNG format supports transparent backgrounds for images, but this is not possible in JPG format. To convert 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 path to the PNG image we want to convert. 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);

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 that can be placed directly inside the website codes and 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.

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

Tags: