1. 程式人生 > 其它 >tp批量壓縮圖片

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);

    }







}