New FlexImage website and repository
June 20th, 2007
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!
June 28th, 2007 at 11:05 PM
Not sure this is the right place for this anymore – but i have a question about rotating images. I looked through the model.rb code and I am trying to do a rotation of a flex_image using the rmagick rotate method. I have the following code
is this the right approach? if so , somehow it does not work (any thoughts?). the rotated image does not seem to be saved.
is this something that we should look at expanding the flex_image with (similar to resize) – if i am off track let me know.
Anyway, the work you have done on flex_image is certainly appreciated.
June 28th, 2007 at 11:05 PM
I got the above working by putting a rotate method in the model
class Photo < FlexImage::Model def rotate # a request to rotate the picture 90 @rmagick_image = Magick::Image.from_blob(self.data).first @rmagick_image.rotate!(90) @blob = @rmagick_image.to_blob write_attribute('data', @blob) save endsorry to keep posting but this might be interesting to others (e.g., somewhat nubies like myself). If there is a better forum let me know.
thanks,
June 28th, 2007 at 11:05 PM
If you want to implement your own image processing methods, the best way to do that is like so:
class MyImage < FlexImage::Model def fooify! img = rmagick_image # do stuff to img self.rmagick_image = img self end endThat’s how all the existing image processing methods work.
It’s important to reassign the processed image to
self.rmagick_imageand it’s important the method returnsselfso that it is handled properly by the templates renderer.This may be a good thing to have added to the plugin itself. I’ll look into it.
June 28th, 2007 at 11:05 PM
Thanks, I have tried what you suggested and the image is correctly rotated. I had to to a save after the call to fooify! in the controller to get the db to be updated.
June 28th, 2007 at 11:05 PM
Hi Alex.
Great plugin. Very easy to use.
I have a concern about performance. The images are all saved into one directory. On a site with a lot of photo uploads it will be a problem. I think, depending on the OS and file system, it is not good idea to have many thousands of files in a single directory.
Do you know any solutions for this issue?
Thanks, Egze
June 29th, 2007 at 12:56 AM
Egze: I have not encountered this problem yet, so haven’t developed a fix for it. But I happily accept patches :)
Just put them in a pastie and link them here.
July 14th, 2007 at 11:36 AM
Thanks for the pluggin!
My problem:
I’m storing my images in the file system. I have a few methods to format pics for different pages. So, I set up named routes for each. But, each named route has the same path to my images directory. Each named route works, but it only processes to the first named route action. Only the thumbnail sizes work unless I put the photo map first… then only that size works.
map.thumbnails ’/photos/:id.jpg’, :controller => ‘Photo’, :action => ‘thumb’
map.photo ’/photos/:id.jpg’, :controller => ‘Photo’, :action => ‘profile’
Thx, Bob
July 17th, 2007 at 12:19 PM
Hi Alex,
I have some questions on installation of FlexImage/RMagick. Im using Windows for development of my rails app before I upload to a Linux/Unix server that has RMagick. However, I am limited in what version of RMagick I can use for windows. In the link http://rmagick.rubyforge.org/install-faq.html#win it says the installation for Windows is called rmagick-win32 with an older version of RMagick and it comes with ImageMagick. I assume ImageMagick is not needed since FlexImage replaces that. Is that correct? Will FlexImage work with the rmagick-win32 version and is there a conflict with ImageMagick? Thanks.
-Richard
July 22nd, 2007 at 11:33 PM
Richard,
FlexImage should work with whatever the latest RMagick for windows version is. And ImageMagick is required, for RMagick. And RMagick is required for FlexImage. So ImageMagick must be installed.
FlexImage is a Rails plugin that makes uploading serving images easier. ImageMagick and RMagick are libraries for processing images that FlexImage depends on.
I hope that clears up the confusion.
July 22nd, 2007 at 11:39 PM
R Dunne,
Well those routes just won’t work, with FlexImage or any other Rails actions.
I usually use something like:
But there is no way in rails routes to have 2 identical routes, that go to different actions. Rails has no way to know which you want.
July 30th, 2007 at 08:04 AM
Hi Alex,
after upgrading to Revision 8 of FlexImage all worked nearly well in production mode. I had several classes where there the imagepath was overwritten and wrong images showed up.
Now I have the problem that a definition like that does not work anymore:
class ShipmentImageController < ApplicationController flex_image :action => ‘show’, :class => ShipmentImage, :overlay => { :file => ‘images/watermark.png’, :alignment => :bottom_right, :offset => 10 } end
Image shows up, and log is ok, but there’s no watermark seen on my image. What’s going wrong?
July 30th, 2007 at 01:07 PM
You should be using a .flexi template instead the flex_image macro. You can see just how to that towards the bottom of this page:
http://fleximage.rubyforge.org/install.html
You should need a rendering template that looks like:
I think whats happening is the deprecated macro has not been updated to perform the overlay operation. Use a .flexi view as described above and it should be no problem.
August 5th, 2007 at 10:57 AM
Hmm, noob question…
I can do all of the resizing, cropping, and border methods, but for some reason the overlay and text methods don’t work at all.
With @photo.overlay!... I get: undefined method ‘cur_image’ for photos/logo.png PNG 35×34 35×34+0+0 DirectClass 8-bit 3kb:Magick::Image
And with @photo.text!... I get: undefined method ‘annotate’ for 333×250 DirectClass 8-bit:Magick::Image
Any advice?
August 5th, 2007 at 09:38 PM
My hunch is that your ImageMagick/RMagick combo is out of date. Make sure you have the latest versions of both installed.
August 6th, 2007 at 01:34 PM
I’m using the one that came in the installer for windows. It says throughout the documentation that I must use the version that comes with the installer, and not to update to the newest version, or it won’t work.
So, am I out of luck with those features until I migrate everything over to my linux/apache server? That would kinda suck, since the reason I chose this plugin over the others was for the overlay and text features :(
August 7th, 2007 at 06:20 AM
Hi there,
Thanks for the wonderful plugin. Everything’s great so far. But I have one question, is it possible to determine the file size of the image being uploaded, ‘BEFORE’ it is even uploaded ? I know that pre_process_image will resize the image to the size we want. But, I have a thought that this resizing process is done ‘AFTER’ the file is creatr.
Thanks In Advance !
August 11th, 2007 at 05:08 PM
@Steve If RMagick does not support it, than the plugin will not for sure. Sorry bud. It has been a long time since I used my windows RMagick install.
@Chris: There is no way to do what you ask. Rails wont even begin processing the request until it has been fully sent. This means that the uploaded data has to make it to the server before you can do anything with it. And you would need that data anyway to find out what size the image is. Sorry bud.
August 27th, 2007 at 01:49 PM
Hey Alex,
I have a form partial that creates a new database record with an uploaded image file and some meta data about the image.
I want to use the same form partial for edits, if I want to change the meta data or upload a new image.
However, I’m getting this error when I try to change the metadata without specifying a file to upload:
“Uploaded file contains no binary data. Be sure that {:multipart => true} is set on your form.”
I have specified :multipart => true in the form. How can I use the same form partial and not have to specify an image file, if I don’t want to?
Thanks,
David
August 28th, 2007 at 10:31 PM
Hi Alex (and all),
One question.
I am working on a Rails-based multisite sort-of-portal system, where each site in the system uses the same core (engine/database/mongrel_cluster), and the content which is output to user based on access url (host name). Each site also has its own templates so it is possible to have absolutely different design for each of them.
Currently, I use FlexImage to handle image processing. My usage pattern is in a form of ( /image/<size_type>/<image_id> ). So I can have /image/show/123.jpg and /image/small/123.jpg to render big and thumb versions of the same image.
Now, there’s a problem I have faced and not sure if it is possible to resolve without switching from FlexImage to hand-written processor. What I would like to be able to do is to set different settings for size_types, depending on which site in the system is accessed, retaining commonly used size_types. Sort of http://site1.com/image/thumb/123.jpg would render 50×50 thumb, and http://site2.com/image/thumb/123.jpg would render a 100×100 thumb.
I was sort of able to achieve this effect by class_eval’ing flex_image macros in my ImageController in development mode. But everything is being cached in production mode, so the real-time class_eval in controller just doesn’t work this way.
What I think would potentially work for me is to use .flexi templates, but being able to set default path for template inclusion (so, say, .flexi templates are fetched not from default location, but from layouts/sites/<site_code>/flexi/<action>.flexi ).
I wonder if it is possible at all.. or may be I’m just have a brain black-out and moving in a totally wrong direction? I’m not as good at lower-level Rails as I would like to be, yet.. :(
August 29th, 2007 at 02:30 PM
Hi,
Not sure if you got my earlier question, but I need some help with FlexImage. I’m storing images in a database record along with other metadata. I can create the records fine. However, when I try to edit the record and change some of the metadata without specifying an image to upload, I receive an error from FlexImage that multipart => “true” might not be set.
Is there a way to use a form to edit data and optionally specify another image to upload?
Thanks,
David
September 1st, 2007 at 09:00 AM
@David The answer to your question is a little funky. The problem is that, in general, a Rails model expects all its data to be fed back into it when doing an update. But this is not possible with binary data uploaded to a file since the browser cannot put the existing data in the file upload field.
The solution I have used is something like:
So we see if a file was uploaded, and if there was no file (indicated by the size of the file being 0), then we delete it form the params so it doesn’t mess with our existing data.
It’s worked great for me on several projects.
September 1st, 2007 at 09:15 AM
@Mike
I’m not sure if this will work on Rails 1.2.3 but I know it works on edge.
ActionCOntroller::Basehas an accessor that stores where it looks for views. By default, this isapp/views. But you can add to it.Something like:
This way, if a view doesn’t exist in a particular area of the site, it will use a global view instead.
I have never used it for .flexi views, but I don’t why it woould work any differently.
September 10th, 2007 at 06:00 PM
Hello Alex,
Thanks for a great plugin! I’m working on extending it a bit to do some color manipulations, like black & white and sepia. Here’s my method for black and white:
class Photo < FlexImage::Model ... def greyscale! image = rmagick_image image.quantize(256, Magick::GRAYColorspace) self.rmagick_image = image self end
I think this should work, because it is pretty close to the demo of greyscale from the rmagick pages: http://rmagick.rubyforge.org/portfolio.html, and it’s pretty close to your model methods. However, the quantize method doesn’t seem to do anything. Is there something obvious I’m missing?
Thanks,
Matt
September 11th, 2007 at 08:01 AM
I found the problem with the above post. Here’s a greyscale function you can add to FlexImage if you like! Sepia toning coming soon!
class Photo < FlexImage::Model
...
def greyscale! end
September 14th, 2007 at 03:05 PM
Hi Alex,
I am interested in storing images in a folder other than public since some photos may be private and be seen only to certain users. I am also concerned about speed and I am not sure if page caching would work for what I want since these images can be accessed in the public directory. Let me know if that is true. Maybe there is way to cache files so it is not easily accessible?
To solve this problem I was wondering if FlexImage can process two images, a thumbnail and regular photo both within a certain size, stored in a folder when it is uploaded to the server. Also when the webpage loads these pictures it would bypass the flex_image call so that the server would not have to do any image processing or would call the flex_image only to load the image without any processing. Can this be done and how?
If you have any other suggestions on how to maintain privacy of images and yet have speed when loading that would appreciated. Thanks.
-Richard
September 17th, 2007 at 12:07 AM
Richard:
Page caching makes that hard. And page caching is the only kind of caching that really works well with dynamic images. I can see a work around though, its just a bit convoluted.
1) Create an after with custom caching behavior. Something like:
3) Your controller actions check for the private cache, with something like:
3) Create a custom cache clearer to clear your custom cache when things change.
Should work. I’m sure it could be refactored into something cleaner though.
September 18th, 2007 at 11:24 AM
Thanks Alex,
Wow this sounds great that a custom made caching can be made. I am not familar with caching except only what I read in the Agile Rails book with the discussion of page and action caching.
I should be able to see some performance difference even in development mode right? Out of curiosity can one do standard page/action caching in development mode? I tried and it did not seem to work.
For your code in the cache_image method you had f.write response.body. I know response is an attribute of the ActionController::Base but I could not find on the rails API what body is. What does that do and where can I find that on the rails api? Also I assume you meant request_uri not request.uri.
Also, if I have a page that have several images using a rendering of partials then this should still work. I just need to add the id of the photo in the string cache variable. Let me know if that is not true.
Should I cache individual images during upload process? I realize that a person who opens for the first time the page with several images (several thumbnails and a large pict) it would take a performance hit. I assume subsequent calls to the page will be fine since it would be cached.
Thanks again. You have been a tremendous help.
-Richard
September 19th, 2007 at 08:16 AM
Richard:
I should be able to see some performance difference even in development mode right?
When you use rails’ built in caching methods, they only work in production by default. If you want to enable caching in development mode, edit config/environments/development.rb and change config.action_controller.perform_caching to true. But it’s best to not develop in this mode this your changes wont always be reflected. And also, if you write your own custom cacher as stated above, this value is ignored and will work everywhere unless you make it aware of what environment its running in.
For your code in the cache\_image method you had f.write response.body.
In an after_filter, there is always a local variable “response” which holds the information, data, headers, etc of the response that was just sent to the browser. I believe it is an instance of Net::HTTP. So when requesting an image served by rails, response.body contains all the binary data that makes that image.
Also I assume you meant request\_uri not request.uri
Indeed. Or more accurately request.request\_uri
Also, if I have a page that have several images using a rendering of partials then this should still work.
Remember that your browser make a separate request for each image, no matter how many are on the page. So each image will need a unique url anyway. Simply use that unique url as their cached filename however you need to.
Should I cache individual images during upload process?
There is no way built into FlexImage to do this, and you may want to if its a huge amount of stuff. But in my projects so far, it has simply not been necessary.
November 29th, 2007 at 09:59 AM
Do you have plans to update the documentation soon? I’m having a bunch of trouble getting this to work properly. I posted a request for help in the FlexImage forums on RubyForge, but there is zero activity there.
February 5th, 2008 at 03:28 PM
Thanks for all your efforts in building this plugin. I can’t wait to start using it. However I’ve ran into some problems installing this into my RoR app. First some system info: I am running Ubuntu 7.10 “Gutsy Gibbon” Desktop Ruby version 1.8.6 Rails version 2.0.2 ImageMagick 6.3.8 RMagick 2.2.0
I had some problems installing ImageMagick, but I found a website that had a easy step-by-step instructions that seemed to have worked: http://www.urbanpuddle.com/articles/2008/01/22/compile-imagemagick-for-rmagick-2-0-0-gem
Next, I installed the Flex Image plug in as per your instructions: ruby script/plugin install http://beautifulpixel.com/svn/plugins/flex_image/
And here’s the part that has the error: I type in: ruby script/generate flex_image ProductPhoto
And it outputs: /usr/lib/ruby/gems/1.8/gems/rmagick-2.2.0/lib/RMagick2.so: /usr/lib/ruby/gems/1.8/gems/rmagick-2.2.0/lib/RMagick2.so: undefined symbol: SketchImage – /usr/lib/ruby/gems/1.8/gems/rmagick-2.2.0/lib/RMagick2.so (LoadError) from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’ from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require’ from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in `new_constants_in’ from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require’ from /usr/lib/ruby/gems/1.8/gems/rmagick-2.2.0/lib/RMagick.rb:11 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:32:in `gem_original_require’ from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:32:in `require’ from /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:496:in `require’ ... 17 levels… from /usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/commands/generate.rb:1 from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require’ from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’ from script/generate:3
I’m a Unix/Ubuntu/RubyOnRails newbie, so please be patient with me. Let me know if you need any more background info.
February 13th, 2008 at 05:31 PM
Hey,
I’ve been having some issues with the text! method.
Here’s my .flexi: @image.resize! :size => ‘300×300’ @image.text! @image.credit, :alignment => :bottom_right, :color => “white”, :position => “2”
The idea is to watermark the image with the owner’s name before displaying it. The only problem is that the text! method spits out a blank black image with the text in the corner—the original image data seems to have been lost.
Any suggestions?
Rails 2.0.2 and Rmagick 1.15.12
February 20th, 2008 at 05:47 PM
I’m with Carl, I get the same problem using the flex_image plugin on my hosting server.
(Local – Rails 1.2.3, Rmagick 1.15.9)
(Host – Rails 1.2.3, Rmagick 1.15.10)
February 21st, 2008 at 11:28 AM
I have encountered this before. It’s most likely due to an RMagick or ImageMagick version that is too old. I’m not currently sure what version you need. See if you can update it on you local machine, and if it starts working you know that was it.
I haven’t tested it, but I am pretty sure that FlexImage won’t work with RMagick 2.0, so be wary of that.
I am in the process of rewriting this plugin from the ground up, since, to be honest, its a bit of a mess. Hopefully issues like these will go away once that is complete.
February 21st, 2008 at 11:34 AM
@Blair: I’m not sure how to help. It looks like RMagick and it’s dependencies are not installed properly. RMagick can be a real pain to get working right. Best of luck and sorry I can’t help more.
February 21st, 2008 at 11:36 AM
My local XP box works fine with the older Rmagick version (sorry I wasn’t clearer). The Linux hosting server spits out the black image.
I’ll let you know if I find anything else out.
March 3rd, 2008 at 03:49 PM I think it is an old ImageMagick version that’s causing the problem. I hacked the file a bit to work around it:
# draw text on transparent image temp_image = Magick::Image.new(image.columns, image.rows) { self.background_color = Magick::Pixel.new(0, 0, 0, 255) }I changed it to this:# draw text on transparent image temp_image = Magick::Image.new(image.columns, image.rows) { self.background_color = 'none'}March 25th, 2008 at 03:54 AM
Hello All,
Anyone has a problem after upgrading to rails 2.0.2 from 1.x?
My .flexi views are being sent as text to the browser instead of an image.
Any idea? Gokhan
March 25th, 2008 at 03:54 AM
Hello All,
Anyone has a problem after upgrading to rails 2.0.2 from 1.x?
My .flexi views are being sent as text to the browser instead of an image.
Any idea? Gokhan