How to use imgscalr using Grails

I just started using Groovy and Grails the last few days. I have no previous Java experience, so you have to excuse this (possibly) very simple question. I searched google and qaru and did not find anything that helped me with the actual installation.

I have loaded work with images, and I store the file on the server. I used the IBM Grails tutorial to help me with this. It works great.

I would also like to resize the file in large, medium and small format. I wanted to use imgscalr for this, but I can't get it to work. I downloaded version 4.2, which contains various .jar files. Do I need to place them somewhere on the server and refer to them? The only thing I did was add these lines to buildConfig.groovy

 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' } 

and import org.imgscalr.Scalr.* in my PhotoController.Groovy

Here is my code for saving the file on the server, I would also like to resize and save the image.

 def save() { def photoInstance = new Photo(params) // Handle uploaded file def uploadedFile = request.getFile('photoFile') if(!uploadedFile.empty) { println "Class: ${uploadedFile.class}" println "Name: ${uploadedFile.name}" println "OriginalFileName: ${uploadedFile.originalFilename}" println "Size: ${uploadedFile.size}" println "ContentType: ${uploadedFile.contentType}" def webRootDir = servletContext.getRealPath("/") def originalPhotoDir = new File(webRootDir, "/images/photographs/original") originalPhotoDir.mkdirs() uploadedFile.transferTo(new File(originalPhotoDir, uploadedFile.originalFilename)) BufferedImage largeImg = Scalr.resize(uploadedFile, 1366); def largePhotoDir = new File(webRootDir, "/images/photographs/large") largePhotoDir.mkdirs() photoInstance.photoFile = uploadedFile.originalFilename } if (!photoInstance.hasErrors() && photoInstance.save()) { flash.message = "Photo ${photoInstance.id} created" redirect(action:"list") } else { render(view:"create", model:[photoInstance: photoInstance]) } } 

The error I get is No such property: Scalr for class: garethlewisweb.PhotoController

I'm obviously doing something very bad. Any guidance is appreciated.

+4
source share
3 answers

This is the first google result for "How to use imgscalr in grails", and I was surprised at the lack of information and examples when it ran it. Although the first answer is close, there are still a few errors that need to be fixed.

For those who ended here like me through google, here is a more detailed example of how to use this beautiful plugin correctly:

First declare the plugin in the 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 installation, just paste this piece of code into your controller, in an action that gets a 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, which will resize the image and save it to the file system. But first import this into a file:

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

And then we 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 (...) } } 

It worked for me. Change any details as necessary, such as a system pointer or image size.

+3
source

Instead

 import org.imgscalr.Scalr.* 

Do you want to

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

Then resize requires a BufferedImage (looking at 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 will have to watch overwriting existing image files.

+1
source

Place the jar files in the "lib" directory of your Grails application. Then you can remove this line from BuildConfig.groovy

+1
source

All Articles