php压缩图片

在项目开发过程中少不了会用到图片上传功能,考虑到服务器带宽及磁盘空间,需要对图片进行压缩。

前端的压缩可以节约带宽,后端的压缩可以节省磁盘空间。

function resizeImage($source,$percent=0.5){
        if(empty($source)){
            return false;
        }
        //获取原图宽高,图片类型
        list($width, $height, $type) = getimagesize($source);
        $type = image_type_to_extension($type,false);
        $new_width = $width * $percent;
        $new_height = $height * $percent;
        //等比例创建画布
        $image_thump = imagecreatetruecolor($new_width,$new_height);
        $bg = imagecolorallocate($image_thump,255,255,255);
        imagefill($image_thump,0,0,$bg);
        $fun = "imagecreatefrom".$type;
        $img = $fun($source);
        ImageDestroy($source);
        //将原图复制到图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
        imagecopyresampled($image_thump,$img,0,0,0,0,$new_width,$new_height,$width,$height);
        $func = "image".$type;
        $func($image_thump,$source);
        //销毁内存
        ImageDestroy($image_thump);
        return true;
    }

另:小程序的图片上传接口自带压缩图片功能,在配置项中有,接口地址
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值