php等比縮放圖片的函數

來源:互聯網
上載者:User
  1. function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
  2. {
  3. $pic_width = imagesx($im);
  4. $pic_height = imagesy($im);
  5. if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
  6. {
  7. if($maxwidth && $pic_width>$maxwidth)
  8. {
  9. $widthratio = $maxwidth/$pic_width;
  10. $resizewidth_tag = true;
  11. }
  12. if($maxheight && $pic_height>$maxheight)
  13. {
  14. $heightratio = $maxheight/$pic_height;
  15. $resizeheight_tag = true;
  16. }
  17. if($resizewidth_tag && $resizeheight_tag)
  18. {
  19. if($widthratio<$heightratio)
  20. $ratio = $widthratio;
  21. else
  22. $ratio = $heightratio;
  23. }
  24. if($resizewidth_tag && !$resizeheight_tag)
  25. $ratio = $widthratio;
  26. if($resizeheight_tag && !$resizewidth_tag)
  27. $ratio = $heightratio;
  28. $newwidth = $pic_width * $ratio;
  29. $newheight = $pic_height * $ratio;
  30. if(function_exists("imagecopyresampled"))
  31. {
  32. $newim = imagecreatetruecolor($newwidth,$newheight);
  33. imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  34. }
  35. else
  36. {
  37. $newim = imagecreate($newwidth,$newheight);
  38. imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
  39. }
  40. $name = $name.$filetype;
  41. imagejpeg($newim,$name);
  42. imagedestroy($newim);
  43. }
  44. else
  45. {
  46. $name = $name.$filetype;
  47. imagejpeg($im,$name);
  48. }
  49. }
複製代碼

參數說明:$im 圖片對象,應用函數之前,你需要用imagecreatefromjpeg()讀取圖片對象,如果PHP環境支援PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();$maxwidth 定義產生圖片的最大寬度(單位:像素)$maxheight 產生圖片的最大高度(單位:像素)$name 產生的圖片名$filetype 最終產生的圖片類型(.jpg/.png/.gif)

代碼注釋:第3~4行:讀取需要縮放的圖片實際寬高第8~26行:通過計算實際圖片寬高與需要產生圖片的寬高的壓縮比例最終得出進行圖片縮放是根據寬度還是高度進行縮放,當前程式是根據寬度進行圖片縮放。如果你想根據高度進行圖片縮放,你可以將第22行的語句改成$widthratio>$heightratio第28~31行:如果實際圖片的長度或者寬度小於規定產生圖片的長度或者寬度,則要麼根據長度進行圖片縮放,要麼根據寬度進行圖片縮放。第33~34行:計算最終縮放產生的圖片長寬。第36~45行:根據計算出的最終產生圖片的長寬改變圖片大小,有兩種改變圖片大小的方法:ImageCopyResized()函數在所有GD版本中有效,但其縮放映像的演算法比較粗糙。ImageCopyResamples(),其像素插值演算法得到的映像邊緣比較平滑,但該函數的速度比ImageCopyResized()慢。第47~49行:最終產生經過處理後的圖片,如果你需要產生GIF或PNG,你需要將imagejpeg()函數改成imagegif()或imagepng()第51~56行:如果實際圖片的長寬小於規定產生的圖片長寬,則保持圖片原樣,同理,如果你需要產生GIF或PNG,你需要將imagejpeg()函數改成imagegif()或imagepng()。

GD庫1.6.2版以前支援GIF格式,但因GIF格式使用LZW演算法牽涉專利權,因此在GD1.6.2版之後不支援GIF的格式。如果你是WINDOWS的環境,你只要進入PHP.INI檔案找到extension=php_gd2.dll,將#去除,重啟APACHE即可,如果你是Linux環境,又想支援GIF,PNG,JPEG,你需要去下載libpng,zlib,以及freetype字型並安裝。

  • 聯繫我們

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