0

I want to use ImageMagick in my Grails application. I am using the following dependencies in BuildConfig.groovy

compile('jmagick:jmagick:6.6.9')
compile('org.im4java:im4java:1.2.0')

however when I try and run I get this error:

org.im4java.core.CommandException: java.io.IOException: Cannot run program 
"convert": error=2, No such file or directory.

How do I use ImageMagick or Jmagick with Grails without installing it directly, am I missing some dependencies?

doelleri
  • 19,232
  • 5
  • 61
  • 65
Programmer
  • 59
  • 2

3 Answers3

1

jmagick and im4java take two different approaches to using ImageMagick. In short, jmagick uses Java INI to access the ImageMagick API and im4java calls the ImageMagick tools. In other words, jmagick bundles ImageMagick while im4java does not. As you probably know, the error is due to ImageMagick either not being installed or not being in the environment PATH.

im4java

The two don't go together. For example, you cannot use im4java to call out to jmagick in order to avoid installing ImageMagick on the computer hosting the Grails app. To use im4java you need to install ImageMagick.

jmagick

Because jmagick is partly implemented in C, it might be problematic to use in a Servlet container. It would certainly make the web app non-portable. What might work is to have the Servlet container provide jmagick compiled for that platform, and exclude jmagick from the war file. Don't ask me how to do that :)

Community
  • 1
  • 1
Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
0

First thing first: i suggest you to use 1.4.0 version of imagemagick :). Reading the error seems that you have not configured the imagemagick path. In my grails application that uses imagemagick code looks like this:

 @Value('${externalTools.imagemagick.path:/usr/local/bin}')
 private String imagemagickPath

 CreateTemporaryThumbnailsResult call(CreateTemporaryThumbnailsEvent event){

    log.debug("IMAGEMAGICK PATH: $imagemagickPath")

    String originalFilePath = generateOriginalFilePath(event.fileName, event.fileExtension)
    String mediumFilePath = generateMediumFilePath(event.fileName, event.fileExtension)

    // create command
    ConvertCmd cmd = new ConvertCmd();
    cmd.setSearchPath(imagemagickPath)

    // create the operation, add images and operators/options
    IMOperation op = new IMOperation();
    op.addImage(originalFilePath);

    op.thumbnail(mediumSizeImage, mediumSizeImage)
    op.background("white")
    op.gravity("center")
    op.extent(mediumSizeImage,mediumSizeImage)
    op.addImage(mediumFilePath);

    // execute the operation
    try {
        cmd.run(op);
    } catch (IOException e) {
        return new CreateTemporaryThumbnailsResult(false)
    } catch (InterruptedException e){
        return new CreateTemporaryThumbnailsResult(false)
    } catch(Exception e){
        return new CreateTemporaryThumbnailsResult(false)
    }

    return new CreateTemporaryThumbnailsResult(true, event.fileName + ImageSize.MEDIUM.toString() + "." + event.fileExtension, event.fileName)
}

I used hipsteroid grails project as example to discover how to use imagemagick with grails, i suggest you to do the same if you have any doubts, it's really full of information about images manipulation with groovy and imagemagick.

Luca Farsetti
  • 257
  • 2
  • 14
0

You could also install imageMagick on your machine and use "executeCommand" to execute a shell command to edit the images, like:

 def resizeCommand = "/usr/bin/convert pathTo/myOriginalImage.jpg -resize 1920 -quality 80 pathTo/myNewImage.jpg"
            executeCommand(resizeCommand)
Mexx
  • 359
  • 3
  • 17