php大圖產生小圖代碼(縮圖程式)

來源:互聯網
上載者:User
 代碼如下 複製代碼

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.111cn.net' . $stype . '的縮圖。');
   break;
 }
 imagedestroy($desf);
 imagedestroy($srcf);

}

//此縮圖調用方法

 代碼如下 複製代碼

bigtosmallimg($file,$path,$w=120,$h=90);
/*

$file = 圖片的路徑
$path = 產生後儲存的路徑
$w =圖片寬度
$h =圖片高度
*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.