Blog, PHP, PHP Development

Convert JPG, PNG, GIF and, WebP in PHP

In this tutorial, we are going to convert image format to each other with 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

Convert JPG to WebP Image Format

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

Below 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 that in 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 that in 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 that in here must be in gif.

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_gif2webp('img/a.gif','img/b.webp',100);

Convert WebP to JPG

Webp is next gen image format that created by Google, but is has own disadvantages. For example Safari browser doesn’t support this format in 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 that in 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);

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 that in 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

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 that in 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 colors count argument to define the number of usage colors.

Function usage:

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