TP5使用 think\Image 图片处理类(压缩、加水印)

composer 安装:
composer require topthink/think-image

使用
` public function upload_handle() {
$config = [
‘size’ => 20971520,
‘ext’ => ‘jpg,png,jpeg’
];

    $file = $this->request->file('file');
    $imagesize = getimagesize($file->getInfo()['tmp_name']);
    $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
    $save_path = '/uploads/';
    $info = $file->validate($config)->move($upload_path);  //保存原图

    if ($info) {
        $url = str_replace('\\', '/', $save_path . $info->getSaveName());
        $image = Image::open(str_replace('\/', '/',ROOT_PATH.'public'.$url));
        $date_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads').'/'.date('Ymd');
        if(!file_exists($date_path)){
            mkdir($date_path,0777,true);
        }
        $thumb_path = $date_path.'/'.$info->getFilename();
        $path_parts = pathinfo($thumb_path);
        $path = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];  //缩略图文件名
        $file_path = $result = substr($path,strripos($path,"public")+6);
        $water_path = str_replace('\/', '/',ROOT_PATH).'public/static/images/water.png';
        //->thumb()等比例缩放
        //->water()加水印
        $image->thumb(1200, 750)->water($water_path,5)->water($water_path)->save($path);   //大缩略图(加两个不同位置的水印)
        $path = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb_m.'.$path_parts['extension'];  //缩略图文件名
        $file_path = $result = substr($path,strripos($path,"public")+6);
        $image->thumb(715, 500)->save($path);   //小缩略图
        $result = [
            'error' => 0,
            'url' => $file_path,
            'size'=>$imagesize[0].'*'.$imagesize[1],
        ];
    } else {
        $result = [
            'error' => 1,
            'message' => $file->getError()
        ];
    }

    return json($result);
}`
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值