Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#512 Changed algorithm for risizing non square cover art #517

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,28 @@
* @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-";
public static final String ARTIST_COVERART_PREFIX = "ar-";
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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}