Render Fleximage inline

May 7th, 2008

When you write your plugins the right way, Rails just works in nice ways for you. madrobby (a.k.a. Thomas Fuchs, as made famous by script.aculo.us javascript awesomeness), provided a useful tip in the Fleximage wiki on GitHub.

If you have some quick and small templates for a simple resize operation, you can do a render :inline in order to create a template on the fly for quick and easy rendering of a small template. Try this in your controller:

1
2
3
4
def thumb
  @photo = Photo.find(params[:id])
  render :inline => "@photo.operate {|p| p.resize '100x100'}", :type => :flexi
end

I have never used an inline template before, but I can definitely see how that could be handy. Although, to be honest, I feel like this is very much view logic and would like to see this in a view file in all but the absolute simplest cases.

Thanks for the tip Thomas!

Fleximage is "Top Rated"

April 21st, 2008

According to AgileWebDevelopment.com Fleximage is in the first 15 “top rated” rails plugins on their site. Currently it sits at 5/5 with 71 votes. Not bad at all!

If you use it, and like it, go give it a thumbs up.

So I know you guys are out there. If you are using the plugin on a production site, go ahead and add yourself to the Who uses Fleximage Wiki page over on Github. I would love to see what you creative have done with dynamic images.

There was a minor annoyance with all version of Fleximage up to this point. If you upload an image to a model, and validation fails for whatever reason, then you would have to find the image on your disk again, re-upload the image again, before you resubmit the form. Wouldn’t it be nice of the file stayed uploaded, even if the record isnt ready to be saved yet? How about getting a preview of the uploaded image too?

It’s all possible with the latest version of Fleximage up on GitHub. Checkout this GitHub wiki page for details.

You ever make something, and then look back a few years later and say “What the hell was I thinking”? This happened to me recently. Fleximage has been quite a useful plugin for me, but under the hood it was a mess; the API was cumbersome, and mixing in all the image transformations as direct model methods was code smell.

This release represents the 3rd complete rewrite of this code base. The first effort was SuperImage. It worked, but it happened almost entirely in the controller and made more interesting effects harder. The first Fleximage was next introducing more tranformations and .flexi views and a good effort to making it more MVC friendly.

Get Fleximage v2 here

So I now present, Fleximage v2. The output is very similar, but the API and internals are completely different. Here’s some highlights.

  • Creation date based master image storage to prevent OS imposed directory limits. i.e. /path/to/images/2008/02/06/999.jpg
  • Rails 2 formatted resource route friendly
  • Magic columns to capture filename and image size
  • Image transformation methods are not injected into your model or controller at all.
  • Image operations now happen via Operator class that refactor each transformation into their own encapsulated class.
  • Easily output JPG, PNG, or GIF based on request format.
  • Simple acts_as_* style model activation rather than using a cumbersome inheritance method
  • Side by side file upload or URL fields for easy uploading.
  • Uploading will only ever replace you image file if the uploaded file actually has content, removing the need for “if file.size > 0” nonsense in your controller.
  • Rake tasks to convert from master image directory styles, as well as master image formats (jpg => png for instance)

And it’s all hosted on GitHib. It’s new, it’s awesome, and has extensive documentation in the attached Wiki.

Lastly, if you are using Fleximage in an existing production site, please add it on the WhoUsesFleximage page. I know you’re out there.

FleursFrance.com

February 21st, 2008

I have been developing a little site for the florist of my recent wedding. It’s a simple custom designed Rails CMS. Its got some page content management, a cool gallery manager, and of course, FlexImage is used to resize images and stamp them with a custom copyright.

The main problem with the images is that some were taken by the florist herself, but other are taken by professional photographers, and proper credit needed to be given. So I provided a simple text field that will write anything to the bottom left of an image allowing custom copyright on a per photo basis. And permanently stamping it on the image meaning it can’t be stolen, at least without a nasty cropping, but there is only so much you can do against that.

It’s been a fun little project.

This information is now old. A new version of the plugin can be found here with lots of instructions and examples.

FlexImage has officially been moved to RubyForge. Find all the new Goodies here:

fleximage.rubyforge.org

New SVN: svn://rubyforge.org/var/svn/fleximage

It’s got a new getting started guide, as well as an extensive examples section.

The README is still a bit out of date and I will hopefully be updating that soon. But this new site should make it far easier to get people up and running with FLexImage the right way, and get them doing it properly, with .flexi templates and all that jazz.

Just in time for me to show this off tomorrow night at the North Bay Ruby Users Group. All you Sonoma County Rubyists may as well stop on by!

This information is now old. A new version of the plugin can be found here with lots of instructions and examples.

NOTE: Before you update your plugin, install the dsl_accessor gem.

gem install dsl_accessor

Sorry it took so long guys, but FlexImage now has full support for multiple classes with different settings. The problem with class variables (the @@foo variety) is that if they point to the exact same object in all classes that inherit from the class it was defined in. For example:

class A
  @@foo = 'A'
  def self.foo
    @@foo
  end
end
A.foo #=> "A"
class B < A
  @@foo = 'B'
end
A.foo #=> "B" 
B.foo #=> "B"

So changing @@foo in the class B, changes it in class A, as well as it’s siblings.

The solution

There is a great gem out there class dsl_accessor. It provides a way to declare inheritance safe class level variables through an elegant API. I have transitioned all class level variables in FlexImage from the old @@foo style to the new and sexy accessors.

This also means some syntactic sugar (although, still backwards compatible). Where before you had to declare these values like this:

self.file_store = 'path/to/master_images'

Now you can do any of the following:

file_store        'path/to/master_images'
self.file_store   'path/to/master_images'
self.file_store = 'path/to/master_images'

And rest assured that all attributes are safe from their parent and siblings.

Found a bug?

As always, simply post here with any problems you may have and I will assist as best I can.

Happy Flexing!

FlexImage and respond_to

November 27th, 2006

This information is now old. A new version of the plugin can be found here with lots of instructions and examples.

TomFarm has posted a very cool example about how to use FlexImage with the respond_to(format) style process handling.

NOTE: this requires rails 1.2 or edge rails

This gives you a nice RESTful solution to image display simply by file extension. You can render an image with:

/project_images/1.jpg

Or display the html with a title and caption for an image with:

/project_images/1

Or get the image attributes in xml with:

/project_images/1.xml

Here’s how:

First, you have to register the jpg extension with the Rails MIME handler.

#environment.rb
Mime::Type.register("image/jpeg", :jpg)

Make sure you are using restful routes for you image urls.

#routes.rb
map.resources :project_images

And finally create your respond_to block.


# ProjectImagesController
def show
  @project_image = ProjectImages.find(params[:id])
  respond_to do |format|
    format.jpg  { render_flex_image(@project_image) }
    format.html # show.rhtml
    format.xml  { render(:xml => @project_image.to_xml) }
  end
end

Pretty slick. Thanks Tom!

This information is now old. A new version of the plugin can be found here with lots of instructions and examples.

A bunch of super useful image processing functions has been added to the FlexImage toolbox. Now it can do more than just resize and crop an image. FlexImage now has the ability to add borders, shadows, and overlays.

=>

Update or install:

ruby script/plugin install http://beautifulpixel.com/svn/plugins/flex_image

For full documentation see the rdoc

But for the eager and impatient, here are some examples. They are posted along with the controller code that generated them.

Read the rest of this entry