具體用法如下:
$a=thumbImage('1.jpg',200,100,'2.jpg');//將1.jpg生成寬為200,高為100的縮略圖,並保存為2.jpg print_r($a);
打印結果:
Array(
[status] => 0
[file] => 1.jpg
[width] => 200
[height] => 200
[savePath] => 2.jpg
[info] => 縮略圖片生成成功
)
$a=thumbImage('1.jpg',200,100);//將1.jpg生成寬為200,高為100的縮略圖,並直接覆蓋1.jpg,不生成新圖片
函數代碼如下:
<?php /* *php生成圖片縮略圖的函數 *www.phpernote.com */ function thumbImage($file,$thumbWidth,$thumbHeight,$savePath='',$isCut=true,$quality=100){ $result=array('status'=>0,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>''); if(!file_exists($file)){ return array('status'=>1,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'圖片文件不存在'); } //檢測文件類型 $fp=fopen($file,'rb'); $str=fread($fp,2); //只讀2字節 fclose($fp); $str=@unpack("c2chars",$str); $typeCode=intval($str['chars1'].$str['chars2']); $fileType=''; switch($typeCode){ case 255216: $fileType='jpg'; break; case 7173: $fileType='gif'; break; case 6677: $fileType='bmp'; break; case 13780: $fileType='png'; break; default: $fileType=$typeCode; break; } if($str['chars1']=='-1'&&$str['chars2']=='-40'){ $fileType='jpg'; } if($str['chars1']=='-119'&&$str['chars2']=='80'){ $fileType='png'; } if(!in_array($fileType,array('jpg','gif','bmp','png'))){ return array('status'=>2,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'圖片文件類型不正確:'.$fileType); } //圖片縮放處理 if($fileType=='jpg'){ $im=imagecreatefromjpeg($file); } if($fileType=='gif'){ $im=imagecreatefromgif($file); } if($fileType=='png'){ $im=imagecreatefrompng($file); } if(empty($savePath)) $savePath=$file; $width=imagesx($im); $height=imagesy($im); if($width<$thumbWidth&&$height<$thumbHeight){ return array('status'=>3,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'圖片尺寸小於生成縮略圖的尺寸'); } $ratio=$width/$height;//實際圖象的比例 $thumbRatio=$thumbWidth/$thumbHeight;//改變後的圖象的比例 if($isCut){//裁切處理 if(function_exists('imagepng')&&(str_replace('.','',PHP_VERSION)>=512)){//針對php版本大於5.12參數變化後的處理情況 $quality=9; } if($ratio>=$thumbRatio){//高度優先 $newimg=imagecreatetruecolor($thumbWidth,$thumbHeight); imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,(($height)*$thumbRatio),$height); imagejpeg($newimg,$savePath,$quality); } if($ratio<$thumbRatio){//寬度優先 $newimg=imagecreatetruecolor($thumbWidth,$thumbHeight); imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,$width,(($width)/$thumbRatio)); imagejpeg($newimg,$savePath,$quality); } }else{//不裁切處理 if($ratio>=$thumbRatio){ $newimg=imagecreatetruecolor($thumbWidth,($thumbWidth)/$ratio); imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,($thumbWidth)/$ratio,$width,$height); imagejpeg($newimg,$savePath,$quality); } if($ratio<$thumbRatio){ $newimg=imagecreatetruecolor(($thumbHeight)*$ratio,$thumbHeight); imagecopyresampled($newimg,$im,0,0,0,0,($thumbHeight)*$ratio,$thumbHeight,$width,$height); imagejpeg($newimg,$savePath,$quality); } } ImageDestroy($im);//www.phpernote.com/php-function/1007.html return array('status'=>0,'file'=>$file,'width'=>$thumbWidth,'height'=>$thumbHeight,'savePath'=>$savePath,'info'=>'縮略圖片生成成功'); }