This commit is contained in:
looly
2021-12-17 18:04:59 +08:00
parent 8c7106d7f3
commit 1756535595
3 changed files with 16 additions and 8 deletions

View File

@@ -253,23 +253,30 @@ public class Img implements Serializable {
* @return this
*/
public Img scale(int width, int height) {
return scale(width, height, Image.SCALE_SMOOTH);
}
/**
* 缩放图像(按长宽缩放)<br>
* 注意:目标长宽与原图不成比例会变形
*
* @param width 目标宽度
* @param height 目标高度
* @param scaleType 缩放类型,可选{@link Image#SCALE_SMOOTH}平滑模式或{@link Image#SCALE_DEFAULT}默认模式
* @return this
* @since 5.7.18
*/
public Img scale(int width, int height, int scaleType) {
final Image srcImg = getValidSrcImg();
int srcHeight = srcImg.getHeight(null);
int srcWidth = srcImg.getWidth(null);
int scaleType;
if (srcHeight == height && srcWidth == width) {
// 源与目标长宽一致返回原图
this.targetImage = srcImg;
return this;
} else if (srcHeight < height || srcWidth < width) {
// 放大图片使用平滑模式
scaleType = Image.SCALE_SMOOTH;
} else {
scaleType = Image.SCALE_DEFAULT;
}
if (ImgUtil.IMAGE_TYPE_PNG.equals(this.targetImageType)) {
// png特殊处理借助AffineTransform可以实现透明度保留
final double sx = NumberUtil.div(width, srcWidth);// 宽度缩放比