php压缩图片

admin 2023-10-14 451 阅读 0评论

/**
* @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);
}



喜欢就支持以下吧
点赞 0

发表评论

快捷回复: 表情:
aoman baiyan bishi bizui cahan ciya dabing daku deyi doge fadai fanu fendou ganga guzhang haixiu hanxiao zuohengheng zhuakuang zhouma zhemo zhayanjian zaijian yun youhengheng yiwen yinxian xu xieyanxiao xiaoku xiaojiujie xia wunai wozuimei weixiao weiqu tuosai tu touxiao tiaopi shui se saorao qiudale qinqin qiaoda piezui penxue nanguo liulei liuhan lenghan leiben kun kuaikule ku koubi kelian keai jingya jingxi jingkong jie huaixiao haqian aini OK qiang quantou shengli woshou gouyin baoquan aixin bangbangtang xiaoyanger xigua hexie pijiu lanqiu juhua hecai haobang caidao baojin chi dan kulou shuai shouqiang yangtuo youling
提交
评论列表 (有 0 条评论, 451人围观)

最近发表

热门文章

最新留言

热门推荐

标签列表