3

データベースのバイト[]フィールドに保存したプロフィール写真があります。

私がしたいのは、実行時に画像のサムネイルを作成することです。Web ページのさまざまな場所にさまざまなサイズの画像を表示する必要があるためです。コメント セクションやその他の領域に画像を表示する facebook のようなもの。

私が使用できる任意の grails プラグイン、私は google imageTool、imageMagick grails プラグインを持っています。それを行うための他のアプローチについては、誰でもプラグインを推奨できます。

ありがとう。

4

1 に答える 1

1

はい、grails使用できるプラグインがあります。

このImageTools プラグインを参照してください

プラグインをインストールした後、次のステートメントを使用して、目的のサイズのサムネイルを生成できます

または、それがシンアプリであり、外部依存関係が必要ない場合..次のコードソースを使用できます

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 ) 

  }
于 2013-03-18T07:36:32.533 に答える