My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
UsingTheImagesService  
Images service example
Updated Dec 1, 2010 by necrod...@gmail.com

The MIT License

Copyright (c) 2009 Brian W. Gibson http://brianwgibson.com/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Google's trademarks and other brand features are not included in this license. Please see Google's standard guidelines for third party use of Google brand features for information about this usage.


Gemfile

# Critical default settings:
disable_system_gems
disable_rubygems
bundle_path ".gems/bundler_gems"

# List gems to bundle here:
gem "dm-appengine"
gem "sinatra"

config.ru

require 'appengine-rack'
require 'image'

AppEngine::Rack.configure_app(
  :application => 'image',
#  :precompilation_enabled => true,  
  :version => 1)

run Sinatra::Application

image.rb

require 'sinatra'
require 'appengine-apis/images'

module AppEngine
  module Images
    class Image
      def scale(factor)
        # make sure width and height are > 0 
        resize([(width * factor).to_i, 1].max, [(height * factor).to_i, 1].max)
      end

      def clip(factor)
        crop(factor, factor, 1.0 - factor, 1.0 - factor)
      end
    end
  end
end

enable :sessions

get '/' do 
  session['transforms'] ||= ''
  # split transforms into array using dots as the delimiter if it's followed by a transform command
  @transforms = session['transforms'][1..-1].split(/\.(?=rotate|flip|scale|clip)/) rescue []
  erb :index
end

get '/transform' do
  session['transforms'] << case params[:t]
    when 'turn_cw' then '.rotate(90)'
    when 'turn_ccw' then '.rotate(-90)'
    when 'flip_h' then '.flip(:horizontal)'
    when 'flip_v' then '.flip(:vertical)'
    when 'double' then '.scale(2)'
    when 'halve' then '.scale(0.5)'
    when 'clip' then '.clip(0.1)'
    else ''
  end
  redirect '/'
end

get '/reset' do
  session['transforms'] = '' 
  redirect '/'
end

get '/image' do
  image = AppEngine::Images.open('public/gae.jpg')

  # apply the string of transforms if available, remembering to remove the leading dot 
  image = image.instance_eval(session['transforms'][1..-1]) unless session['transforms'].empty?

  content_type :jpeg
  image.data
end

views/index.erb

<html>
<head>
    <title>Images Service Demo</title>
</head>
<body>

<img src="/image" />

<ul>
<% @transforms.each do |t| %>
    <li><%= t %></li>
<% end %>
</ul>

<p><a href="/transform?t=turn_cw">Turn CW</a> | 
<a href="/transform?t=turn_ccw">Turn CCW</a> | 
<a href="/transform?t=flip_h">Flip H</a> | 
<a href="/transform?t=flip_v">Flip V</a> |
<a href="/transform?t=double">Double Size</a> | 
<a href="/transform?t=halve">Halve Size</a> |  
<a href="/transform?t=clip">Clip</a> |  
<a href="/reset">Reset</a></p>

</body>
</head>

public/gae.jpg (not licensed)

Comment by kaabo...@gmail.com, Mar 5, 2010

must be changed Appengine >> AppEngine?


Sign in to add a comment
Powered by Google Project Hosting