ImgCompress.php 3.8 KB

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