-
-
-
-
- 圖片批次縮放
-
-
-
- class tool_slt_resize{
- public $width=null;
- public $height=null;
- public $MMsg=null;
- publicsgension_🎜> public $MMsg=null;
- 'jpg','gif','jpeg','png','bmp');
- public function __construct(){
- header('Content-type: text/html; charset=UTF-8' );
- }
-
- /**
- * 讀取一個資料夾下所有的檔案
- * @param $src 資料夾路徑
- * @return bool
- */
- public function getAllFile($src,$new){
- set_time_limit(0); //php腳本運行時間無限時
- ob_end_clean(); //關鍵1
- echo str_pad('',1024); //關鍵2
-
- $handle=opendir($src); //開啟目錄句柄
- while(($file=readdir($handle)) !== false){ //循環遍歷目錄下的所有檔案
- if($file != '.' && $file != '..' ) { //如果不是目前目錄或上層目錄
- $fullPath = $src.'/'.$file; //得到目前檔案的全路徑
- $newPath = $new.'/'.$file; //新的檔案存放路徑
- $dir=dirname($newPath);
- if(!file_exists($dir)){
- mkdir($dir);
- }
- if( is_dir($fullPath)){ //判斷是否表示一個資料夾
- $this->getAllFile($fullPath,$newPath); //是資料夾的話再遞歸執行一次函數
- }else{ //開始影像處理
- $extentsion=$this->get_extension($fullPath);
- if(in_array($extentsion,$this->extension)){ //後綴是否合法
- // echo $newPath .'';
- $im = imagecreatefromjpeg($fullPath);
- $width = imagesx($im);
- $height = imagesy($im);
- $this- >resize_to($im,$width,$height,$this->width,$this->height,$fullPath,$newPath);
- $msg= $fullPath.'處理完成';
- echo $msg;
- flush(); //刷新輸出緩衝
- }
- }
- }
- }
- echo '全部處理完成';
- }
-
-
- /**
- * 取得一些影像的資訊
- * getimagesize回傳一個四個單元的陣列($width, $height, $type, $attr)
- * @param $src
- * @return array
- */
- public function getImgInfo($src){
- return getimagesize($src);
- }
-
- /
- * 取得檔案字尾
- * @param $file
- * @return mixed
- */
- function get_extension($file){
- $info = pathinfo($file);
- return strtolower($info['extension']);
- }
- //圖片縮放
- public function resize_to($image,$width,$height,$dst_width,$dst_height,$path,$dstpath){
- // set_time_limit(0);
- $resize_width = 00; 🎜> $resize_height = 0;
- if($dst_width && $width > $dst_width ){
- $resize_width = 1;
- $width_ratio = $dst_width/dst_wid ($dst_height && $height > $dst_height){
- $resize_height = 1;
- $height_ratio = $dst_height/$height;
- }
- if($resize_height&&$resize_height&wid /寬度優先
- if($width_ratio $scale_org[0] = $width * $width_ratio;
- $scale_org[1] = $height * $width_ratio;
- $scale_org[1] = $height * $width_ratio;
- $scale_org[1] = $height * $width_ratio;
- $scale_org[1] = $height * $width_ratio;
- $scale_org[1] = $height * $width_ratio; $scale_org[1] = $height * $width_ratio; $scale_org[1] = $height * $width_ratio; $scale_org[1] = $height * $width_ratio; } }
- // 高度優先
- else{
- $scale_org[0] = $width * $height_ratio;
- $scale_org[1] = $height * $height_ratio;
- }
- }
- elseif($resize_width){
- $scale_org[0] = $dst_width;
- $scale_org[1] = $dst_width*$height/$width;
- } $scale_org[0] = $dst_height*$width/$height;
- $scale_org[1] = $dst_height;
- }
- if(function_exists("imagecopyres $newim = imagecreatetruecolor($scale_org[0], $scale_org[1]);
- imagecopyresampled($newim, $image, 0, 0, 0, 0, $scale_org[0], $scale_org[1] , $width, $height);
- }else{
- $newim = imagecreate($scale_org[0], $scale_org[1]);
- imagecopyresized($newim, $image, 0, 0, 0, 0,$scale_org[0], $scale_org[1], $width, $height);
- }
- ImageJpeg ($newim,$dstpath);
- ImageDestroy ($newim);
- }
- }
-
- $a=new tool_slt_resize(); //接受表單資料開始處理圖片
- $oldPath=isset($_GET['oldPath'])?$_GET['oldPath']:'';
- $newPath=isset($_GET['newPath' ) ) ])?$_GET['newPath']:'';
- $width=isset($_GET['width'])?$_GET['width']:'';
- $height= isset ( $_GET['height'])?$_GET['height']:'';
- $a->width=$width;
- $a->height=$height;
- if ( isset($_GET['提交'])){
- if($oldPath==''||$newPath==''||$width==''){
- echo '請正確填寫表單';
- 退出;
- }else{
- $a->getAllFile($oldPath,$newPath);
-
- }
- }
- ?>
- ?>
- ?>
- ?>
- ?>
- ?>
- ?>
- ?>
- ?>
- ?>
- ?> h1 > 圖片縮放縮放
-
-
|