Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do the temp files go when using MiniMagick in a Ruby on Rails app?

I'm using MiniMagick to perform some image resizing on images uploaded through a multi-part form. I need to generate a few different types of images from the originally uploaded file. Here's the code that's performing the image processing:

// Generates a thumbnail image
mm = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm.resize(thumbnail_dimensions.join("x"))
mm.write(Rails.root.join('public', 'uploads', "t_"+new_url))

// Generates cropped version
mm_copy = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm_copy.crop('200x200')
mm_copy.write(Rails.root.join('public', 'uploads', "c_"+new_url))

new_url is the path to the image in the public folder. The thumbnail routine works perfectly. When the app goes to start processing the cropped version, that is where things start breaking and I can't for the life of me figure it out. I receive the following error when from this code:

No such file or directory - /tmp/mini_magick20110627-10055-2dimyl-0.jpg

I read some stuff about possible race conditions with the garbage collector in Rails but I wasn't able to resolve the issue. I tried this from the console as well and can create MiniMagick instances but receive the No such file error there as well. At this point, I have no idea where to go so I'm hoping someone here has some helpful suggestions. Thanks for your help!

Details:

  • OS: Ubuntu (Lucid Lynx)
  • Rails Version: 3.0.7
  • Ruby Version: 1.8.7
  • MiniMagick Version: 3.3
like image 586
Zachary Abresch Avatar asked Jun 27 '11 18:06

Zachary Abresch


2 Answers

Did you installed ImageMagick? If not, try sudo apt-get install ImageMagick, and then restart your webrick server

like image 85
AdrianHwang Avatar answered Nov 09 '22 00:11

AdrianHwang


it's probably the race condition which is mentioned here:

https://ar-code.lighthouseapp.com/projects/35/tickets/6-race-condition-with-temp_file

here's one fix:

http://rubyforge.org/tracker/index.php?func=detail&aid=9417&group_id=1358&atid=5365

alternatively, and probably easier, you could try this:

// Generates a thumbnail image
mm = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm_copy = mm.clone   # clone the opened Image, instead of re-opening it

mm.resize(thumbnail_dimensions.join("x"))
mm.write(Rails.root.join('public', 'uploads', "t_"+new_url))

// Generates cropped version
mm_copy.crop('200x200')
mm_copy.write(Rails.root.join('public', 'uploads', "c_"+new_url))
like image 45
Tilo Avatar answered Nov 09 '22 01:11

Tilo