tp批量压缩图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace app\index\controller;
 
use think\Controller;
 
class Index extends Controller
{
    public function index()
    {
        return '开发中。。';
    }
 
 
    public function test(){
 
        if($this->request->isPost()){
 
            $file = request()->file('file');
            if(empty($file)) return showError(config('status.badRequest'),'上传文件不能为空');
            $data['file'] = $file;
            $info = $file->validate(['ext'=>'bmp,jpg,JPG,PNG,GIF,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $filename = $info->getSaveName();
                $filename = str_replace('\\','/',$filename);
                $filename = '/uploads/'.$filename;
                $image = \think\Image::open( './'.$filename);
 
                $filenameInfo = explode('.',$filename);
                $thumbImg = $filenameInfo[0].'_thumb_.'.$filenameInfo[1];
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                $image->thumb(600,600,\think\Image::THUMB_SCALING)->save( './'.$thumbImg);
                //保存照片exif信息
                $exif = exif_read_data('./'.$filename,0, true);
                halt($exif);
            }
 
        }
 
 
        return $this->fetch('');
    }
 
 
 
    public function dirFile(){
        $dir = "./uploads"//要获取的目录
        ini_set('max_execution_time', '0');
        $this->scan_all($dir);
 
    }
 
 
    function scan_all($dir){
        $temp = scandir($dir);
        if(is_array($temp) && count($temp)>2){
            array_shift($temp);
            array_shift($temp);
            foreach($temp as $v){
                $cur_dir=$dir.DIRECTORY_SEPARATOR.$v;
                if($v == 'ueditor') {
                    continue;
                }
                if(is_dir($cur_dir)){
 
                    echo "<blockquote>";
                    $this->scan_all($cur_dir);
                    echo "</blockquote>";
                }else if(is_file($cur_dir)){
                      $filename = $dir.'/'.$v ;
                      $filename = str_replace('\\', '/', $filename);
                      if(strstr($filename, 'thumb') !== false ){
                          continue;
                      }
                    echo "<blockquote>";
                    echo $filename;
                    $this->yasuo($filename);
                    echo "</blockquote>";
 
 
 
                }else{
                    echo 'err';
                }
            }
        }
    }
 
    public function yasuo($filename){
 
        $image = \think\Image::open($filename);
        $filename = ltrim($filename, '.');
        $filenameInfo = explode('.',$filename);
        $thumbImg = $filenameInfo[0].'_thumb_.'.$filenameInfo[1];
        $image->thumb(600,600,\think\Image::THUMB_SCALING)->save( './'.$thumbImg,$type = null, $quality = 100);
 
    }
 
 
 
 
 
 
 
}

  

posted @   Abner3721  阅读(90)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示