Image 如何使用Grails使用imgscalr

Image 如何使用Grails使用imgscalr,image,grails,groovy,imgscalr,Image,Grails,Groovy,Imgscalr,最近几天我刚刚开始使用Groovy和Grails。我以前没有Java方面的经验,所以您必须原谅这个(可能)非常基本的问题。我搜索了Google和Stack Overflow,但并没有找到任何对我的实际安装有帮助的东西 我有一个图像上传工作,我存储在服务器上的文件。我使用了IBMGrails教程来指导我完成它。那很好 我还想以大、中、小格式调整文件大小。我想用imgscalr来做这个,但我无法让它工作。我已经下载了包含各种.jar文件的版本4.2。我需要把这些放在服务器上的某个地方并引用它们吗?我

最近几天我刚刚开始使用Groovy和Grails。我以前没有Java方面的经验,所以您必须原谅这个(可能)非常基本的问题。我搜索了Google和Stack Overflow,但并没有找到任何对我的实际安装有帮助的东西

我有一个图像上传工作,我存储在服务器上的文件。我使用了IBMGrails教程来指导我完成它。那很好

我还想以大、中、小格式调整文件大小。我想用imgscalr来做这个,但我无法让它工作。我已经下载了包含各种.jar文件的版本4.2。我需要把这些放在服务器上的某个地方并引用它们吗?我所做的唯一一件事就是将这些行添加到
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'
}
在my
PhotoController.Groovy中导入org.imgscalr.Scalr.*

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'
}
这是我将文件保存到服务器上的代码,我还想调整大小并保存图像

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])
    }
}
我得到的错误是
没有这样的属性:类的Scalr:garethlewisweb.PhotoController

我显然做错了什么。感谢您的指导。

而不是

import org.imgscalr.Scalr.*
你想要

import org.imgscalr.Scalr
import javax.imageio.ImageIO
然后,
resize
需要一个
buffereImage
(查看),请尝试:


当然,您必须注意是否有文件覆盖了已有的图像

将jar文件放在Grails应用程序的“lib”目录中。然后,您可以从
BuildConfig.groovy

中删除该行。这是“如何在grails中使用imgscalr”的第一个google结果,我很惊讶在google上搜索时缺少信息和示例。虽然第一个答案很接近,但仍有一些错误需要纠正

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'
}
对于像我这样通过谷歌在这里结束的人,这里有一个更详细的例子,说明如何正确使用这个漂亮的插件:

首先,在
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'
}
然后,在安装之后,只需将这段代码粘贴到您的控制器中,在接收包含上传图像的多部分表单的操作中

def create() {

     def userInstance = new User(params)

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

     (...) 
}
在我的域中,我实现了这个
storeImageInFileSystem
方法,它将调整图像大小并将其存储在文件系统中。但首先,将其导入文件:

import org.imgscalr.Scalr
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
然后,实现该方法:

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

       (...)
    }
}

这对我很有效。根据需要更改任何详细信息,如系统目录或图像大小。

对不起。刚刚更新了问题。try
import org.imgscalr.Scalr
而不是
import org.imgscalr.Scalr.
我得到
Groovy:无法解析org.imgscalr.Scalr类
try
运行时'org.imgscalr:imgscalr lib:4.2'
而不是在您的构建中编译'org.imgscalr:imgscalr lib:4.2',如果获得未知方法异常,请参见下文:-)
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])
    }
}