压缩JPG图片
程序员文章站
2022-04-18 18:22:16
...
php代码
/** * 将图片以自定义品质,另存为JPG格式,将会删除源图片 * * @param string $filename 图片名称,包含路径 * @param int $quality 图片品质,0到100,默认90,100为最高品质 */ public function resaveToJpeg($filename, $quality = 90) { $path = dirname($filename); $path = rtrim($path, '/').'/'; $basename = pathinfo($filename, PATHINFO_FILENAME); $extName = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); switch($extName) { case 'jpg': $im = imagecreatefromjpeg($filename); break; case 'png': $im = imagecreatefrompng($filename); break; case 'gif': $im = imagecreatefromgif($filename); break; } imagejpeg($im, $path.$basename.'.jpg', $quality); if(in_array($extName, array('png','gif'))) { @unlink($filename); } imagedestroy($im); }