ImageScalr + Grails + AWS插件

时间:2013-09-28 09:44:07

标签: grails amazon-s3 image-resizing image-scaling

我试图将一些图片上传到我的webapp。 我遇到的问题是我试图使用Imagescalr创建原始照片的拇指,我使用AWS插件将它们上传到我的桶。 所以,我的代码如下:(删除的验证和不影响问题/可能答案的事情)

def uploadPic() {
def f = request.getFile('file')
.   
.
.       
def s3file = f.inputStream.s3upload(filename) { //this is for the normal photo
            path "POI/ID/"
    }

def imageIn = ImageIO.read(???); //Dont know if I can put the f file here as parameter... do I have to store it somewhere first, call the s3 file, or I can resize on - the - fly?
BufferedImage scaledImage = Scalr.resize(imageIn, 150);



//Here I should upload the thumb. How can I call something like what is done for the normal photo? 

所以问题/问题在代码中解释,希望有人知道如何做到这一点。提前致谢。

1 个答案:

答案 0 :(得分:2)

在Grails中,

,request.getFile()不返回java.io.File对象。你可以使用输入流来写出一个文件但是我可能会做这样的事情,虽然我会使用服务并且稍微分解一下。但这应该让你开始朝着正确的方向前进。请考虑更多伪代码工作流建议。

def uploadPic() {

   def f = request.getFile('file')
   def tempFile = new File('/some/local/dir/myImage.png') 
   f.transferTo(tempFile)

   // upload the original image to S3
   whateverApi.s3Upload(tempFile)

   def bufferedImage = ImageIO.read(tempFile)
   def scaledBufferedImage = Scalr.resize(bufferedImage, 150)

   // write the scaledImg to disk
   def scaledImage = new File('/some/local/dir/myImage-150.png');
   ImageIO.write(scaledBufferedImage, "png", scaledImage);

   //upload scaled image to S3
   whateverApi.s3Upload(scaledImage)

   // clean up
   tempFile.delete()
   scaledImage.delete()

}