tp上传图片并给图片压缩

51 篇文章 2 订阅

首先下载一下扩展:

找到根目录文件夹输入cmd命令:(composer 安装)

composer require topthink/think-image

 

版本一

存在问题:会生成三张图片占用内存,下一个版本已经解决

html代码:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="{:url('index/test/testa')}" method="post" enctype="multipart/form-data">
        <input type="file" name="image" required="required"></div>
        <input type="submit" value="提交">
    </form>
</body>
</html>

控制代码:

<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Log;
use think\Db;

class Test extends Controller
{

    public function index(){
        return $this->fetch( 'index' );
    }

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

        if ($info) {
            $url = str_replace('\\', '/', $save_path . $info->getSaveName());
			// dump($url);exit;
            $image = \think\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);   //大缩略图(加两个不同位置的水印)
			$image->thumb(1200, 750)->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);
    }
}
?>

 

版本二

解决三张图片问题

html代码:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="{:url('index/test/testa')}" method="post" enctype="multipart/form-data">
        <input type="file" name="image" required="required"></div>
        <input type="submit" value="提交">
    </form>
</body>
</html>

控制器代码:

<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Log;
use think\Db;

class Test extends Controller
{

    public function index(){
        return $this->fetch( 'index' );
    }

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

        if ($info) {
            $url = str_replace('\\', '/', $save_path . $info->getSaveName());
			// dump($url);exit;
            $image = \think\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);
            $image->thumb(715, 500)->save($path);   //小缩略图
            $result = [
                'error' => 0,
                'url' => $file_path,
                'size'=>$imagesize[0].'*'.$imagesize[1],
            ];
        } else {
            $result = [
                'error' => 1,
                'message' => $file->getError()
            ];
        }
		unlink( ROOT_PATH . 'public' . DS . $url);//删除原本图片

        return json($result);
    }

}
?>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值