Home  >  Article  >  php教程  >  压缩JPG图片

压缩JPG图片

PHP中文网
PHP中文网Original
2016-05-25 17:05:161090browse

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);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn