Convert JPG, PNG, GIF and, WebP in PHP

H Bahonar
2 min readSep 8, 2022

--

In this tutorial, we are going to convert image formats 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 JPG to WebP Image Format

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

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

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

Function usage:

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

Convert WebP to JPG

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

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.

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

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

More details and examples on Convert JPG, PNG, GIF and, WebP in PHP

--

--

H Bahonar

I am a Web Developer & Designer. Specialist in WordPress. CEO of Honar Systems