Last active
August 29, 2015 14:24
-
-
Save rbobillot/a7ee146c34a185fe606d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dimRefs = ( 160, 190 ) | |
def deleteFile( name:String ) = new File(name).delete | |
def createImage( name:String ):BufferedImage = name match { | |
case n if n startsWith "http://" => ImageIO.read( new URL (name) ) | |
case n if n startsWith "https://" => ImageIO.read( new URL (name) ) | |
case _ => ImageIO.read( new File(name) ) | |
} | |
def imgDims( img:BufferedImage ) = { | |
try { | |
( img.getWidth,img.getHeight ) | |
} | |
catch { | |
case _:Throwable => ( -1, -1 ) | |
} | |
} | |
def changeSize(in:String, out:String, percent:Double) = { | |
lazy val image = createImage( in ) | |
lazy val dims = imgDims( image ) | |
lazy val width:Int = (dims._1 * percent).toInt | |
lazy val height:Int = (dims._2 * percent).toInt | |
lazy val outputImage = new BufferedImage( width, height, image.getType ) | |
lazy val g2d:Graphics2D = outputImage.createGraphics | |
lazy val format = out.substring( out.lastIndexOf(".")+1 ) | |
try { | |
g2d.drawImage( image, 0, 0, width, height, null ) | |
g2d.dispose | |
ImageIO.write( outputImage, format, new File(out) ) | |
(width, height) | |
} | |
catch { | |
case _:Throwable => (-1, -1) | |
} | |
} | |
def resize( src:String ) = { | |
lazy val dims = imgDims( createImage(src) ) | |
lazy val lenDif = dimRefs._1 - dims._1 | |
lazy val widDif = dimRefs._2 - dims._2 | |
lazy val maxDif = lenDif min widDif | |
if ( maxDif < 0 ) { | |
if ( maxDif == lenDif ) | |
changeSize( src, "tmpSize.jpg", (dimRefs._1).toDouble / dims._1 ) | |
else | |
changeSize( src, "tmpSize.jpg", (dimRefs._2).toDouble / dims._2 ) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment