How to use imgscalr using Grails

我只是一个虾纸丫 提交于 2019-12-04 09:42:06

This is the first google result for "How to use imgscalr in grails" and I was surprised with the lack of informations and examples when googling it. Although the first answer is close, there's still a few mistakes to be corrected.

To anyone that ended here like me through google, heres a more detailed example of how to correctly use this nice plugin:

First, declare the plugin in your BuildConfig.groovy file:

dependencies {
    // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.

    // runtime 'mysql:mysql-connector-java:5.1.20'
    compile 'org.imgscalr:imgscalr-lib:4.2'
}

Then, after installed, just paste this piece of code in your controller, in the action that receives the multi-part form with the image uploaded.

def create() {

     def userInstance = new User(params)

     //saving image
     def imgFile = request.getFile('myFile')
     def webRootDir = servletContext.getRealPath("/")
     userInstance.storeImageInFileSystem(imgFile, webRootDir)  

     (...) 
}

Inside my domain, I implemented this storeImageInFileSystem method, that will resize the image and store it in the filesystem. But first, import this to the file:

import org.imgscalr.Scalr
import java.awt.image.BufferedImage
import javax.imageio.ImageIO

And then, implement the method:

def storeImageInFileSystem(imgFile, webRootDir){

    if (!imgFile.empty)
    {
        def defaultPath = "/images/userImages"
        def systemDir = new File(webRootDir, defaultPath)

        if (!systemDir.exists()) {
            systemDir.mkdirs()
        }

        def imgFileDir = new File( systemDir, imgFile.originalFilename)
        imgFile.transferTo( imgFileDir )

        def imageIn = ImageIO.read(imgFileDir);

        BufferedImage scaledImage = Scalr.resize(imageIn, 200);   //200 is the size of the image
        ImageIO.write(scaledImage, "jpg", new File( systemDir, imgFile.originalFilename )); //write image in filesystem

       (...)
    }
}

This worked well for me. Change any details as the need, like the system diretory or the size of the image.

Instead of

import org.imgscalr.Scalr.*

You want

import org.imgscalr.Scalr
import javax.imageio.ImageIO

Then resize needs a BufferedImage (looking at the JavaDocs), so try:

    def originalPhotoDir = new File(webRootDir, "/images/photographs/original")
    originalPhotoDir.mkdirs()
    def originalPhotoFile = new File(originalPhotoDir, uploadedFile.originalFilename)
    uploadedFile.transferTo( originalPhotoFile )

    // Load the image
    BufferedImage originalImage = ImageIO.read( originalPhotoFile )
    // Scale it
    BufferedImage largeImg = Scalr.resize(uploadedFile, 1366);

    // Make the destination folder
    def largePhotoDir = new File(webRootDir, "/images/photographs/large" )
    largePhotoDir.mkdirs()

    // Write the large image out
    ImageIO.write( largeImg, 'png', new File( largePhotoDir, uploadedFile.originalFilename )

Of course, you'll have to watch for files overwriting already existing images

Put the jar file(s) in the 'lib' directory of your Grails application. You may then delete that line from BuildConfig.groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!