先拋出composer插件
composer require intervention/image
use Intervention\Image\ImageManagerStatic as Image;
public function youhua() { $hostdir=ROOT_PATH . 'public' . DS .'yh'; //要讀取的文件夾 $filesnames = scandir($hostdir); foreach ($filesnames as $name) { if($name=='.'||$name=='..'){ continue; }else{ $aurl= ROOT_PATH . 'public' . DS .'yh' . DS . $name; $img = Image::make($aurl);//打開的路徑 $img->resize(200, 266);//定制寬高 $img->save($aurl);//保存 echo $aurl . "<br/>"; //輸出 } } }
我這段代碼實現的是在原來的基礎上直接覆蓋,相當於無痕壓縮。
有輸出另外需求的可以改變save路徑