Use PHP to Convert Png to Jpg With Compression

Use PHP to convert PNG to JPG with compression?

Do this to convert safely a PNG to JPG with the transparency in white.

$image = imagecreatefrompng($filePath);
$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);
$quality = 50; // 0 = worst / smaller file, 100 = better / bigger file
imagejpeg($bg, $filePath . ".jpg", $quality);
imagedestroy($bg);

Converting PNG to JPEG file

<?php
$image = imagecreatefrompng('yourlocation/image.png');
imagejpeg($image, 'yournewlocation/image.jpg', 70);
imagedestroy($image);
?>

Use PHP to convert PNG to JPEG

You can use something like this :

function pngTojpg($pngImage, $outputPngFile, $outputJpgFile, $quality) {
$image = imagecreatefrompng($pngImage);

//Save the png image
imagepng($image, $outputPngFile);

//Save the jpeg image
imagejpeg($image, $outputJpgFile, $quality);

// Free up memory
imagedestroy($image);
}

"quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75)"

The php doc : imagejpeg, imagecreatefrompng

These functions are from the GD library, here the installation instruction : Php GD

Use PHP to convert PNG data into JPG data

You can use file_get_contents:

$img = imagecreatefromstring(file_get_contents($thumb_url));
if ($img !== false)
imagejpeg($img, "/path/to/save/file.jpg");

convert png to jpg and download jpg image

A simple google let me get this question on stackoverflow, a simple function is given to convert images.

For the correct jpg headers I found this answer.

Combining the two should do the job

How can I convert all images to jpg?

Try this code: originalImage is the path of... the original image... outputImage is self explaining enough. Quality is a number from 0 to 100 setting the output jpg quality (0 - worst, 100 - best)

function convertImage($originalImage, $outputImage, $quality)
{
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];

if (preg_match('/jpg|jpeg/i',$ext))
$imageTmp=imagecreatefromjpeg($originalImage);
else if (preg_match('/png/i',$ext))
$imageTmp=imagecreatefrompng($originalImage);
else if (preg_match('/gif/i',$ext))
$imageTmp=imagecreatefromgif($originalImage);
else if (preg_match('/bmp/i',$ext))
$imageTmp=imagecreatefrombmp($originalImage);
else
return 0;

// quality is a value from 0 (worst) to 100 (best)
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);

return 1;
}

Convert JPG/GIF image to PNG in PHP?

You just need imagepng() then. In fact it almost becomes a one-liner:

 imagepng(imagecreatefromstring(file_get_contents($filename)), "output.png");

You would use $_FILES["id"]["tmp_name"] for the filename, and a different output filename obviously. But the image format probing itself would become redundant.

Convert PNG to JPEG, Using Compression Quality, Why Inverted colors?

Try and write newBufferedImage and not bufferedImageFile on "Code part 2".

So change this line to be:

writer.write(null, new IIOImage(newBufferedImage, null, null), jpegParams);

If you try and write a PNG image in JPEG format your going to end up with weird results.



Related Topics



Leave a reply



Submit