gpt4 book ai didi

image - 提升图像上传,调整大小,存储在数据库中,显示

转载 作者:行者123 更新时间:2023-12-04 03:47:07 25 4
gpt4 key购买 nike

是否存在一个简单的示例,说明如何上传图像,调整图像大小,将其存储在数据库中,然后使用Lift来提供图像?

我敢肯定,我可以通过文件上传,Java 2D API,Lift Mapper和Response API将其组合在一起。但是有什么示例代码可以按照“正确”或推荐的方式执行吗?

最佳答案

我通过创建一个新的MappedField对链接到s3的Mapper字段进行了此操作。我也有一些代码可以调整大小,但是尚未测试或部署(请谨慎使用)。

class MappedS3Image[T<:Mapper[T]](owner: T, val path:String, maxWidth: String, maxHeight:String) extends MappedString[T](owner, 36) {

def url:String = MappedS3Image.fullImgPath(path, is)

def setFromUpload(fileHolder: Box[FileParamHolder]) = {
S3Sender.uploadImageToS3(path, fileHolder).map(this.set(_))
}

override def asHtml:Node = <img src={url} style={"max-width:" + maxWidth + ";max-height:"+maxHeight} />
override def _toForm: Box[Elem] = Full(SHtml.fileUpload(fu=>setFromUpload(Full(fu))))

}


import java.awt.Image
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import java.awt.Graphics2D
import java.awt.AlphaComposite

object ImageResizer {

def resize(is:java.io.InputStream, maxWidth:Int, maxHeight:Int):BufferedImage = {
val originalImage:BufferedImage = ImageIO.read(is)

val height = originalImage.getHeight
val width = originalImage.getWidth

if (width <= maxWidth && height <= maxHeight)
originalImage
else {
var scaledWidth:Int = width
var scaledHeight:Int = height
val ratio:Double = width/height
if (scaledWidth > maxWidth){
scaledWidth = maxWidth
scaledHeight = (scaledWidth.doubleValue/ratio).intValue
}
if (scaledHeight > maxHeight){
scaledHeight = maxHeight
scaledWidth = (scaledHeight.doubleValue*ratio).intValue
}
val scaledBI = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB)
val g = scaledBI.createGraphics
g.setComposite(AlphaComposite.Src)
g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
g.dispose
scaledBI
}
}
}

关于image - 提升图像上传,调整大小,存储在数据库中,显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1404814/

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