attachment_fu and s3 woes, or how I spent most of Friday 2
At OML we use Amazon S3 for storage, when we launched the site Gravatar was very unreliable so we had to roll our own with attachment_fu, and it worked fairly well.
With our redesign we want to make all avatars square coz the site looks better that way, so I added a JavaScript image cropper to help you crop your uploaded pictures into squares. It didn’t take long to write and it worked fine in my local development machine. But I was using file_system in attachment_fu locally instead of S3. (Now in retrospect I should’ve setup my own test bucket on S3)... I switched it to S3 on the beta site and it stopped working, that is, the images don’t get cropped properly! After about 10 hours of tracing through attachment_fu and trying silly things such as writing my own with_image override in s3_backend.rb, in the end I only had to add a single line and a minor change. Here are the before and after code…
before:
def crop2square(args)
image2crop = find_or_initialize_thumbnail(:full)
image2crop.with_image do |img|
@data = img.crop("#{args[:width]}x#{args[:height]}+#{args[:x1]}+#{args[:y1]}")
end
attachment_options[:thumbnails].each { |suffix, size| create_or_update_thumbnail(@data, suffix, *size) }
end
after:
def crop2square(args)
image2crop = find_or_initialize_thumbnail(:full)
image2crop.temp_path = image2crop.create_temp_file
image2crop.with_image do |img|
@data = img.crop("#{args[:width]}x#{args[:height]}+#{args[:x1]}+#{args[:y1]}")
end
attachment_options[:thumbnails].each { |suffix, size| create_or_update_thumbnail(@data.path, suffix, *size) }
end
I don’t really feel like explaining why this works right now, and if you’re having the same problem I’m sure you won’t care either and you just want to copy and paste the code and move onto your next problem.
Hope this helps…