這是一款利用php自帶的功能把指定的大圖生成我們指定大小的縮略圖代碼,使用方便簡單,只要把設置下面四個參數就可以生成自己想的大小的縮略圖了,代碼如下:
- function bigtosmallimg($file,$path,$w=120,$h=90)
- {
- $img=$path.$file;
- $imgarr=getimagesize($img);
- $sw=$imgarr[0];//原圖寬
- $sh=$imgarr[1];//原圖高
- $stype=$imgarr[2];
- //按比例縮放
- if($sw/$sh>$w/$h){
- $mw=$w;
- $mh=(int)$sh*($w/$sw);
- }
- else{
- $mw=(int)$sw*($h/$sh);
- $mh=$h;
- }
- switch($stype){//根據上傳好的圖形文件類型新建一個用來生成縮略圖的源文件。
- case 1:
- $srcf = imagecreatefromgif($img);
- break;
- case 2:
- $srcf = imagecreatefromjpeg($img);
- break;
- case 3:
- $srcf = imagecreatefrompng($img);
- break;
- default:
- showmsg('程序調用錯誤。');
- break;
- }
- $desf =imagecreatetruecolor($mw,$mh);
- imagecopyresampled($desf,$srcf,0,0,0,0,$mw,$mh,$sw,$sh);
- $sm_name=$path."s_".$file;
- switch($stype){
- case 1:
- imagegif($desf,$sm_name);
- break;
- case 2:
- imagejpeg($desf,$sm_name);
- break;
- case 3:
- imagepng($desf,$sm_name);
- break;
- default:
- showmsg('無法生成www.49028c.com' . $stype . '的縮略圖。');
- break;
- }
- imagedestroy($desf);
- imagedestroy($srcf);
- }
- //開源代碼Vevb.com
- //此縮略圖調用方法,代碼如下:
- bigtosmallimg($file,$path,$w=120,$h=90);
- /*
- $file = 圖片的路徑
- $path = 生成后保存的路徑
- $w =圖片寬度
- $h =圖片高度
- */
新聞熱點
疑難解答