php 產生圖片縮圖-大圖產生小圖片

來源:互聯網
上載者:User

/*
    函數:產生縮圖
  MakeBuild("images/a.jpg","news/b.jpg","100");
    參數:
    echo $BuildFile;   原圖 帶路徑
    echo $newFile;    產生的縮圖 帶路徑
    echo $File_width;   縮圖寬度值
    echo $File_height;   縮圖高度值 (預設為寬度的比例值)
    echo $rate;     縮圖象品質;
 */
 function MakeBuild($BuildFile,$newFile,$File_width,$File_height=0,$rate=1000) {
    if(!is_file($BuildFile)){
   $this->msg("檔案 ".$BuildFile." 不是一個有效圖形檔案! 系統無法產生該檔案的縮圖!");
   return false;
    }
    $data = GetImageSize($BuildFile);
    switch($data[2]){
  case 1:
   $im = @ImageCreateFromGIF($BuildFile);
   break;
  case 2:
   $im = @ImageCreateFromJPEG($BuildFile);
   break;
  case 3:
   $im = @ImageCreateFromPNG($BuildFile);
   break;
    }
    if(!$im){
   return false;
    }
    else{
   $srcW = ImageSX($im);  # 取得原圖寬度;
   $srcH = ImageSY($im); # 取得原圖高度;
   $dstX = 0;
   $dstY = 0;
   
  if($File_height==0){
   $File_height = $File_width/$srcW*$srcH;
  }
   
  if ($srcW*$File_height>$srcH*$File_width){
   $fFile_height = round($srcH*$File_width/$srcW);
   $dstY = floor(($File_height-$fFile_height)/2);
   $fFile_width = $File_width;
  }
  else {
   $fFile_width = round($srcW*$File_height/$srcH);
   $dstX = floor(($File_width-$fFile_width)/2);
   $fFile_height = $File_height;
  }
  $ni = ImageCreateTrueColor($File_width,$File_height);
  $dstX = ($dstX<0)?0:$dstX;
  $dstY = ($dstX<0)?0:$dstY;
  $dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX;
  $dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY;
  ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH);
   
  ImageJpeg($ni,$newFile,$rate); # 產生縮圖;
  imagedestroy($im);     # imagedestroy(resource) 釋放image關聯的記憶體
    }
 }

聯繫我們

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