ImageCompressService.php 4.9 KB

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