From f9b12cc0918e1d16764f15809b8dd83fc79b82a3 Mon Sep 17 00:00:00 2001 From: "Y.Tory" <5343692+kagemomiji@users.noreply.github.com> Date: Mon, 24 Jun 2024 12:35:28 +0000 Subject: [PATCH] kagemomiji/airsonic-advanced#512 Changed algorithm for risizing non square cover art --- .../player/controller/CoverArtController.java | 20 +++++++++----- .../player/service/CoverArtCreateService.java | 2 +- .../org/airsonic/player/util/ImageUtil.java | 26 +++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java b/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java index a8aea6634..5edf57b4a 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java +++ b/airsonic-main/src/main/java/org/airsonic/player/controller/CoverArtController.java @@ -62,7 +62,7 @@ * @author Sindre Mehus */ @Controller -@RequestMapping({"/coverArt", "/ext/coverArt", "/coverArt.view", "/ext/coverArt.view"}) +@RequestMapping({ "/coverArt", "/ext/coverArt", "/coverArt.view", "/ext/coverArt.view" }) public class CoverArtController { public static final String ALBUM_COVERART_PREFIX = "al-"; @@ -70,13 +70,20 @@ public class CoverArtController { public static final String PLAYLIST_COVERART_PREFIX = "pl-"; public static final String PODCAST_COVERART_PREFIX = "pod-"; + // Version of the cover art generation algorithm. + // Increment this if the algorithm changes. + private static final int COVERART_VERSION = 1; + static final Logger LOG = LoggerFactory.getLogger(CoverArtController.class); - @Autowired MediaFileService mediaFileService; - @Autowired CoverArtService coverArtService; + @Autowired + MediaFileService mediaFileService; + @Autowired + CoverArtService coverArtService; @Autowired private SettingsService settingsService; - @Autowired PlaylistService playlistService; + @Autowired + PlaylistService playlistService; @Autowired private CoverArtCreateService coverArtCreateService; @Autowired @@ -93,7 +100,8 @@ public void init() { * get last modified time epoch millisecond * * @param coverArtRequest target coverArtRequest - * @return last modified time in epoch milliseconds. if coverArtRequest id null, then return -1L + * @return last modified time in epoch milliseconds. if coverArtRequest id null, + * then return -1L */ /* private long getLastModifiedMiili(CoverArtRequest coverArtRequest) { @@ -198,7 +206,7 @@ private void sendUnscaled(CoverArtRequest coverArtRequest, HttpServletResponse r } private Path getCachedImage(CoverArtRequest request, int size) throws IOException { - String hash = DigestUtils.md5Hex(request.getKey()); + String hash = DigestUtils.md5Hex(request.getKey() + "-" + COVERART_VERSION); String encoding = request.getCoverArt() != null ? "jpeg" : "png"; Path cachedImage = getImageCacheDirectory(size).resolve(hash + "." + encoding); diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/CoverArtCreateService.java b/airsonic-main/src/main/java/org/airsonic/player/service/CoverArtCreateService.java index 9c9167efd..5875bc72b 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/CoverArtCreateService.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/CoverArtCreateService.java @@ -228,7 +228,7 @@ public BufferedImage createImage(CoverArtRequest coverArtRequest, int size) { if (bimg == null) { reason = "ImageIO.read"; } else { - return ImageUtil.scale(bimg, size, size); + return ImageUtil.scaleToSquare(bimg, size); } } LOG.warn("Failed to process cover art {}: {} failed", coverArt, reason); diff --git a/airsonic-main/src/main/java/org/airsonic/player/util/ImageUtil.java b/airsonic-main/src/main/java/org/airsonic/player/util/ImageUtil.java index bb120a3ba..0e9bf174d 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/util/ImageUtil.java +++ b/airsonic-main/src/main/java/org/airsonic/player/util/ImageUtil.java @@ -66,4 +66,30 @@ public static BufferedImage scale(BufferedImage image, int width, int height) { return thumb; } + public static BufferedImage scaleToSquare(BufferedImage image, int size) { + int w = image.getWidth(); + int h = image.getHeight(); + int scale = Math.max(w, h); + + BufferedImage squareImage = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_RGB); + squareImage.getGraphics().drawImage(image, (scale - w) / 2, (scale - h) / 2, null); + + do { + scale /= 2; + if (scale < size) { + scale = size; + } + BufferedImage temp = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_RGB); + Graphics2D g2 = temp.createGraphics(); + g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, + RenderingHints.VALUE_INTERPOLATION_BILINEAR); + g2.drawImage(squareImage, 0, 0, temp.getWidth(), temp.getHeight(), null); + g2.dispose(); + + squareImage = temp; + } while (scale != size); + + return squareImage; + } + }