gpt4 book ai didi

java - 调整上传图片的大小以便更好地查看

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:42 28 4
gpt4 key购买 nike

我有一张个人资料图片,我将其存储在数据库中的 byte[] 字段中。

我想做的是在运行时创建图像缩略图。因为我必须在网页的不同位置显示不同尺寸的图像。像 facebook 这样的东西,它在评论部分和其他区域显示图像。

我可以使用的任何 grails 插件,我有 google imageTool,imageMagick grails 插件。任何人都可以推荐该插件用于任何其他方法来做到这一点。

谢谢。

最佳答案

是的,您可以使用 grails 插件。

查看此 ImageTools plugin

安装插件后,您可以使用以下语句生成所需大小的缩略图

或者如果它是一个瘦应用程序并且您不想要外部依赖项..您可以使用以下代码 Source

import java.awt.Image as AWTImage 
import java.awt.image.BufferedImage
import javax.swing.ImageIcon
import javax.imageio.ImageIO as IIO
import java.awt.Graphics2D

static resize = { bytes, out, maxW, maxH ->
AWTImage ai = new ImageIcon( bytes ).image
int width = ai.getWidth( null )
int height = ai.getHeight( null )

def limits = 300..2000
assert limits.contains( width ) && limits.contains( height ) : 'Picture is either too small or too big!'

float aspectRatio = width / height
float requiredAspectRatio = maxW / maxH

int dstW = 0
int dstH = 0
if( requiredAspectRatio < aspectRatio ){
dstW = maxW
dstH = Math.round( maxW / aspectRatio )
}else{
dstH = maxH
dstW = Math.round( maxH * aspectRatio )
}

BufferedImage bi = new BufferedImage( dstW, dstH, BufferedImage.TYPE_INT_RGB )
Graphics2D g2d = bi.createGraphics()
g2d.drawImage( ai, 0, 0, dstW, dstH, null, null )

IIO.write( bi, 'JPEG', out )

}

关于java - 调整上传图片的大小以便更好地查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15471349/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com