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