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

2

9

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
Kittle answered 27/6, 2011 at 18:22 Comment(2)
Does the crop code work if you comment out the thumbnail lines above?Furfur
Hmmm ... I haven't tried that. I'm getting back to this project now so I'll check it out and post the results.Kittle
I
7

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

Illuminometer answered 21/6, 2012 at 5:54 Comment(1)
This is the actual command => sudo apt-get install imagemagickAscomycete
G
3

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))
Gensmer answered 31/10, 2011 at 16:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.