php压缩图片
/**
* @param string $src 源文件地址
* @param int $percent 图片质量
* 图片压缩
*/
function ImgCompress($src,$percent=1){
//配置内存大小
ini_set('memory_limit', '256M');
//typearr存的是一些图片的格式,用作下文getimagesize()获取图片信息中的图片格式进行比对
$typearr = array("gif", "jpeg", "png", "swf", "psd", "bmp",'17'=>'webp');
list($width, $height, $type, $attr) = getimagesize($src); //获取图片信息
/**-------------------------------------------------------------------------------*/
//对imagecreatefrom 系列函数进行拼接从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串
$func = "imagecreatefrom" . $typearr[$type - 1];
$image = $func($src);
/**-----------------------------------------------------------------------------------*/
//创建一个新的真彩色图像
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_thump = imagecreatetruecolor($new_width, $new_height);
/**------------------------------------------------------------------------------*/
//图像处理
imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
/**-------------------------------------------------------------------------------------------*/
//拼接一个image系列函数(将图片输出到浏览器或文件)
$imgfunc= "image" . $typearr[$type - 1];
$imgfunc($image_thump,$src); //原图覆盖
//销毁图像资源
imagedestroy($image);
//销毁给定的变量
unset($image);
}
优化透明图片的处理
/**
* @param string $src 源文件地址
* @param int $percent 图片质量
* GD库 图片压缩
*/
function ImgCompress($src,$percent=1){
//配置内存大小
ini_set('memory_limit', '256M');
//typearr存的是一些图片的格式,用作下文getimagesize()获取图片信息中的图片格式进行比对
$typearr = array("gif", "jpeg", "png", "swf", "psd", "bmp",'17'=>'webp');
list($width, $height, $type, $attr) = getimagesize($src); //获取图片信息
/**-----------------------------------------------------------------------------------------------------*/
//对imagecreatefrom 系列函数进行拼接从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字符串
$func = "imagecreatefrom" . $typearr[$type - 1];
$image = @$func($src);
/**-----------------------------------------------------------------------------------------------------*/
//创建一个新的真彩色图像
$new_width = intval($width * $percent);
$new_height = intval($height * $percent);
$image_thump = imagecreatetruecolor($new_width, $new_height);
/**-----------------------------------------------------------------------------------------------------*/
// 处理透明背景图片变成黑色的问题
if(strtolower($typearr[$type - 1])=='png'){
//imageantialias($image_thump, true);
$color = imagecolorallocatealpha($image_thump, 0, 0, 0, 127);
imagecolortransparent($image_thump, $color);
imagefill($image_thump, 0, 0, $color);
imagesavealpha($image_thump,true);
}
/**----------------------------------------------------------------------------------------------------*/
//图像处理
imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
/**--------------------------------------------------------------------------------------------------------------------*/
//拼接一个image系列函数(将图片输出到浏览器或文件)
$imgfunc= "image" . $typearr[$type - 1];
//原图覆盖
$imgfunc($image_thump,$src);
//销毁图像资源
imagedestroy($image);
//销毁给定的变量
unset($image);
}
发表评论