当前位置:Gxlcms > PHP教程 > php如何实现图片压缩的同时保持清晰度

php如何实现图片压缩的同时保持清晰度

时间:2021-07-01 10:21:17 帮助过:53人阅读

直接展示详细代码:

免费学习视频教程分享:php视频教程

  1. <?php
  2. /**
  3. * 图片压缩类:通过缩放来压缩。如果要保持源图比例,把参数$percent保持为1即可。
  4. * 即使原比例压缩,也可大幅度缩小。数码相机4M图片。也可以缩为700KB左右。如果缩小比例,则体积会更小。
  5. * 结果:可保存、可直接显示。
  6. */
  7. class imgCompression{
  8. private $src;
  9. private $image;
  10. private $imageinfo;
  11. private $percent = 0.5;
  12. /**
  13. * 图片压缩
  14. * @param $src 源图
  15. * @param float $percent 压缩比例
  16. */
  17. public function __construct($src, $percent=1)
  18. {
  19. $this->src = $src;
  20. $this->percent = $percent;
  21. }
  22. /** 高清压缩图片
  23. * @param string $saveName 提供图片名(可不带扩展名,用源图扩展名)用于保存。或不提供文件名直接显示
  24. */
  25. public function compressImg($saveName='')
  26. {
  27. $this->_openImage();
  28. if(!empty($saveName)) $this->_saveImage($saveName); //保存
  29. else $this->_showImage();
  30. }
  31. /**
  32. * 内部:打开图片
  33. */
  34. private function _openImage()
  35. {
  36. list($width, $height, $type, $attr) = getimagesize($this->src);
  37. $this->imageinfo = array(
  38. 'width'=>$width,
  39. 'height'=>$height,
  40. 'type'=>image_type_to_extension($type,false),
  41. 'attr'=>$attr
  42. );
  43. $fun = "imagecreatefrom".$this->imageinfo['type'];
  44. $this->image = $fun($this->src);
  45. $this->_thumpImage();
  46. }
  47. /**
  48. * 内部:操作图片
  49. */
  50. private function _thumpImage()
  51. {
  52. $new_width = $this->imageinfo['width'] * $this->percent;
  53. $new_height = $this->imageinfo['height'] * $this->percent;
  54. $image_thump = imagecreatetruecolor($new_width,$new_height);
  55. //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
  56. imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,
  57. $this->imageinfo['width'],$this->imageinfo['height']);
  58. imagedestroy($this->image);
  59. $this->image = $image_thump;
  60. }
  61. /**
  62. * 输出图片:保存图片则用saveImage()
  63. */
  64. private function _showImage()
  65. {
  66. header('Content-Type: image/'.$this->imageinfo['type']);
  67. $funcs = "image".$this->imageinfo['type'];
  68. $funcs($this->image);
  69. }
  70. /**
  71. * 保存图片到硬盘:
  72. * @param string $dstImgName 1、可指定字符串不带后缀的名称,使用源图扩展名 。
  73. * 2、直接指定目标图片名带扩展名。
  74. */
  75. private function _saveImage($dstImgName)
  76. {
  77. if(empty($dstImgName)) return false;
  78. $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];
  79. //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
  80. $dstExt = strrchr($dstImgName ,".");
  81. $sourseExt = strrchr($this->src ,".");
  82. if(!empty($dstExt)) $dstExt =strtolower($dstExt);
  83. if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
  84. //有指定目标名扩展名
  85. if(!empty($dstExt) && in_array($dstExt,$allowImgs)){
  86. $dstName = $dstImgName;
  87. }elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){
  88. $dstName = $dstImgName.$sourseExt;
  89. }else{
  90. $dstName = $dstImgName.$this->imageinfo['type'];
  91. }
  92. $funcs = "image".$this->imageinfo['type'];
  93. $funcs($this->image,$dstName);
  94. }
  95. /**
  96. * 销毁图片
  97. */
  98. public function __destruct(){
  99. imagedestroy($this->image);
  100. }
  101. }
  102. // eg:
  103. $source = 'img.jpg';//原图
  104. $dst_img = 'img1.jpg'; //可加存放路径
  105. $percent = 1; //压缩比例 原图压缩,不缩放
  106. $image = new imgCompression($source,$percent);
  107. $image->compressImg($dst_img);
  108. ?>

相关文章教程推荐:php教程

以上就是php如何实现图片压缩的同时保持清晰度的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行