php產生圖片縮圖方法收集

來源:互聯網
上載者:User
php產生圖片縮圖方法函數代碼如下:

/*
*php產生圖片縮圖的函數
*www.scutephp.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.scutephp.com/php-function/1007.html
return array('status'=>0,'file'=>$file,'width'=>$thumbWidth,'height'=>$thumbHeight,'savePath'=>$savePath,'info'=>'縮圖片產生成功');

}

使用方法如下:

$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,不產生新圖片

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.