Rails is Bailing out in RMagick…

Maybe I’m a fool for putting my RMagick into a Rails controller and not a model. We’ll see, later. After I adjust things a bit and try the model approach.

Here the code as a stand alone script:

# RMagick Test
require 'rubygems'
require 'rmagick'
include Magick

puts "Enter the name of the file to thumb:"
image_to_alter = gets.chomp
img = Image.read(image_to_alter)[0]

thumbnail_height = 100
thumbnail_width = 100

geometry_obj = Geometry.new(thumbnail_width, thumbnail_height, nil, nil, '!')
chg_geom_img = img.change_geometry(geometry_obj) {|cols, rows, image| image.resize(cols, rows)}
chg_geom_img_name = 'thumbnail_' + image_to_alter#@basename_of_f

chg_geom_img.write(chg_geom_img_name)

Here is the original image:
buddagold.jpg

Here is the image after resizing with my Ruby script:

thumbnail_buddagold.jpg

Here is the version that was resized using almost the identical code, from within a Rails app:
rails_thumbnail_buddagold.jpg

Screwy, huh?
Any tips/advice very welcome!

3 comments ↓

#1 Jim Jones on 07.31.07 at 2:38 am

I am experiencing the same issue with file_column and rmagick where I am getting grey areas after resizing.

Did you find any solutions to this?

Thanks in advance.

Jim

#2 Mike Laurence on 12.15.07 at 12:18 am

FYI - this issue is sometimes due to the binary field in the db_files table having a size limit smaller than the uploaded file - e.g., uploading a 100k picture to a MySQL BLOB field (which holds 65,536 bytes max). To fix it, you can alter your table to MEDIUMBLOB or LONGBLOB, and to prevent future mishaps, you can add a :limit parameter to your ‘data’ column in your db_files migration that is large (rails will automagically use MEDIUMBLOB or whatever in that case):

t.column ‘data’, :binary, :limit => 10.megabytes

I realize that this is several months after your post, but I figure it might help anyone with the same issue who stumbles across this thread via Google - like I did, when I didn’t know what was wrong :-)

#3 Adam Prall on 05.04.08 at 3:49 pm

We mostly like PHP here, but in the studio experience this issue with PHP and Ruby both, as well as in certain JPEG decoders. It usually occurs because a resizing (or other image manipulation) script is interpreting the JPEG data before it’s completely loaded, the gray boxes are the JPEG algorithm’s version of “I dunno what goes here babe!” - so build in a checksum on the loaded data versus the file data if you want to be certain the JPEG is completely loaded.

Leave a Comment