Skip to content

Commit

Permalink
[release-2.19] refactor: preserve original image if smaller than requ…
Browse files Browse the repository at this point in the history
…ested thumbnail size (#6583)

This is an automated cherry-pick of #6582

/assign ruibaby

```release-note
当生成缩略图时如果原图尺寸小于请求尺寸则返回原图以保持其质量
```
  • Loading branch information
halo-dev-bot authored Sep 4, 2024
1 parent 32f563b commit 30d054a
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package run.halo.app.core.attachment;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -74,10 +76,14 @@ private void generateThumbnail(Path tempImagePath) throws IOException {
throw new UnsupportedOperationException(
"Unsupported image format for: " + formatNameOpt.orElse("unknown"));
}
var thumbnail = Scalr.resize(img, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH,
size.getWidth());
var formatName = formatNameOpt.orElse("jpg");
var thumbnailFile = getThumbnailFile(formatName);
if (img.getWidth() <= size.getWidth()) {
Files.copy(tempImagePath, thumbnailFile.toPath(), REPLACE_EXISTING);
return;
}
var thumbnail = Scalr.resize(img, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH,
size.getWidth());
ImageIO.write(thumbnail, formatName, thumbnailFile);
}

Expand Down

0 comments on commit 30d054a

Please sign in to comment.