Can paperclip read photo geometry off an S3 bucket?
Asked Answered
R

3

12

I would like to read the geometry of a photo off of my S3 container.

When it's on my local, this works :

def photo_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file photo.path(style)
end

But it doesn't seem to work when I switch my model over to S3.. Any recommendations?

The bigger story, is I'm trying to write some code that will allow me to retrieve photos from S3, allow users to crop them, and then reupload them back to S3 still assigned by paperclip.

EDIT:

This is the error that is returned :

Paperclip::NotIdentifiedByImageMagickError: photos/199/orig/greatReads.png is not recognized by the 'identify' command.
from /Users/daniellevine/Sites/hq_channel/vendor/gems/thoughtbot-paperclip-2.3.1/lib/paperclip/geometry.rb:24:in `from_file'
from /Users/daniellevine/Sites/hq_channel/app/models/photo.rb:68:in `photo_geometry'
from (irb):1
Retsina answered 1/7, 2010 at 18:51 Comment(0)
R
16

If you're using S3 as a storage mechanism, you can't use the geometry method above (it assumes a local file). Paperclip can convert from S3 file to local TempFile with the Paperclip::Geometry.from_file:

Here is my updated code:

def photo_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file(photo.to_file(style))
end
Retsina answered 1/7, 2010 at 20:1 Comment(3)
#to_file was removed in paperclip 3.0.1. In that and later versions, use Paperclip::Geometry.from_file(Paperclip.io_adapters.for(photo.styles[style]))Aquilar
@IsaacBetesh This does not work for me. I get the following error: Paperclip::AbstractAdapter#path delegated to @tempfile.path, but @tempfile is nil: Paperclip::NilAdapter. FYI, I'm using s3 with the fog gem.Frequent
I'm using S3 directly (i.e. aws-sdk gem) so I can't say anything definite about fog, but your stack trace will probably contain some clues.Aquilar
D
11

This works for s3 and local

def photo_geometry(style = :original)
  @geometry ||= {}
  photo_path = (photo.options[:storage] == :s3) ? photo.url(style) : photo.path(style)
  @geometry[style] ||= Paperclip::Geometry.from_file(photo_path)
end
Devonna answered 27/6, 2011 at 9:2 Comment(0)
D
1

I had more or less exactly the same issue, but none of the answers here worked for me, but this did:

# helper method used by the cropper view to get the real image geometry
def image_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file open("https:" + image.url(style))
end
Downall answered 2/8, 2018 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.