develop with

Add Image Processing to Heroku buildpack

Process images in your heroku application

In this tutorial, we’ll add the image_processing library to a heroku build pack. The libgd is a native extension that is used for processing images in ruby.

Setup: Take a look at [setting up a custom build pack](/how-do-i/ruby/setup-custom-ruby-buildpack.html) article.

First, create an S3 bucket to store the binary library for use in your build pack. Let’s call it my-heroku-binaries. In the lib/language_pack/base.rb, add a variable for your binary bucket.

MY_BINARY_URL = 'https://s3.amazonaws.com/my-heroku-binaries'

Build the binary for libgd2 using vulcan. See Ryan Daigle’s tutorial on building with vulcan. Download the binary and put it in your bucket.

In the compile method in lib/language_pack/ruby.rb add a call to the install_libgd method.

def compile
    Dir.chdir(build_path)
    remove_vendor_bundle
    install_ruby
    install_jvm
    setup_language_pack_environment
    setup_profiled
    install_libgd
    allow_git do
      install_language_pack_gems
      build_bundler
      create_database_yml
      install_binaries
      run_assets_precompile_rake_task
    end
    super
  end

Create the method below:

# Install libgd2 headers and dev libs. Cedar has the primary lib already.
# Unfortunately we can't install to /usr, so symlink all of the things.
def install_libgd
  dir = File.join('vendor', LIBGD_VENDOR_PATH)
  FileUtils.mkdir_p dir
  Dir.chdir(dir) do
    run("curl #{MY_BINARY_URL}/libgd2-noxpm-dev-stripped_#{LIBGD_VERSION}_amd64.tar.gz -s -o - | tar xzf -")
    run("ln -fs /usr/lib/libgd.so.2.0.0 lib/libgd.so")
  end
end

The Ubuntu Lucid-based dyno image includes the libgd shared library in /usr/lib, but not the headers which are required to compile the image_processing library. So the Ubuntu libgd2-noxpm-dev (2.0.36~rc1~dfsg-3.1ubuntu1) package was transformed to a tarball that’s downloaded and inserted into the slug at /app/vendor/libgd during compilation.

comments powered by Disqus

Want to see a topic covered? create a suggestion

Get more developer references and books in the developwith store.