PHP圖片裁剪函數(映像不變形)

來源:互聯網
上載者:User
  1. *exif_imagetype -- 判斷一個映像的類型
  2. *說明:函數功能是把一個映像裁剪為任意大小的映像,映像不變形
  3. * 參數說明:輸入 需要處理圖片的 檔案名稱,產生新圖片的儲存檔案名稱,產生新圖片的寬,產生新圖片的高
  4. */
  5. // 獲得任意大小映像,不足地方展開,不產生變形,不留下空白
  6. function my_image_resize($src_file, $dst_file , $new_width , $new_height) {
  7. $new_width= intval($new_width);
  8. $new_height=intval($new_width);
  9. if($new_width <1 || $new_height <1) {
  10. echo "params width or height error !";
  11. exit();
  12. }
  13. if(!file_exists($src_file)) {
  14. echo $src_file . " is not exists !";
  15. exit();
  16. }
  17. // 映像類型
  18. $type=exif_imagetype($src_file);
  19. $support_type=array(IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_GIF);
  20. if(!in_array($type, $support_type,true)) {
  21. echo "this type of image does not support! only support jpg , gif or png";
  22. exit();
  23. }
  24. //Load image
  25. switch($type) {
  26. case IMAGETYPE_JPEG :
  27. $src_img=imagecreatefromjpeg($src_file);
  28. break;
  29. case IMAGETYPE_PNG :
  30. $src_img=imagecreatefrompng($src_file);
  31. break;
  32. case IMAGETYPE_GIF :
  33. $src_img=imagecreatefromgif($src_file);
  34. break;
  35. default:
  36. echo "Load image error!";
  37. exit();
  38. }
  39. $w=imagesx($src_img);
  40. $h=imagesy($src_img);
  41. $ratio_w=1.0 * $new_width / $w;
  42. $ratio_h=1.0 * $new_height / $h;
  43. $ratio=1.0;
  44. // 產生的映像的高寬比原來的都小,或都大 ,原則是 取大比例放大,取大比例縮小(縮小的比例就比較小了)
  45. if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
  46. if($ratio_w < $ratio_h) {
  47. $ratio = $ratio_h ; // 情況一,寬度的比例比高度方向的小,按照高度的比例標準來裁剪或放大
  48. }else {
  49. $ratio = $ratio_w ;
  50. }
  51. // 定義一個中間的臨時映像,該映像的寬高比 正好滿足目標要求
  52. $inter_w=(int)($new_width / $ratio);
  53. $inter_h=(int) ($new_height / $ratio);
  54. $inter_img=imagecreatetruecolor($inter_w , $inter_h);
  55. //var_dump($inter_img);
  56. imagecopy($inter_img, $src_img, 0,0,0,0,$inter_w,$inter_h);
  57. // 產生一個以最大邊長度為大小的是靶心圖表像$ratio比例的臨時映像
  58. // 定義一個新的映像
  59. $new_img=imagecreatetruecolor($new_width,$new_height);
  60. //var_dump($new_img);exit();
  61. imagecopyresampled($new_img,$inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h);
  62. switch($type) {
  63. case IMAGETYPE_JPEG :
  64. imagejpeg($new_img, $dst_file,100); // 儲存映像
  65. break;
  66. case IMAGETYPE_PNG :
  67. imagepng($new_img,$dst_file,100);
  68. break;
  69. case IMAGETYPE_GIF :
  70. imagegif($new_img,$dst_file,100);
  71. break;
  72. default:
  73. break;
  74. }
  75. } // end if 1
  76. // 2 靶心圖表像 的一個邊大於原圖,一個邊小於原圖 ,先放大平普映像,然後裁剪
  77. // =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
  78. else{
  79. $ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //取比例大的那個值
  80. // 定義一個中間的大映像,該映像的高或寬和靶心圖表像相等,然後對原圖放大
  81. $inter_w=(int)($w * $ratio);
  82. $inter_h=(int) ($h * $ratio);
  83. $inter_img=imagecreatetruecolor($inter_w , $inter_h);
  84. //將原圖縮放比例後裁剪
  85. imagecopyresampled($inter_img,$src_img,0,0,0,0,$inter_w,$inter_h,$w,$h);
  86. // 定義一個新的映像
  87. $new_img=imagecreatetruecolor($new_width,$new_height);
  88. imagecopy($new_img, $inter_img, 0,0,0,0,$new_width,$new_height);
  89. switch($type) {
  90. case IMAGETYPE_JPEG :
  91. imagejpeg($new_img, $dst_file,100); // 儲存映像
  92. break;
  93. case IMAGETYPE_PNG :
  94. imagepng($new_img,$dst_file,100);
  95. break;
  96. case IMAGETYPE_GIF :
  97. imagegif($new_img,$dst_file,100);
  98. break;
  99. default:
  100. break;
  101. }
  102. }// if3
  103. }// end function
  104. my_image_resize('test.gif','11111.gif','100px','100px');
  105. ?>
複製代碼

小編總結:以上實現的php映像裁剪函數,靈活應用了php gd庫的相關函數,包括 imagecreatefromjpeg、imagecreatefrompng、imagecreatefromgif、imagecreatetruecolor等函數的用法。因此,靈活掌握php gd庫中各函數的用法,對於裁剪圖片、浮水印、縮圖等功能的實現,至關重要。

  • 聯繫我們

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