首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Grails使用imgscalr

如何通过Grails使用imgscalr
EN

Stack Overflow用户
提问于 2013-01-18 21:00:37
回答 3查看 3.3K关注 0票数 4

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

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

我还想以大、中、小格式调整文件的大小。我想使用imgscalr来做这件事,但是我不能让它工作。我已经下载了4.2版,它包含了各种.jar文件。我是否需要将它们放在服务器上的某个位置并引用它们?我所做的唯一一件事就是将这些行添加到buildConfig.groovy

代码语言:javascript
复制
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'
}

和我的PhotoController.Groovy中的import org.imgscalr.Scalr.*

这是我将文件保存到服务器上的代码,我还想调整大小并保存图像。

代码语言:javascript
复制
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])
    }
}

我得到的错误是No such property: Scalr for class: garethlewisweb.PhotoController

很明显我做错了什么。如有任何指导,敬请惠顾。

EN

回答 3

Stack Overflow用户

发布于 2013-09-11 21:12:47

这是谷歌关于“如何在grails中使用imgscalr”的第一个结果,当我在谷歌上搜索它时,我惊讶地发现缺乏信息和例子。虽然第一个答案很接近,但仍然有一些错误需要纠正。

对于像我这样通过google在这里结束的人,这里有一个更详细的例子来说明如何正确使用这个很好的插件:

首先,在BuildConfig.groovy文件中声明插件:

代码语言:javascript
复制
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'
}

然后,在安装之后,只需将这段代码粘贴到您的控制器中,在接收包含上传图像的多部分表单的操作中。

代码语言:javascript
复制
def create() {

     def userInstance = new User(params)

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

     (...) 
}

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

代码语言:javascript
复制
import org.imgscalr.Scalr
import java.awt.image.BufferedImage
import javax.imageio.ImageIO

然后,实现该方法:

代码语言:javascript
复制
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

       (...)
    }
}

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

票数 3
EN

Stack Overflow用户

发布于 2013-01-18 21:14:42

而不是

代码语言:javascript
复制
import org.imgscalr.Scalr.*

你想要的

代码语言:javascript
复制
import org.imgscalr.Scalr
import javax.imageio.ImageIO

然后resize需要一个BufferedImage (查看the JavaDocs),因此尝试:

代码语言:javascript
复制
    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 )

当然,你必须注意覆盖已经存在的图像的文件

票数 1
EN

Stack Overflow用户

发布于 2013-06-23 17:30:30

将jar文件放在Grails应用程序的'lib‘目录中。然后,可以从BuildConfig.groovy中删除该行

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14399537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档