function creat_thumbnail($img,$w,$h,$path) { $org_info = getimagesize($img); //獲得映像大小且是通過post傳遞過來的 //var_dump($org_info); //Array ( [0] => 1024 [1] => 768 [2] => 3 [3] => width="1024" height="768" [bits] => 8 [mime] => image/png ) $orig_x = $org_info[0]; //映像寬度 $orig_y = $org_info[1]; //映像高度 $orig_type = $org_info[2]; //圖片類別即尾碼 1 = GIF,2 = JPG,3 = PNG, //是真彩色映像 if (function_exists("imagecreatetruecolor")) { switch($orig_type) { //從給定的gif檔案名稱中取得的映像 case 1 : $thumb_type = ".gif"; $_creatImage = "imagegif"; $_function = "imagecreatefromgif"; break; //從給定的jpeg,jpg檔案名稱中取得的映像 case 2 : $thumb_type = ".jpg"; $_creatImage = "imagejpeg"; $_function = "imagecreatefromjpeg"; break; //從給定的png檔案名稱中取得的映像 case 3 : $thumb_type = ".png"; $_creatImage = "imagepng"; $_function = "imagecreatefrompng"; break; } } //如果從給定的檔案名稱可取得的映像 if(function_exists($_function)) { $orig_image = $_function($img); //從給定的$img檔案名稱中取得的映像 } if (($orig_x / $orig_y) >= (4 / 3)) //如果寬/高 >= 4/3 { $y = round($orig_y / ($orig_x / $w)); //對浮點數進行四捨五入 $x = $w; } else //即 高/寬 >= 4/3 { $x = round($orig_x / ($orig_y / $h)); $y = $h; } $sm_image = imagecreatetruecolor($x, $y); //建立真彩色圖片 //重採樣拷貝部分映像並調整大小 Imagecopyresampled($sm_image, $orig_image, 0, 0, 0, 0, $x, $y, $orig_x, $orig_y); //imageJPEG($sm_image, '', 80); //在瀏覽器輸出映像 $tnpath = $path."/"."s_".date('YmdHis').$thumb_type; //縮圖的路徑 $thumbnail = @$_creatImage($sm_image, $tnpath, 80); //產生圖片,成功返回true(或1) imagedestroy ($sm_image); //銷毀映像 if($thumbnail==true) { return $tnpath; } } |