Simple Cropping with Paperclip

Simple cropping with Paperclip

You only need to use # instead of > as a parameter:

has_attached_file :picture, :styles => { :thumb => "200x200#" }

How do you crop a specific area with paperclip in Rails (3)?

Check {size}{offset} combination here:

http://www.imagemagick.org/script/command-line-processing.php#geometry

Example where numbers are width, height, x, y:

90x90+40+30

Paperclip parses the style options string and it is limited to resizing and cropping. Complex ImageMagick options work if they are being passed as :convert_options, because they are added to convert command without modification.

has_attached_file :image,
:styles => { :thumb => "" },
:convert_options => { :thumb => "-crop 90x90+40+30" },
:default_style => :thumb

Links to thumbnail processor source code and wiki page:

  • https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb
  • https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

Stop paperclip from cropping and resizing before other parameters

I tried creating my own paperclip processor, but I ran into issues. My processor was overriding the cropper.rb processor as they both tried to use the same method within the Thumbnail processor; so I could move the resize command to the end of the arguments list but the crop argument was nowhere to be found. I couldn't figure out how to include the cropper processor inside of my custom processor, and the whole thing just seemed like a mess and it wasn't working.

No answer found. I ended up switching to Carrierwave and encountering another set of problems which I've finally arrived at a solution for. Too bad Carrierwave doesn't log the processing of images, but that's a small price to pay.

Carrierwave RMagick not removing transparency in convert to jpg

Carrierwave +repage option not working

How do I get the top region of an image when cropping with Rails and Paperclip?

Here's my favorite way to do it:

:styles => { :large => "", :medium => "", :thumb => ""},
:convert_options => {
:large => "-gravity north -thumbnail 300x300^ -extent 300x300" ,
:medium => "-gravity north -thumbnail 200x200^ -extent 200x200",
:thumb => "-gravity north -thumbnail 100x100^ -extent 100x100"
}

Note that instead of # you use ^ + extent.

Gravity parameters are like on a map: north, northeast, east ...

How to resize and crop in ImageMagick and Paperclip?

Not sure if this handles #1 in the question, but it does #2 and #3.

has_attached_file :picture, 
:styles => { :medium => "800x200#" }

Thanks to colli8marko for this!

Simple cropping with Paperclip

Rails 3.0 Image Cropping with Paperclip and s3 on Heroku

Could it be that the problem has to do with files without an extension?

For instance when you allow people to give an URL for an image to use on your service and you download that image for conversion and storage on S3 it could be the download does not have an extension, while it does have a content type and therefore is displayed and processed correctly as an image.

The problem occurs here: (See comments)

 def to_file style = default_style
return @queued_for_write[style] if @queued_for_write[style]
filename = path(style)
extname = File.extname(filename) # Likely the Nil is returned here
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname]) # Ext name is used here
file.binmode
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
file.rewind
return file
end

You can try the following monkey patch if the extension indeed causes the Nil:

module Paperclip
module Storage
module S3
def to_file style = default_style
return @queued_for_write[style] if @queued_for_write[style]
filename = path(style)
extname = File.extname(filename) || "" # <==== Changed
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname])
file.binmode
file.write(AWS::S3::S3Object.value(path(style), bucket_name))
file.rewind
return file
end
end
end
end


Related Topics



Leave a reply



Submit