将PNG文件转换为JPG文件
@param $pngFilePath string PNG文件路径
@param $jpgFilePath string JPG文件路径

@param $quality int JPG质量,0-100,值越低,压缩率越高
@return void
@throws Exception
/
function convertPngToJpg($pngFilePath, $jpgFilePath, $quality = 80)
{
// 检讨文件是否存在
if (!file_exists($pngFilePath)) {
throw new Exception("png文件不存在.");
}
// 创建一个新的 PNG 图像资源
$pngImage = imagecreatefrompng($pngFilePath);
if ($pngImage === false) {
throw new Exception("无法创建png资源.");
}
// 创建一个新的真彩色图像(无透明度)
$width = imagesx($pngImage);
$height = imagesy($pngImage);
$jpgImage = imagecreatetruecolor($width, $height);
// 将 PNG 图像复制到真彩色图像上
imagecopy($jpgImage, $pngImage, 0, 0, 0, 0, $width, $height);
// 将图像保存为 JPG 文件
if (!imagejpeg($jpgImage, $jpgFilePath, $quality)) {
throw new Exception("保存jpg文件失落败.");
}
// 销毁图像资源
imagedestroy($pngImage);
imagedestroy($jpgImage);
}