Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
== Installation

Gem: (Recommended)

gem install fleximage

# in config/environment.rb
config.gem 'fleximage'

Plugin:

./script/plugin install git://github.com/Squeegy/fleximage.git


Expand Down Expand Up @@ -49,21 +49,21 @@ Fleximage uses a simple templating engine that allows you to re-render images ex
== 1. Installation

Gem: (Recommended)

gem install fleximage

# in config/environment.rb
config.gem 'fleximage'

Plugin:

./script/plugin install git://github.com/Squeegy/fleximage.git


== 2. Activating your model

You need to let your model know it should be Fleximage-friendly. Lets say you have a model for photos.

# app/models/photo.rb
class Photo < ActiveRecord::Base
acts_as_fleximage :image_directory => 'public/images/uploaded_photos'
Expand All @@ -77,9 +77,9 @@ There are many other options for your model. Refer to the <tt>Fleximage::Model:
== 3. The upload form

Your users need a way to upload their images into your site. Here is how we might render a form to create a photo record.

# app/views/photos/new.html.erb

<% form_for @photo, :html => { :multipart => true } do |f| %>
<p>
<b>Name</b><br />
Expand All @@ -101,7 +101,7 @@ Your users need a way to upload their images into your site. Here is how we mig
<%= f.submit "Create" %>
</p>
<% end %>

*NOTE*: The ":html => { :multipart => true }" is *VERY* *IMPORTANT*. Without this snippet your browser will not send binary data to the server. If things aren't working, check to make sure you have this in your form declaration.

The relevant bit of our form code is:
Expand All @@ -119,7 +119,7 @@ Right along side the upload field is a simple text field, which maps to the "ima
You can have just one of these fields, or both. The model will know how to do the right thing either way.

When the user submits the form, all you have to do is assign the form contents to your object in standard Rails fashion, and the image is uploaded and saved for you. Creating a new photo may look like this in your controller:

# app/controllers/photos_controller.rb
def create
@photo = Photo.new(params[:photo])
Expand All @@ -137,10 +137,10 @@ When the user submits the form, all you have to do is assign the form contents t
Rails 2 has amazing support for format driven responses. Given a photo object, by default it would have an HTML view that describes information about that photo. With Fleximage, the JPG or (GIF or PNG) view can be the image data itself.

A photo HTML view may look like this:

# app/views/photos/show.html.erb
# http://mysite.com/photos/123

<p>
<%= image_tag formatted_photo_path(@photo, :jpg) %>
</p>
Expand Down Expand Up @@ -169,7 +169,7 @@ The filename of the template should look like this: <tt>action_name.jpg.flexi</t
The syntax of the view is pure ruby, but to process the image the view needs to call +operate+ on the instance of your model.

Here is the view to render a photo record at 320x240:

# app/views/photos/show.jpg.flexi
# http://mysite.com/photos/123.jpg
@photo.operate do |image|
Expand All @@ -188,7 +188,7 @@ Here is a .+flexi+ template that will do much more:
:offset => '20x20'
image.border :size => 10, :color => 'green'
image.text 'I like Cheese'
image.unsharp_mask
image.unsharp_mask
image.shadow
end

Expand Down Expand Up @@ -221,7 +221,7 @@ You don't want to render JPGs? That's fine. Just link to the format you want (

# app/views/photos/show.html.erb
<%= image_tag photo_path(@photo, :gif) %>

# app/views/photos/show.gif.flexi
@photo.operate do |image|
@photo.resize '150x150', :crop => true
Expand All @@ -232,7 +232,7 @@ The Fleximage template engine will automatically detect the format that is being
== Converting/Upgrading your master image store

Are you upgrading your live app to the new file store creation date based format? Did you start out with PNG image storage, and later realize you need to store with the more space economic JPG instead? Not problem, Fleximage provides some rake tasks to help you out.

Each conversion rake task requires that you tell it the class for which that you are changing the file store. For example, if you want to change to the new creation date based storage structure, for the class +Photo+, you can run a rake task like this:

rake fleximage:convert:to_nested FLEXIMAGE_CLASS=Photo
Expand Down
38 changes: 19 additions & 19 deletions lib/fleximage/aviary_controller.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
module Fleximage

module AviaryController
def self.api_key(value = nil)
value ? @api_key = value : @api_key
end

def self.api_key=(value = nil)
api_key value
end

# Include acts_as_fleximage class method
def self.included(base) #:nodoc:
base.extend(ClassMethods)
end

module ClassMethods

# Invoke this method to enable this controller to allow editing of images via Aviary
def editable_in_aviary(model_class, options = {})
unless options.has_key?(:secret)
raise ArgumentError, ":secret key in options is required.\nExample: editable_in_aviary(Photo, :secret => \"My-deep-dark-secret\")"
end

# Don't verify authenticity for aviary callback
protect_from_forgery :except => :aviary_image_update
protect_from_forgery :except => :aviary_image_update

# Include the necesary instance methods
include Fleximage::AviaryController::InstanceMethods

# Add before_filter to secure aviary actions
before_filter :aviary_image_security, :only => [:aviary_image, :aviary_image_update]

# Allow the view access to the image hash generation method
helper_method :aviary_image_hash

# Save the Fleximage model class
model_class = model_class.constantize if model_class.is_a?(String)
dsl_accessor :aviary_model_class, :default => model_class
dsl_accessor :aviary_secret, :default => options[:secret]
end

end

module InstanceMethods

# Deliver the master image to aviary
def aviary_image
render :text => @model.load_image.to_blob,
:content_type => Mime::Type.lookup_by_extension(self.class.aviary_model_class.image_storage_format.to_s)
end

# Aviary posts the edited image back to the controller here
def aviary_image_update
@model.image_file_url = params[:imageurl]
@model.save
render :text => 'Image Updated From Aviary'
end

protected
def aviary_image_hash(model)
Digest::SHA1.hexdigest("fleximage-aviary-#{model.id}-#{model.created_at}-#{self.class.aviary_secret}")
end

def aviary_image_security
@model = self.class.aviary_model_class.find(params[:id])
unless aviary_image_hash(@model) == params[:key]
render :text => '<h1>403 Not Authorized</h1>', :status => '403'
end
end

end
end

end
20 changes: 10 additions & 10 deletions lib/fleximage/blank.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Fleximage

# The +Blank+ class allows easy creation of dynamic images for views which depends models that
# do not store images. For example, perhaps you want a rendering of a text label, or a comment,
# or some other type of data that is not inherently image based.
Expand All @@ -8,27 +8,27 @@ module Fleximage
# a new Fleximage::Blank object right in your view.
#
# Usage:
#
#
# Fleximage::Blank.new(size, options = {}).operate { |image| ... }
#
# Use the following keys in the +options+ hash:
#
# * color: the color the image will be. Can be a named color or a Magick::Pixel object.
#
#
# Example:
#
# # app/views/comments/show.png.flexi
# Fleximage::Blank.new('400x150')).operate do |image|
# # Start with a chat bubble image as the background
# image.image_overlay('public/images/comment_bubble.png')
#
#
# # Assuming that the user model acts_as_fleximage, this will draw the users image.
# image.image_overlay(@comment.user.file_path,
# :size => '50x50',
# :alignment => :top_left,
# :offset => '10x10'
# )
#
#
# # Add the author name text
# image.text(@comment.author,
# :alignment => :top_left,
Expand All @@ -40,9 +40,9 @@ module Fleximage
# :opacity => 0.5,
# }
# )
#
#
# # Add the comment body text
# image.text(@comment.body,
# image.text(@comment.body,
# :alignment => :top_left,
# :offset => '10x90',
# :color => color(128, 128, 128),
Expand All @@ -52,18 +52,18 @@ module Fleximage
class Blank
include Fleximage::Model
acts_as_fleximage

def initialize(size, options = {})
width, height = Fleximage::Operator::Base.size_to_xy(size)

@uploaded_image = Magick::Image.new(width, height) do
self.colorspace = Magick::RGBColorspace
self.depth = 8
self.density = '72'
self.format = 'PNG'
self.background_color = options[:color] || 'none'
end

@output_image = @uploaded_image
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/fleximage/helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Fleximage
module Helper

# Creates an image tag that links directly to image data. Recommended for displays of a
# temporary upload that is not saved to a record in the databse yet.
def embedded_image_tag(model, options = {})
Expand All @@ -9,20 +9,20 @@ def embedded_image_tag(model, options = {})
mime = Mime::Type.lookup_by_extension(format.to_s).to_s
image = model.output_image(:format => format)
data = Base64.encode64(image)

options = { :alt => model.class.to_s }.merge(options)

result = image_tag("data:#{mime};base64,#{data}", options)
result.gsub(%r{src=".*/images/data:}, 'src="data:')

rescue Fleximage::Model::MasterImageNotFound => e
nil
end

# Creates a link that opens an image for editing in Aviary.
#
# Options:
#
#
# * image_url: url to the master image used by Aviary for editing. Defauls to <tt>url_for(:action => 'aviary_image', :id => model, :only_path => false)</tt>
# * post_url: url where Aviary will post the updated image. Defauls to <tt>url_for(:action => 'aviary_image_update', :id => model, :only_path => false)</tt>
#
Expand All @@ -33,9 +33,9 @@ def link_to_edit_in_aviary(text, model, options = {})
post_url = options.delete(:image_update_url) || url_for(:action => 'aviary_image_update', :id => model, :only_path => false, :key => key)
api_key = Fleximage::AviaryController.api_key
url = "http://aviary.com/flash/aviary/index.aspx?tid=1&phoenix&apil=#{api_key}&loadurl=#{CGI.escape image_url}&posturl=#{CGI.escape post_url}"

link_to text, url, { :target => 'aviary' }.merge(options)
end

end
end
Loading