Image Resizing for Play Framework 2
Java Scala
Switch branches/tags
Nothing to show
Latest commit 35a6981 Sep 15, 2014 @leon leon 1.1.2
Failed to load latest commit information.
project Support for play framework 2.3.4 #8 Sep 11, 2014
.gitignore Support for play framework 2.3.4 #8 Sep 11, 2014 1.1.2 Sep 15, 2014 1.1.2 Sep 15, 2014
deploy.sbt Support for play framework 2.3.4 #8 Sep 11, 2014

Scalr plugin for Play Framework 2


Add plugin to dependencies

val appDependencies = Seq(
	"se.digiplant" %% "play-scalr" % "1.1.2"

// To simplify the reverse routing we can import the digiPlant namespace
TwirlKeys.templateImports += "se.digiplant._"

Add to conf/application.conf

# Resource plugin save directory
# is relative to app, but can be absolute to filesystem also

# Scalr image cache location (This is simply another play-res source location)
# All resized images get cached here so they only need to be resized once

# Tell scalr to use scalrcache source we just created to cache it's images

Add to conf/routes

# Dynamic image scaling
GET    /res/:width/:height/:file, width: Int, height: Int)

# Fixed image scaling
GET    /res/small/:file     , width: Int = 50, height: Int = 50)
GET    /res/medium/:file    , width: Int = 200, height: Int = 200)

# Image resizing for any folder specified as path parameter (will cache thumbnails using play-res plugin)
GET    /scalr/:width/:height/*file"/public/images", file, width: Int, height: Int)


Reverse routing to the play-res plugin backed scalr resizer

The play-res plugin generates a unique sha1 hash of the file and stores the file on disk in an optimal 4char/4char/sha1hash.ext directory structure. By only having to save the fileuid (5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg) to the db it's easy to retrieve the image or other document (see play-res documentation)

<img src=""5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg", 150, 100)" alt="" />

Reverse routing to a directory resizer

Will take the image /public/images/test/myimage.jpg and resize it to 150, 100 and store it in res/scalr/test/myimage_150x100.jpg and return the url as specified in the routes file.

<img src=""test/myimage.jpg", 150, 100)" alt="" />


By specifying a mode you can fit the image within the bounding box in different ways.

// Specify one of automatic, fit_exact, fit_to_width, fit_to_height, default is automatic"test/myimage.jpg", 150, 100, "fit_to_height")

Advanced Usage

You can specify multiple sources if you would like to split you images into categories, such as profile images / uploads. By then adding another source in conf/application.conf


you will also have to add another route

GET    /profile/:width/:height/:file"profile", file, width: Int, height: Int)