Skip to content

Instantly share code, notes, and snippets.

@geekduck
Created June 20, 2014 07:48
Show Gist options
  • Save geekduck/d1d26b09254b3f8d0ba2 to your computer and use it in GitHub Desktop.
Save geekduck/d1d26b09254b3f8d0ba2 to your computer and use it in GitHub Desktop.
@Grab(group='org.imgscalr', module='imgscalr-lib', version='4.2') // Grailsの場合はBuildConfig.groovyのdependenciesに 「compile 'org.imgscalr:imgscalr-lib:4.2'」
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import org.imgscalr.*
def srcFilepath = "/path/to/target.jpg"
def destFormat = 'jpg'
BufferedImage sourceImg = ImageIO.read(new File(srcFilepath))
BufferedImage destImg = null
if(sourceImg.height >= sourceImg.width){
// 縦長の画像の場合は横幅を基準にリサイズ
destImg = Scalr.resize(sourceImg, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_WIDTH, 320, 480)
}
else{
// 横長の画像の場合は高さを基準にリサイズ
destImg = Scalr.resize(sourceImg, Scalr.Method.AUTOMATIC, Scalr.Mode.FIT_TO_HEIGHT, 480, 320)
}
File newFile = new File("/path/to/resize_${System.currentTimeMillis()}.${destFormat}")
ImageIO.write(destImg, destFormat, newFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment