Tomi Hiltunen

Enthusiastic app developer in Google Go & HTML5

maanantai 18. maaliskuuta 2013

Optimizing images on GAE blobstore using Go

So your app allows users to post image files to the App Engine blobstore?

About App Engine blobstore

Blobstore is a service provided by Google App Engine for storing files or "blobs". The blobstore provides an easy method for allowing users upload files to your app. On this article I will focus solely on image files and how to optimize them after they are uploaded to the blobstore.

Reasons for optimizing the images

Optimizing the images may mean scaling down the image dimensions and changing the compression rate. This affects the image's file size. Smaller file size means lower storage costs and shorter download times. Shorter download times make happy users.

How to do it

Please note: I expect that you already know the basics of using blobstore for uploading files!

Step 1) Download my image optimizer package from GitHub

Step 2) Import the package to your project

import "github.com/tomihiltunen/gae-go-image-optimizer"

Step 3) Where you would normally call "blobstore.ParseUploads()"

// Create settings for the optimization.
optimizerOptions := optimg.NewCompressionOptions(r) // r *http.Request

// Set maximum image dimension
// 0 = no change (default)
optimizerOptions.Size = 1600

// Set image quality
// 100 = no compression (defaults to 75)
optimizerOptions.Quality = 75

// Call the ParseBlobs method.
// Return values are similar to blobstore.ParseUploads().
blobs, other, err := optimg.ParseBlobs(optimizerOptions)

// Do the rest
...

Considerations

Reading and writing to the blobstore is a rather lengthy operation. Optimizing images immediately during upload process will make your app appear slow to users. In this case the image would be first uploaded to the blobstore, the request redirected to your app's handler, image read from the blobstore, optimized and finally, the new image is written to the blobstore and the original one deleted. As you know, responsiveness of your app affects directly the experience of using your app.

One option for eliminating the slow response times is to have a task queue for the optimization process. When the blob is uploaded your handler will just add the object's and blob's keys to the task queue. When the queue has finished the optimization it will simply replace the blobkey to the objects properties. The old blob can be used as the image until the task queue has finished the optimization.

Optimizing on the client-side

If you do not need server-side optimization, you can use plugin like Plupload which has the options for optimizing image dimensions and compression rate before uploading the image to the server. Benefits of this approach is that the upload time is shorter and you don't have to use your server resources for the optimization process as you can make the client do the heavy lifting.

Ei kommentteja:

Lähetä kommentti