2009-09-10 22 views
9

有没有一个简单的例子,如何上传图像,调整大小,存储在数据库中,然后使用Lift提供图像?提起图像上传,调整大小,存储在数据库中,显示

我敢肯定,我可以将它从文件上传,Java 2D API,Lift Mapper和响应API组合在一起。但是,有没有我可以遵循的示例代码来实现“正确”或推荐的方式?

+2

没有冒犯,但这听起来像是一个体面的项目来写自己!它具有一切:阴谋,冒险和SQL。 – 2009-09-10 11:58:24

+0

是的,我要去!我只是觉得我会在开始之前寻求建议。 – Joe

回答

6

我通过创建一个新的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 
     } 
    } 
} 
+0

有缺陷的数学。这扰乱了宽高比。我用一个固定版本回答(三年后)。不过,它回答了关于正确使用库的问题,并且非常有用。 –

+0

我在下面添加了一个更正的答案。 –

3

另一个答案很好地描述了如何调整图像大小,并在文件系统上存储对文件的引用。

如果您想使用lift map存储实际的文件内容,您必须创建自定义模型对象,并在其上定义一个二进制字段。尝试是这样的:

package code { 
package model { 


import _root_.net.liftweb.mapper._ 
import _root_.net.liftweb.util._ 
import _root_.net.liftweb.common._ 


// singleton object which manipulates storing of Document instances 
object Document extends Document with KeyedMetaMapper[Long, Document] { 
} 



class Document extends KeyedMapper[Long, Document] { 
    def getSingleton = Document 
    def primaryKeyField = id 

    object id extends MappedLongIndex(this) 

    object name extends MappedString(this, 20) { 
    override def displayName = "Name" 
    override def writePermission_? = true 
    } 

    object content extends MappedBinary(this) { 
    override def displayName = "Content" 
    override def writePermission_? = true 
    } 
} 



} 
} 

然后,在引导类,在末尾添加此Document

Schemifier.schemify(true, Schemifier.infoF _, User, Document) 

瞧。使用Document save (new Document)将其存储到数据库中。 A new Document的字段可以使用set方法设置。尝试使用delete_!,find,findAll方法对Document单例进行删除或在数据库中找到它。从这一点来看,它应该是直截了当的。

最后,要显示图像,可以覆盖Lift的调度规则(在引导类Boot.scala中)。尝试使用这个覆盖pdf请求规则的示例:

def getFile(filename: String): Option[Document] = { 
    val alldocs = Document.findAll() 
    alldocs.find(_.name.get == filename) 
} 

LiftRules.statelessDispatchTable.append { 
    case Req("file" :: name :: Nil, "pdf", GetRequest) => 
    () => 
    println("Got request for: " + name + ".pdf") 
    for { 
     stream <- tryo(
     getFile(name + ".pdf") map { 
      doc => new java.io.ByteArrayInputStream(doc.content.get) 
     } getOrElse null 
    ) 
     if null ne stream 
    } yield StreamingResponse(stream, 
          () => stream.close, 
           stream.available, 
           List("Content-Type" -> "application/pdf"), 
           Nil, 
           200) 
} 
2

根据Jon Hoffman接受的答案,我修复了这些错误。他的版本混淆了纵横比(它总是变成1:1),因为数学在几个地方被关闭了。此版本调整大图片的大小,直到它们适合,并尊重长宽比。

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

    var height = originalImage.getHeight 
    var width = originalImage.getWidth 

    // Shortcut to save a pointless reprocessing in case the image is small enough already 
    if (width <= maxWidth && height <= maxHeight) 
     originalImage 
    else {   
     // If the picture was too big, it will either fit by width or height. 
     // This essentially resizes the dimensions twice, until it fits 
     if (width > maxWidth){ 
      height = (height.doubleValue() * (maxWidth.doubleValue()/width.doubleValue())).intValue 
      width = maxWidth 
     } 
     if (height > maxHeight){ 
      width = (width.doubleValue() * (maxHeight.doubleValue()/height.doubleValue())).intValue 
      height = maxHeight 
     } 
     val scaledBI = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) 
     val g = scaledBI.createGraphics 
     g.setComposite(AlphaComposite.Src) 
     g.drawImage(originalImage, 0, 0, width, height, null); 
     g.dispose 
     scaledBI 
    } 
} 
相关问题