RMagick and Tempfile and StringIO

I’ve got a file upload that uploads an image to a server. That image is
then manipulated via rmagick.

I’ve only just found that for files less than 10KB in size, that the
object is not a Tempfile, but a StringIO. If it’s a Tempfile (e.g.
image file is over 10KB) the site works fine, but for small images, it
fails!

Thing is, that I can’t figure out how to get a StringIO into RMagick to
create an imagemagick image, so that I can manipulate it.

Can anyone help please?

Paul

PJ Net Solutions Ltd
0871 223 5370
http://www.pjnetsolutions.com
Registered Number: 4131671
Registered Address: 35 Ballards Lane, London, N3 1XW
Office Address: The Hub, 3-5 Rickmansworth Road, Watford, Herts, WD18
0GX

I had EXACTLY this problem when I started using RMagick.

What I do is always call f.read on whatever I get back. For instance,
my form has this in it:

<% form_for :avatar, @avatar,
:url => { :action => ‘create_avatar’, :id => @toon },
:html => { :multipart => true } do |f| %>

Image
<%= f.file_field :image_file %>

<%= submit_tag “upload” %>
<% end %>

This means my image goes into “avatar_file” in my controller. I then
call something like this in toon_controller:

def create_avatar

@avatar = @toon.avatars.create(params[:avatar])

end

I think the f.rewind and f.read trick here are the key. From my avatar
model:

def image_file=(f)
f.rewind
self.image_binary = f.read
end

def image_binary=(data)
imglist = Magick::Image.from_blob(data)
img = imglist.first
if img.columns > 75 || img.rows > 75
img.crop_resized!(75, 75, Magick::NorthGravity)
end
img.format = ‘PNG’
self.image = Base64::encode64(img.to_blob)
self.format = ‘image/png’
self.width = img.columns
self.height = img.rows
GC.start
end

def image_binary
Base64::decode64(self.image)
end

What this mess does is hide my ‘image’ field. ‘image’ is always
SQL-safe Base64 encoded, while image_binary is the undecoded version.
I won’t let people store more than the correct size in my database, so
I adjust it on input and just base64 decode it on output. There are
times where I want the actual base64 version (email springs to mind)
but usually I just refer to image_binary and send that.

–Michael

Michael G. wrote:

I had EXACTLY this problem when I started using RMagick.

Brilliant. Just what I needed. Sorted it now.

Thanks

Paul


PJ Net Solutions Ltd
0871 223 5370
http://www.pjnetsolutions.com
Registered Number: 4131671
Registered Address: 35 Ballards Lane, London, N3 1XW
Office Address: The Hub, 3-5 Rickmansworth Road, Watford, Herts, WD18
0GX

Paul J. wrote:

Michael G. wrote:

I had EXACTLY this problem when I started using RMagick.

Brilliant. Just what I needed. Sorted it now.

Thanks

Paul


I had the same issue… i solved by identifying whether the incoming
uploaded file is an “image”…?

is_image = (params[:image][:data].content_type.strip()[0…4] == ‘image’)

then i manipulate the object accordingly.
But i got another strange behavior…!! In IE when i upload a gif image
of 2.60KB, it’s mime-type is seen as “image/pjpeg” ?? But when i upload
the same image in FF it’s correctly seen as “image/gif”!!!

I need to restrict the image-upload to png/jpg type of images… now i
don’t know how to sort out this issue…

Kindly help me to overcome this weird behavior in IE…