function convertPngToJpg($pngFilePath, $jpgFilePath, $quality = 80)
{
if (!file_exists($pngFilePath)) {
throw new Exception("png文件不存在.");
}
$pngImage = imagecreatefrompng($pngFilePath);
if ($pngImage === false) {
throw new Exception("无法创建png资源.");
}
$width = imagesx($pngImage);
$height = imagesy($pngImage);
$jpgImage = imagecreatetruecolor($width, $height);
imagecopy($jpgImage, $pngImage, 0, 0, 0, 0, $width, $height);
if (!imagejpeg($jpgImage, $jpgFilePath, $quality)) {
throw new Exception("保存jpg文件失败.");
}
imagedestroy($pngImage);
imagedestroy($jpgImage);
}
原文链接:https: