Created
March 27, 2019 04:36
-
-
Save rintoug/b72a5aa57515d21438e776d088ba7c2e to your computer and use it in GitHub Desktop.
How to Convert PNG to JPG with compression in PHP?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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