【问题标题】:Resize uploaded images for better viewing调整上传图像的大小以便更好地查看
【发布时间】:2013-03-06 11:11:28
【问题描述】:

我有一张个人资料图片,我存储在数据库中的 byte[] 字段中。

我想做的是在运行时创建图像缩略图。因为我必须在网页的不同位置显示不同大小的图像。类似 facebook,在评论区和其他区域显示图片。

我可以使用的任何 grails 插件,我有 google imageTool、imageMagick grails 插件。任何人都可以推荐该插件以使用任何其他方法来做到这一点。

谢谢。

【问题讨论】:

    标签: java grails-plugin grails-2.0


    【解决方案1】:

    是的,您可以使用 grails 插件。

    看到这个ImageTools plugin

    安装插件后,您可以使用以下语句生成所需大小的缩略图

    或者如果它是一个瘦应用程序并且您不想要外部依赖..您可以使用以下代码Source

    import java.awt.Image as AWTImage 
    import java.awt.image.BufferedImage 
    import javax.swing.ImageIcon 
    import javax.imageio.ImageIO as IIO 
    import java.awt.Graphics2D 
    
      static resize = { bytes, out, maxW, maxH -> 
          AWTImage ai = new ImageIcon( bytes ).image 
          int width = ai.getWidth( null ) 
          int height = ai.getHeight( null ) 
    
          def limits = 300..2000 
          assert limits.contains( width ) && limits.contains( height ) : 'Picture is either too small or too big!'   
    
          float aspectRatio = width / height 
          float requiredAspectRatio = maxW / maxH 
    
          int dstW = 0 
          int dstH = 0 
          if( requiredAspectRatio < aspectRatio ){ 
            dstW = maxW 
            dstH = Math.round(  maxW / aspectRatio ) 
          }else{ 
            dstH = maxH 
            dstW = Math.round( maxH * aspectRatio ) 
          } 
    
          BufferedImage bi = new BufferedImage( dstW, dstH, BufferedImage.TYPE_INT_RGB ) 
          Graphics2D g2d = bi.createGraphics() 
          g2d.drawImage( ai, 0, 0, dstW, dstH, null, null ) 
    
          IIO.write( bi, 'JPEG', out ) 
    
      }
    

    【讨论】:

    • imageMagick 怎么样,你推荐这个插件吗?
    • @UmairSaleem :从未使用过 imageMagick。所以没有 cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2020-06-14
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多