Google App Engine Ruby Image Resizing?

83 views
Skip to first unread message

Noah Hines

unread,
Oct 4, 2016, 11:20:10 AM10/4/16
to Google App Engine
Hello. I followed the Ruby on Rails Bookshelf App tutorial and built a basic web app where users can upload pictures. I would like to have server-side image resizing, so that when you get a list of all the images (from cloud datastore), you don't have to download huge versions of everything.

It looks like all the other languages have support for resizing Datastore images. Is this something that is planned for Ruby? Is there a solution that will work for me in the meantime?

I've explored using ImageMagick, but that would require installing a binary and I'm not sure if that's possible or a good idea.

If there's a known solution, I'd love to hear it!
Thanks.

Nick (Cloud Platform Support)

unread,
Oct 6, 2016, 2:00:04 PM10/6/16
to Google App Engine
Hey Noah,

Since the Ruby runtime runs on the Flexible Environment, you can use runtime: custom in app.yaml and supply a Dockerfile (in which the first line will be "FROM gcr.io/google_appengine/ruby") (see here a link to the github page of the Ruby runtime Docker image) which will install ImageMagick.

To install ImageMagick in the Dockerfile, you'd probably want to create a script that the Dockerfile runs, which will group together the many commands needed to install the dependencies and build from source. The script would look something like the following:

#!/bin/bash

apt-get -y install \
     build-essential \
     checkinstall \
     libx11-dev \
     libxext-dev \
     zlib1g-dev \
     libpng12-dev \
     libjpeg-dev \
     libfreetype6-dev \
     libxml2-dev

apt-get -y build-dep imagemagick

wget http://www.imagemagick.org/download/ImageMagick.tar.gz

tar -xzvf ImageMagick.tar.gz

cd ImageMagick*

./configure

make

make install

... and the Dockerfile would include lines like:

ADD . /app/
RUN chmod +x /app/install-imagick.sh
RUN /app/install-imagick.sh

This will make ImageMagick available on the VM which runs your Ruby process. You can then call the program from Ruby using any of the various methods by which this can be done (see here a blog post referencing 5 different ways to run commands from Ruby).

Although you could run ImageMagick to resize images dynamically on each request, this is not the most efficient way to go about it if all you require are thumbnails.

For thumbnails, I would recommend, whenever an image is uploaded, creating a process that will use ImageMagick to create and store a resized version for future use. You can use Cloud Pub/Sub to create "task queues" in this way. The basic steps would be:

1. Receive the image upload and store the full image in Cloud Storage
2. Push a message to the Pub/Sub topic you created to manage this workflow (give it a name like "thumbnail-creation")
3. Have a pool of instances with ImageMagick installed who will subscribe to this topic
4. When an instance receives a request corresponding to the published message, have it download the image, and then resize and store the image as a thumbnail

I hope this is helpful, let me know if you have any further questions; I'll be happy to assist!

Cheers,

Nick
Cloud Platform Community Support

Nick (Cloud Platform Support)

unread,
Oct 6, 2016, 2:19:53 PM10/6/16
to Google App Engine
Just a quick addition: you will probably need to add 'ldconfig /usr/local/lib' to the end of the install script. I saw errors without this when testing the install.
Reply all
Reply to author
Forward
0 new messages