php實現圖片壓縮的二個例子

來源:互聯網
上載者:User
  1. /**
  2. * desription 壓縮圖片
  3. * @param sting $imgsrc 圖片路徑
  4. * @param string $imgdst 壓縮後儲存路徑
  5. */
  6. function image_png_size_add($imgsrc,$imgdst){
  7. list($width,$height,$type)=getimagesize($imgsrc);
  8. $new_width = ($width>600?600:$width)*0.9;
  9. $new_height =($height>600?600:$height)*0.9;
  10. switch($type){
  11. case 1:
  12. $giftype=check_gifcartoon($imgsrc);
  13. if($giftype){
  14. header('Content-Type:image/gif');
  15. $image_wp=imagecreatetruecolor($new_width, $new_height);
  16. $image = imagecreatefromgif($imgsrc);
  17. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  18. imagejpeg($image_wp, $imgdst,75);
  19. imagedestroy($image_wp);
  20. }
  21. break;
  22. case 2:
  23. header('Content-Type:image/jpeg');
  24. $image_wp=imagecreatetruecolor($new_width, $new_height);
  25. $image = imagecreatefromjpeg($imgsrc);
  26. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  27. imagejpeg($image_wp, $imgdst,75);
  28. imagedestroy($image_wp);
  29. break;
  30. case 3:
  31. header('Content-Type:image/png');
  32. $image_wp=imagecreatetruecolor($new_width, $new_height);
  33. $image = imagecreatefrompng($imgsrc);
  34. imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  35. imagejpeg($image_wp, $imgdst,75);
  36. imagedestroy($image_wp);
  37. break;
  38. } // bbs.it-home.org
  39. }
  40. /**
  41. * desription 判斷是否gif動畫
  42. * @param sting $image_file圖片路徑
  43. * @return boolean t 是 f 否
  44. */
  45. function check_gifcartoon($image_file){
  46. $fp = fopen($image_file,'rb');
  47. $image_head = fread($fp,1024);
  48. fclose($fp);
  49. return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;
  50. }
  51. ?>
複製代碼

例2:

  1. /*
  2. 函數:調整圖片尺寸或產生縮圖
  3. 返回:True/False
  4. 參數:
  5. $Image 需要調整的圖片(含路徑)
  6. $Dw=450 調整時最大寬度;縮圖時的絕對寬度
  7. $Dh=450 調整時最大高度;縮圖時的絕對高度
  8. $Type=1 1,調整尺寸; 2,產生縮圖
  9. $path='img/';//路徑
  10. $phtypes=array(
  11. 'img/gif',
  12. 'img/jpg',
  13. 'img/jpeg',
  14. 'img/bmp',
  15. 'img/pjpeg',
  16. 'img/x-png'
  17. );
  18. Function Img($Image,$Dw=450,$Dh=450,$Type=1){
  19. IF(!File_Exists($Image)){
  20. Return False;
  21. }
  22. //如果需要產生縮圖,則將原圖拷貝一下重新給$Image賦值
  23. IF($Type!=1){
  24. Copy($Image,Str_Replace(".","_x.",$Image));
  25. $Image=Str_Replace(".","_x.",$Image);
  26. }
  27. //取得檔案的類型,根據不同的類型建立不同的對象
  28. $ImgInfo=GetImageSize($Image);
  29. Switch($ImgInfo[2]){
  30. Case 1:
  31. $Img = @ImageCreateFromGIF($Image);
  32. Break;
  33. Case 2:
  34. $Img = @ImageCreateFromJPEG($Image);
  35. Break;
  36. Case 3:
  37. $Img = @ImageCreateFromPNG($Image);
  38. Break;
  39. }
  40. //如果對象沒有建立成功,則說明非圖片檔案
  41. IF(Empty($Img)){
  42. //如果是產生縮圖的時候出錯,則需要刪掉已經複製的檔案
  43. IF($Type!=1){Unlink($Image);}
  44. Return False;
  45. }
  46. //如果是執行調整尺寸操作則
  47. IF($Type==1){
  48. $w=ImagesX($Img);
  49. $h=ImagesY($Img);
  50. $width = $w;
  51. $height = $h;
  52. IF($width>$Dw){
  53. $Par=$Dw/$width;
  54. $width=$Dw;
  55. $height=$height*$Par;
  56. IF($height>$Dh){
  57. $Par=$Dh/$height;
  58. $height=$Dh;
  59. $width=$width*$Par;
  60. }
  61. }ElseIF($height>$Dh){
  62. $Par=$Dh/$height;
  63. $height=$Dh;
  64. $width=$width*$Par;
  65. IF($width>$Dw){
  66. $Par=$Dw/$width;
  67. $width=$Dw;
  68. $height=$height*$Par;
  69. }
  70. }Else{
  71. $width=$width;
  72. $height=$height;
  73. }
  74. $nImg = ImageCreateTrueColor($width,$height); //建立一個真彩色畫布
  75. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重採樣拷貝部分映像並調整大小
  76. ImageJpeg ($nImg,$Image); //以JPEG格式將映像輸出到瀏覽器或檔案
  77. Return True;
  78. //如果是執行產生縮圖操作則
  79. }Else{
  80. $w=ImagesX($Img);
  81. $h=ImagesY($Img);
  82. $width = $w;
  83. $height = $h;
  84. $nImg = ImageCreateTrueColor($Dw,$Dh);
  85. IF($h/$w>$Dh/$Dw){ //高比較大
  86. $width=$Dw;
  87. $height=$h*$Dw/$w;
  88. $IntNH=$height-$Dh;
  89. ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
  90. }Else{ //寬比較大
  91. $height=$Dh;
  92. $width=$w*$Dh/$h;
  93. $IntNW=$width-$Dw;
  94. ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
  95. }
  96. ImageJpeg ($nImg,$Image);
  97. Return True;
  98. }
  99. }
  100. ?>
  101. if($_SERVER['REQUEST_METHOD']=='POST'){
  102. if (!is_uploaded_file($_FILES["photo"][tmp_name])){
  103. echo "圖片不存在";
  104. exit();
  105. }
  106. if(!is_dir('img')){//路徑若不存在則建立
  107. mkdir('img');
  108. }
  109. $upfile=$_FILES["photo"];
  110. $pinfo=pathinfo($upfile["name"]);
  111. $name=$pinfo['basename'];//檔案名稱
  112. $tmp_name=$upfile["tmp_name"];
  113. $file_type=$pinfo['extension'];//獲得檔案類型
  114. $showphpath=$path.$name;
  115. if(in_array($upfile["type"],$phtypes)){
  116. echo "檔案類型不符!";
  117. exit();
  118. }
  119. if(move_uploaded_file($tmp_name,$path.$name)){
  120. echo "成功!";
  121. Img($showphpath,100,800,2);
  122. }
  123. echo "";
  124. }
  125. ?>
複製代碼
  • 聯繫我們

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