当前位置:首页 > PHP教程 > php图像处理 > 列表

基于PHP实现等比压缩图片大小

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-13 11:01:51 浏览: 评论:0 

通过本段代码给大家介绍基于php实现等比压缩图片大小的相关知识,代码简单易懂,对php压缩图片相关知识感兴趣的朋友参考下吧。

废话不多说了,直接给大家贴php等比压缩图片大小的相关代码了,具体代码如下所示:

  1. <?php 
  2. $im = imagecreatefromjpeg('D:\phpplace\.jpeg'); 
  3. resizeImage($im,,,'xinde','.jpg'); 
  4. function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) 
  5. { 
  6. $pic_width = imagesx($im); 
  7. $pic_height = imagesy($im); 
  8. echo "start-----------------" ; 
  9. if(($maxwidth && $pic_width > $maxwidth) && ($maxheight && $pic_height > $maxheight)) 
  10. { 
  11. if($maxwidth && $pic_width>$maxwidth) 
  12. { 
  13. $widthratio = $maxwidth/$pic_width; 
  14. $resizewidth_tag = true; 
  15. } 
  16. if($maxheight && $pic_height>$maxheight) 
  17. { 
  18. $heightratio = $maxheight/$pic_height; 
  19. $resizeheight_tag = true; 
  20. } 
  21. if($resizewidth_tag && $resizeheight_tag) 
  22. { 
  23. if($widthratio<$heightratio) 
  24. $ratio = $widthratio; 
  25. else 
  26. $ratio = $heightratio; 
  27. } 
  28. if($resizewidth_tag && !$resizeheight_tag) 
  29. $ratio = $widthratio; 
  30. if($resizeheight_tag && !$resizewidth_tag) 
  31. $ratio = $heightratio; 
  32. $newwidth = $pic_width * $ratio; 
  33. $newheight = $pic_height * $ratio; 
  34. if(function_exists("imagecopyresampled")) 
  35. { 
  36. $newim = imagecreatetruecolor($newwidth,$newheight); 
  37. imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height); 
  38. } 
  39. else 
  40. { 
  41. $newim = imagecreate($newwidth,$newheight); 
  42. imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height); 
  43. } 
  44. $name = $name.$filetype; 
  45. imagejpeg($newim,$name); 
  46. imagedestroy($newim); 
  47. } 
  48. else 
  49. { 
  50. $name = $name.$filetype; 
  51. imagejpeg($im,$name); 
  52. }  
  53. } 

以上代码内容是小编给大家介绍的基于PHP实现等比压缩图片大小的相关内容,代码简单易懂,哪里写的不好,欢迎各位大侠多多提出宝贵意见,小编非常乐意。

Tags: PHP等比压缩图片大小

分享到: