tp批量压缩图片

<?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 @ 2021-07-26 20:25  Abner3721  阅读(87)  评论(0编辑  收藏  举报