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, and, WebP in PHP

In this tutorial, we are going to convert image formats to each other with PHP (convert JPG to WebP, PNG to WebP, GIF to WebP, WebP to JPG, WebP to PNG, and WebP to GIF in PHP). Sometimes all browsers can’t support image format like Safari doesn’t support WebP image format. Then we have to change it to other supported formats.

Convert Image Format

Here is the functions list

PHP Convert JPG to WebP Image Format

A WebP image format is a new image format known as the next-gen image format. WebP is text base image that has a lower size than jpg, png, or gif.

Below the code is the converter jpg to png.

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 is the path of the image we want to convert here must be in jpg.

The destination file is the path of the image we convert to that must be in Webp.

The compression quality argument is the quality of the final image. It must be between 0 to 100.

Function usage:

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

Convert PNG to WebP Image Format

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 takes three arguments. The source file is the path of the image we want to convert here must be in png.

The destination file is the path of the image we convert to that must be in Webp.

The compression quality argument is the quality of the final image. It must be between 0 to 100.

Function usage:

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

Convert GIF to WebP Image Format

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 takes three arguments. The source file is the path of the image we want to convert here must be in gif.

The destination file is the path of the image we convert to that must be on WebP.

The compression quality argument is the quality of the final image. It must be between 0 to 100.

Function usage:

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

Convert WebP to JPG in PHP

Webp is a next-gen image format created by Google but it has its own disadvantages. For example, the Safari browser doesn’t support this format in the older version.

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 takes three arguments. The source file is the path of the image we want to convert here must be in Webp.

The destination file is the path of the image we convert to that must be in jpg.

The compression quality argument is the quality of the final image. It must be between 0 to 100.

Function usage:

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

PHP Convert WebP to PNG

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;
}

In this function, compression quality is between 0 to 9.

This function takes three arguments. The source file is the path of the image we want to convert here must be in WebP.

The destination file is the path of the image we convert to that must be in png.

The compression quality argument is the quality of the final image. It must be between 0 to 9.

Function usage:

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

Convert WebP to GIF in PHP

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 is the path of the image we want to convert here must be in WebP.

The destination file is the path of the image we convert to that must be in gif.

In gif format, we add the colors count argument to define the number of usage colors.

Function usage:

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