Home > php教程 > PHP源码 > body text

压缩JPG图片

PHP中文网
Release: 2016-05-25 17:05:16
Original
1092 people have browsed it

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);
}
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template