ImageCompressService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\common\service;
  3. use Exception;
  4. class ImageCompressService
  5. {
  6. /**
  7. * 压缩图片
  8. * @param string $sourcePath 源文件路径
  9. * @param int $quality 压缩质量 (1-100)
  10. * @param int $maxWidth 最大宽度
  11. * @param int $maxHeight 最大高度
  12. * @return string|false 压缩后的文件路径
  13. */
  14. public static function compress($sourcePath, $quality = 80, $maxWidth = 1920, $maxHeight = 1080)
  15. {
  16. try {
  17. // 获取图片信息
  18. $imageInfo = getimagesize($sourcePath);
  19. outFileLog($imageInfo,'upload_img','imgInfo');
  20. if (!$imageInfo) {
  21. throw new Exception('无法获取图片信息');
  22. }
  23. $width = $imageInfo[0];
  24. $height = $imageInfo[1];
  25. $type = $imageInfo[2];
  26. // 如果图片尺寸已经很小,且质量要求不高,直接返回原文件
  27. if ($width <= $maxWidth && $height <= $maxHeight && $quality >= 90) {
  28. return $sourcePath;
  29. }
  30. // 计算新尺寸
  31. $newSize = self::calculateNewSize($width, $height, $maxWidth, $maxHeight);
  32. $newWidth = $newSize['width'];
  33. $newHeight = $newSize['height'];
  34. // 创建源图片资源
  35. $sourceImage = self::createImageFromType($sourcePath, $type);
  36. if (!$sourceImage) {
  37. throw new Exception('不支持的图片格式');
  38. }
  39. // 创建新图片资源
  40. $newImage = imagecreatetruecolor($newWidth, $newHeight);
  41. // 处理透明背景(PNG/GIF)
  42. if ($type == IMAGETYPE_PNG || $type == IMAGETYPE_GIF) {
  43. imagealphablending($newImage, false);
  44. imagesavealpha($newImage, true);
  45. $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
  46. imagefill($newImage, 0, 0, $transparent);
  47. }
  48. // 重新采样
  49. imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  50. // 生成压缩后的文件路径
  51. $compressedPath = self::generateCompressedPath($sourcePath);
  52. // 保存压缩后的图片
  53. $result = self::saveImageByType($newImage, $compressedPath, $type, $quality);
  54. // 释放内存
  55. imagedestroy($sourceImage);
  56. imagedestroy($newImage);
  57. return $result ? $compressedPath : false;
  58. } catch (Exception $e) {
  59. return false;
  60. }
  61. }
  62. /**
  63. * 计算新尺寸(保持宽高比)
  64. */
  65. private static function calculateNewSize($width, $height, $maxWidth, $maxHeight)
  66. {
  67. $ratio = min($maxWidth / $width, $maxHeight / $height, 1);
  68. return [
  69. 'width' => intval($width * $ratio),
  70. 'height' => intval($height * $ratio)
  71. ];
  72. }
  73. /**
  74. * 根据图片类型创建图片资源
  75. */
  76. private static function createImageFromType($path, $type)
  77. {
  78. switch ($type) {
  79. case IMAGETYPE_JPEG:
  80. return imagecreatefromjpeg($path);
  81. case IMAGETYPE_PNG:
  82. return imagecreatefrompng($path);
  83. case IMAGETYPE_GIF:
  84. return imagecreatefromgif($path);
  85. case IMAGETYPE_WEBP:
  86. return imagecreatefromwebp($path);
  87. default:
  88. return false;
  89. }
  90. }
  91. /**
  92. * 根据图片类型保存图片
  93. */
  94. private static function saveImageByType($image, $path, $type, $quality)
  95. {
  96. switch ($type) {
  97. case IMAGETYPE_JPEG:
  98. return imagejpeg($image, $path, $quality);
  99. case IMAGETYPE_PNG:
  100. // PNG压缩级别 0-9,质量参数需要转换
  101. $pngQuality = intval((100 - $quality) / 10);
  102. return imagepng($image, $path, $pngQuality);
  103. case IMAGETYPE_GIF:
  104. return imagegif($image, $path);
  105. case IMAGETYPE_WEBP:
  106. return imagewebp($image, $path, $quality);
  107. default:
  108. return false;
  109. }
  110. }
  111. /**
  112. * 生成压缩后的文件路径
  113. */
  114. private static function generateCompressedPath($originalPath)
  115. {
  116. $pathInfo = pathinfo($originalPath);
  117. return $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '_compressed.' . $pathInfo['extension'];
  118. }
  119. }