2009-09-10 19 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 
    } 
} 
相關問題