php圖片壓縮的實現代碼

來源:互聯網
上載者:User
  1. /*

  2. 函數:調整圖片尺寸或產生縮圖
  3. 返回:True/False
  4. 參數:
  5. $Image 需要調整的圖片(含路徑)
  6. $Dw=450 調整時最大寬度;縮圖時的絕對寬度
  7. $Dh=450 調整時最大高度;縮圖時的絕對高度
  8. $Type=1 1,調整尺寸; 2,產生縮圖
  9. site http://bbs.it-home.org
  10. */

  11. $path='img/';//路徑

  12. $phtypes=array(
  13. 'img/gif',
  14. 'img/jpg',
  15. 'img/jpeg',
  16. 'img/bmp',
  17. 'img/pjpeg',
  18. 'img/x-png'
  19. );

  20. FunctionImg($Image,$Dw=450,$Dh=450,$Type=1){

  21. IF(!File_Exists($Image)){
  22. ReturnFalse;
  23. }
  24. #如果需要產生縮圖,則將原圖拷貝一下重新給$Image賦值
  25. IF($Type!=1){
  26. Copy($Image,Str_Replace(".","_x.",$Image));
  27. $Image=Str_Replace(".","_x.",$Image);
  28. }

  29. #取得檔案的類型,根據不同的類型建立不同的對象

  30. $ImgInfo=GetImageSize($Image);
  31. Switch($ImgInfo[2]){
  32. Case1:
  33. $Img =@ImageCreateFromGIF($Image);
  34. Break;
  35. Case2:
  36. $Img =@ImageCreateFromJPEG($Image);
  37. Break;
  38. Case3:
  39. $Img =@ImageCreateFromPNG($Image);
  40. Break;
  41. }

  42. #如果對象沒有建立成功,則說明非圖片檔案

  43. IF(Empty($Img)){
  44. #如果是產生縮圖的時候出錯,則需要刪掉已經複製的檔案
  45. IF($Type!=1){Unlink($Image);}
  46. ReturnFalse;
  47. }

  48. #如果是執行調整尺寸操作則

  49. IF($Type==1){
  50. $w=ImagesX($Img);
  51. $h=ImagesY($Img);
  52. $width = $w;
  53. $height = $h;
  54. IF($width>$Dw){
  55. $Par=$Dw/$width;
  56. $width=$Dw;
  57. $height=$height*$Par;
  58. IF($height>$Dh){
  59. $Par=$Dh/$height;
  60. $height=$Dh;
  61. $width=$width*$Par;
  62. }
  63. }ElseIF($height>$Dh){
  64. $Par=$Dh/$height;
  65. $height=$Dh;
  66. $width=$width*$Par;
  67. IF($width>$Dw){
  68. $Par=$Dw/$width;
  69. $width=$Dw;
  70. $height=$height*$Par;
  71. }
  72. }Else{
  73. $width=$width;
  74. $height=$height;
  75. }
  76. $nImg =ImageCreateTrueColor($width,$height); #建立一個真彩色畫布
  77. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);#重採樣拷貝部分映像並調整大小
  78. ImageJpeg($nImg,$Image); #以JPEG格式將映像輸出到瀏覽器或檔案
  79. ReturnTrue;
  80. #如果是執行產生縮圖操作則
  81. }Else{
  82. $w=ImagesX($Img);
  83. $h=ImagesY($Img);
  84. $width = $w;
  85. $height = $h;

  86. $nImg =ImageCreateTrueColor($Dw,$Dh);

  87. IF($h/$w>$Dh/$Dw){#高比較大
  88. $width=$Dw;
  89. $height=$h*$Dw/$w;
  90. $IntNH=$height-$Dh;
  91. ImageCopyReSampled($nImg, $Img,0,-$IntNH/1.8,0,0, $Dw, $height, $w, $h);
  92. }Else{ #寬比較大
  93. $height=$Dh;
  94. $width=$w*$Dh/$h;
  95. $IntNW=$width-$Dw;
  96. ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
  97. }
  98. ImageJpeg($nImg,$Image);
  99. ReturnTrue;
  100. }
  101. }
  102. ?>

複製代碼

2、調用樣本

  1. /**
  2. 圖片壓縮處理
  3. */
  4. if($_SERVER['REQUEST_METHOD']=='POST'){
  5. if(!is_uploaded_file($_FILES["photo"][tmp_name])){
  6. echo "圖片不存在";
  7. exit();
  8. }

  9. if(!is_dir('img')){//路徑若不存在則建立

  10. mkdir('img');
  11. }

  12. $upfile=$_FILES["photo"];

  13. $pinfo=pathinfo($upfile["name"]);
  14. $name=$pinfo['basename'];//檔案名稱
  15. $tmp_name=$upfile["tmp_name"];
  16. $file_type=$pinfo['extension'];//獲得檔案類型
  17. $showphpath=$path.$name;
  18. if(in_array($upfile["type"],$phtypes)){
  19. echo "檔案類型不符!";
  20. exit();
  21. }
  22. if(move_uploaded_file($tmp_name,$path.$name)){
  23. echo "成功!";
  24. Img($showphpath,100,800,2);
  25. }
  26. echo "";
  27. }
  28. ?>
複製代碼
  • 聯繫我們

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