Skip to content

Instantly share code, notes, and snippets.

@rintoug
Created March 27, 2019 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rintoug/b72a5aa57515d21438e776d088ba7c2e to your computer and use it in GitHub Desktop.
Save rintoug/b72a5aa57515d21438e776d088ba7c2e to your computer and use it in GitHub Desktop.
How to Convert PNG to JPG with compression in PHP?
<?php
$filePath = dirname(__FILE__).'/hq.png';
$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 = low / smaller file, 100 = better / bigger file
imagejpeg($bg, $filePath . ".jpg", $quality);
imagedestroy($bg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment