Skip to content

Instantly share code, notes, and snippets.

@dhoss
Created May 2, 2013 19:36
Show Gist options
  • Save dhoss/5504780 to your computer and use it in GitHub Desktop.
Save dhoss/5504780 to your computer and use it in GitHub Desktop.
[error] /Users/daustin/web-dev/lumos-pottery/app/thumbnailer/Thumbnailer.scala:41: value scaledWidth is not a member of Int [error] g2d.drawImage(originalImage, 0, 0 scaledWidth, scaledHeight, null)
/** Thumbnailer
* generate a thumbnail of an image
*
* ==Overview==
* {{{
* val thumbnail = new Thumbnailer(.25, "image.jpg", "image-thumbnail.jpg")
* val resized = thumbnail.resize()
* thumbnail.save()
* }}}
*/
package app.thumbnailer
import java.io.File
import java.awt.Graphics2D
import java.awt.Image
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
/** Generate a thumbnail */
class Thumbnailer( scale: Double, file: String, outputPath: String ) {
/** Resize an image and return the data
* @param scale percent to scale image by
* @param file original file to be thumbnailed
* @param outputPath path to put thumbnail (should be moved to a config value)
*/
def resize: Image = {
// get the original image's info
val originalImage: BufferedImage = ImageIO.read(new File(file))
// calculate the new width and height depending on the scale factor
val scaledWidth : Int = (originalImage.getWidth() * scale).toInt
val scaledHeight : Int = (originalImage.getHeight() * scale).toInt
// create the output image
val outputImage : BufferedImage = new BufferedImage(scaledWidth, scaledHeight, originalImage.getType())
// scale the image
val g2d : Graphics2D = outputImage.createGraphics();
g2d.drawImage(originalImage, 0, 0 scaledWidth, scaledHeight, null)
g2d.dispose()
outputImage
}
def save: Unit = {
val resizedImage = resize()
// get file extension
val fileExtension : String = outputPath.substring(outputPath.lastIndexOf(".") + 1)
// write image out to file
ImageIO.write(resizedImage, fileExtension, new File(outputPath))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment