php縮圖代碼,imagecopyresampled函數產生縮圖

來源:互聯網
上載者:User
  1. function CreateImage($SrcImageUrl, $DirImageUrl, $Width, $Height)

  2. {
  3. $img;
  4. $srcw;
  5. $new_width;
  6. $srch;
  7. $new_height;
  8. // 圖片類型
  9. $type = substr(strrchr($SrcImageUrl, "."), 1);
  10. // 初始化映像
  11. if($type == "jpg")
  12. $img = imagecreatefromjpeg($SrcImageUrl);
  13. if($type == "gif")
  14. $img = imagecreatefromgif($SrcImageUrl);
  15. if($type == "png")
  16. $img = imagecreatefrompng($SrcImageUrl);

  17. $srcw = imagesx($img);

  18. $srch = imagesy($img);

  19. if ($srcw / $srch > $Width / $Height)

  20. {
  21. if ($srcw > $Width)
  22. {
  23. $new_width = $Width;
  24. $new_height = $srch * ($Width / $srcw);
  25. }
  26. else
  27. {
  28. $new_width = $srcw;
  29. $new_height = $srch;
  30. }
  31. }
  32. else
  33. {
  34. if ($srch > $Height)
  35. {
  36. $new_height = $Height;
  37. $new_width = $srcw * ($Height / $srch);
  38. }
  39. else
  40. {
  41. $new_width = $srcw;
  42. $new_height = $srch;
  43. }
  44. }

  45. $new_image = imagecreatetruecolor($new_width, $new_height);

  46. imagecopyresampled($new_image, $img, 0, 0, 0, 0, $new_width, $new_height, $srcw, $srch);
  47. imagejpeg($new_image, $DirImageUrl);

  48. imagedestroy($img);

  49. imagedestroy($new_image);
  50. }

複製代碼

3、改進後代碼:

  1. /*

  2. *$o_photo 原圖路徑

  3. *$d_photo 處理後圖片路徑

  4. *$width 定義寬

  5. *$height 定義高

  6. *調用方法 cutphoto("test.jpg","temp.jpg",256,146);

  7. */

  8. function cutphoto($o_photo,$d_photo,$width,$height){

  9. $temp_img = imagecreatefromjpeg($o_photo);

  10. $o_width = imagesx($temp_img); //取得原圖寬

  11. $o_height = imagesy($temp_img); //取得原圖高

  12. //判斷處理方法

  13. if($width>$o_width || $height>$o_height){//原圖寬或高比規定的尺寸小,進行壓縮

  14. $newwidth=$o_width;

  15. $newheight=$o_height;

  16. if($o_width>$width){

  17. $newwidth=$width;

  18. $newheight=$o_height*$width/$o_width;

  19. }

  20. if($newheight>$height){

  21. $newwidth=$newwidth*$height/$newheight;

  22. $newheight=$height;

  23. }

  24. //縮圖片

  25. $new_img = imagecreatetruecolor($newwidth, $newheight);

  26. imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);

  27. imagejpeg($new_img , $d_photo);

  28. imagedestroy($new_img);

  29. }else{//原圖寬與高都比規定尺寸大,進行壓縮後裁剪

  30. if($o_height*$width/$o_width>$height){//先確定width與規定相同,如果height比規定大,則ok

  31. $newwidth=$width;

  32. $newheight=$o_height*$width/$o_width;

  33. $x=0;

  34. $y=($newheight-$height)/2;

  35. }else{ //否則確定height與規定相同,width自適應

  36. $newwidth=$o_width*$height/$o_height;

  37. $newheight=$height;

  38. $x=($newwidth-$width)/2;

  39. $y=0;

  40. }

  41. //縮圖片

  42. $new_img = imagecreatetruecolor($newwidth, $newheight);

  43. imagecopyresampled($new_img, $temp_img, 0, 0, 0, 0, $newwidth, $newheight, $o_width, $o_height);

  44. imagejpeg($new_img , $d_photo);

  45. imagedestroy($new_img);

  46. $temp_img = imagecreatefromjpeg($d_photo);

  47. $o_width = imagesx($temp_img); //取得縮圖寬

  48. $o_height = imagesy($temp_img); //取得縮圖高

  49. //裁剪圖片

  50. $new_imgx = imagecreatetruecolor($width,$height);

  51. imagecopyresampled($new_imgx,$temp_img,0,0,$x,$y,$width,$height,$width,$height);

  52. imagejpeg($new_imgx , $d_photo);

  53. imagedestroy($new_imgx);

  54. }
  55. }
  56. ?>

複製代碼
  • 聯繫我們

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