How to set Paperclip to process original image? - ruby-on-rails

Using Paperclip Gem in a Rails 4 project to attach an image and then clean out all exif data, like this:
has_attached_file :image,
styles: lambda{ |a|
{
large: ['800x', :png],
thumb_340: ['340x340#', :png],
thumb_180: ['180x180#', :png]
}
},
convert_options: { all: '-strip' }
Problem is, convert_options isn't getting called on the original image. What's the best method for reprocessing (or pre-processing) the original to make sure that '-strip' gets called?

try this
has_attached_file :image,
styles: lambda{ |a|
{
original: {convert_options: '-strip'},
large: ['800x', :png],
thumb_340: ['340x340#', :png],
thumb_180: ['180x180#', :png]
}
}

Related

how to add rspec for paperclip gem validation for images

I have a Post model like this:
class Post < ApplicationRecord
has_attached_file :image,
styles: { large: "500X500",
medium: "300x300>",
thumb: "100x100#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
I am not sure what you really want to write a test for, but you can use Paperclip::Shoulda::Matchers to write simple ones:
describe Post do
# For: `has_attached_file :image`
it { should have_attached_file(:image) }
# For: `validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/`
it { should validate_attachment_content_type(:image).
allowing('image/png', 'image/gif').
rejecting('text/plain', 'nonimage/something') }
end

paperclip resize not working for dynamic columns on attachment model

I have a two columns on my attachment model on which the user sets the dimension to which they want to resize the image.
However, the variables when the resize happens are nil, but set to actual values after the resize happens.
below is the code
has_attached_file :file, :styles => lambda { |a|
{ :logo => ["200x50>",:png],
:user_defined => ["#{a.instance.custom_width}x#{a.instance.custom_height}>",:png] }
}
the custom_width & custom_height are nil when conversion happens however the logo conversion works as expected.
I am using ruby 2.2.4p230 & Rails 4.2.4
below is the full mode code
class Attachment < ActiveRecord::Base
belongs_to :model_one
belongs_to :attachable, polymorphic: true
#has_attached_file :file, styles: { logo: ['200x50>',:png] }
has_attached_file :file, styles: lambda { |attachment| attachment.instance.styles }
def styles
Rails.logger.info self.inspect
Rails.logger.info self.attachable
styles = {}
m = "200x50>"
l = "#{self.custom_width}x#{self.custom_height}>"
styles[:logo] = [m, :png]
styles[:user_defined] = [l, :png]
styles
end
end
Can anyone please help and let me know if i am doing something wrong?

conditional convert options paperclip

After some research I was able to add styles based on my image_class column.
Model.rb
has_attached_file :image,
:styles => lambda { |attachment| attachment.instance.decide_styles }
def decide_styles
styles = {}
case self.image_class
when "poster"
styles[:thumb] = ["30x45!", :jpg]
styles[:standard] = ["185x278!", :jpg]
styles[:expanded] = ["372x559!", :jpg]
styles[:big] = ["600x900!", :jpg]
when "cover"
styles[:thumb] = ["30x45!", :jpg]
styles[:standard] = ["300x1200!", :jpg]
end
styles
end
This works smoothly, now I wanted to add conditional convert_options as well. This somehow fails.
has_attached_file :image,
:styles => lambda { |attachment| attachment.instance.decide_styles },
:convert_options => lambda { |attachment| attachment.instance.decide_convert_options }
def decide_styles
...
end
def decide_convert_options
opshunz = {}
case self.image_class
when "poster"
opshunz[:thumb] = "-flop"
opshunz[:standard] = "-flop"
opshunz[:expanded] = "-flop"
opshunz[:big] = "-flop"
when "cover"
opshunz[:thumb] = "-enhance"
opshunz[:standard] = "-enhance"
end
opshunz
end
Error:
NoMethodError: undefined method `instance' for :all:Symbol
from /Users/AnsPoluke/Sites/nulike/app/models/movie_image.rb:8:in `block in <class:MovieImage>'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:431:in `[]'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:431:in `process_options'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:423:in `extra_options_for'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:56:in `convert_options'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:79:in `block in processor_options'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:78:in `each'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/style.rb:78:in `processor_options'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:462:in `block in post_process_style'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `each'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `inject'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:461:in `post_process_style'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:454:in `block in post_process_styles'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:453:in `each'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:453:in `post_process_styles'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:445:in `block (2 levels) in post_process'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:393:in `_run__3861360263242897910__image_post_process__callbacks'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:443:in `block in post_process'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:383:in `_run__3861360263242897910__post_process__callbacks'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/callbacks.rb:36:in `run_paperclip_callbacks'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:442:in `post_process'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:114:in `assign'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/paperclip-4.1.1/lib/paperclip/has_attached_file.rb:66:in `block in define_setter'
from (irb):2
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/AnsPoluke/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
Any ideas why it works perfectly with styles but fails with convert_options?
note: haven't verified that on working code
It seems that the argument to the block passed in :convert_options is already an instance, not the attachment (as opposed to styles option, where it is an attachment)
Try:
convert_options: lambda { |instance| instance.decide_convert_options }
Btw your code would look much better if you extract the configuration data, for example:
has_attached_file :image,
styles: lambda { |attachment| attachment.instance.image_options[:styles] },
convert_options: lambda { |instance| instance.image_options[:convert_options] }
IMAGE_OPTIONS = {
poster: {
styles: {
thumb: ["30x45!", :jpg],
standard: ["185x278!", :jpg],
expanded: ["372x559!", :jpg]
big: ["600x900!", :jpg]
},
convert_options: {
thumb: "-flop",
standard: "-flop",
expanded: "-flop",
big: = "-flop"
}
},
cover: {
styles: {
thumb: ["30x45!", :jpg],
standard: ["300x1200!", :jpg]
},
convert_options: {
thumb: "-enhance",
standard: "-enhance"
}
}
}
def image_options
IMAGE_OPTIONS[self.image_class]
end
I hope that helps
Update:
it looks like your convert_options are not being set here:
https://github.com/thoughtbot/paperclip/blob/a93dfc773b4fd649db4d1281b42a2a71b1ae72ff/lib/paperclip/style.rb#L55
it seems they recommend passing convert_options with styles, like in this spec: https://github.com/thoughtbot/paperclip/blob/263a498195d47563a6227be18cf4463c4c6e7903/spec/paperclip/style_spec.rb#L41
can you try this? so remove convert_options entirely, and in your configuration return hash like:
IMAGE_OPTIONS = {
poster: {
styles: {
thumb: {
geometry: "30x45!",
format: :jpg,
convert_options: '-flop',
},
standard: {...}
expanded: {...}
big: {...}
}
},
cover: {
styles: {...}
Apply it to all since that's what you're doing anyway?
:convert_options => {:all => "-flop"}
failing that you might be looking at creating a Paperclip Processor
The approved answer doesn't work. It can be read in the comments and the author acknowledges that it hasn't been tested in code.
This code does work:
has_attached_file :image,
:styles => lambda { |attachment|
thumb_convert_options = case attachment.instance.image_class
when "poster"
"-flop"
when "cover"
"-enhance"
end
{
thumb: {
convert_options: thumb_convert_options
}
}
}
The correct approach is to have convert_options inside the styles lambda; having it as a separate lambda does not work, at least for Paperclip version 4.1 and higher.
To keep my answer in one place, I've put all the code inline and omitted all styles beside thumb. Obviously to implement this you should keep the method decide_convert_options.
You can also specify convert_options in styles:
class Image < ActiveRecord::Base
has_attached_file :image, styles: -> (attachment) { attachment.instance.paperclip_styles }
private
def image_ratio
# logic to define the image ratio
end
def paperclip_styles
vitrina_geometry = image_ratio > 1.2 ? '268x156>' : '268x156^'
vitrina_convert_options = if image_ratio > 1.2
"-quality 75 -gravity center -crop '268x156+0+0'"
else
"-quality 75 -strip -gravity center -background '#FFFFFF' -extent 268x156"
end
{
medium: {
geometry: '500x500>',
convert_options: '-quality 75 -strip'
},
thumb: {
geometry: '256x148>',
convert_options: '-quality 75 -strip'
},
small: {
geometry: '120x120>',
convert_options: '-quality 75 -strip'
},
course_thumb: {
geometry: '395x220^',
convert_options: '-quality 75 -gravity center -crop \'395x220+0+0\''
},
:vitrina => {
geometry: vitrina_geometry,
convert_options: vitrina_convert_options
}
}
end
end
Add the convert_options into the styles themselves. Here is an example for a generic rails Image model which contains two styles and corresponding booleans to enable these styles.
# == Schema Information
#
# Table name: images
#
# id :integer not null, primary key
# image_file_name :string(255)
# image_content_type :string(255)
# image_file_size :integer
# hero_style :boolean
# thumb_style :boolean
# image_updated_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
#
class Image < ActiveRecord::Base
# These are the postprocessing options.
# The boolean <stylename>_style? attributes controls which styles are created.
STYLES = {
hero: {geometry: "2500x800#", convert_options: "-quality 75 -strip", source_file_options: ""},
thumb: {geometry: "312x100#", convert_options: "-quality 75 -strip", source_file_options: ""}
}
has_attached_file :image,
styles:
lambda { |file|
r = {}
STYLES.keys.each do |stylename|
r[stylename] = STYLES[stylename] if file.instance.method("%s_style?" % stylename).call
end
return r
}
validates_attachment :image, :presence => true,
content_type: { content_type: ["image/jpeg", "image/png"] },
file_name: {matches: [/png\Z/, /jpe?g\Z/]}
end

Paperclip: width is 100%, height is 60% of width

How would I write a Paperclip style to keep the width of the uploaded image at 100% but crop only the height to 60% of the width?
Something like this:
has_attached_file :image, :styles => { :cropped => "100%x[60% of height]" }
has_attached_file :image, :styles => {
:original => "100x60>",
:thumb => Proc.new { |instance| instance.resize }
}
#### End Paperclip ####
def resize
geo = Paperclip::Geometry.from_file(image.to_file(:original))
height = (geo.width.to_i * 60)/100
width = geo.width
"#{width.round}x#{height.round}!"
end
Hope this could help you
has_attached_file :image, :styles => after_save :save_image_dimensions
def save_image_dimensions
geo = Paperclip::Geometry.from_file(image.path)
self.image_height = (geo.height.to_i * 60)/100
end
If you have problem on extract dimension From below you can get a good help
https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions
Please look into this link for cropping images using paperclip
http://viget.com/extend/manual-cropping-with-paperclip
Thanks

How to make Paperclip crop and NOT scale an attached image?

I want Paperclip to crop, and not scale (see :full1 in this excerpt)
class Graphic < ActiveRecord::Base
has_attached_file :image, :styles => { :full0 => "940x1000#" #want it to scale, and crop if neccessary
:full1 => "940#", #want it to crop width, not scale
}
I want :full1 to work, but it doesn't. By "work" I mean it should crop the image's width, but do nothing to it's height. The reason is I'm uploading web screenshots, and I want them to be trimmed to 940px wide (from the center), but their height should remain intact. As far as what I research on paperclip I'm not finding how to do this.
Apparently it's quite supported by ImageMagick: http://www.imagemagick.org/Usage/crop/#crop_strip But I don't know how to jam this into paperclip on rails.
Many thanks!
Could you just set the height to something absurdly large so that it will be a non-issue?
class Graphic < ActiveRecord::Base
has_attached_file :image, :styles => { :full0 => "940x1000#" #want it to scale, and crop if neccessary
:full1 => "940x9999999#", #want it to crop width, not scale
}
I think that will crop anything wider than 940px.
You can user convert options for image process, Following will crop the image centrally.
has_attached_file :profile_picture, :storage => :s3,
:styles => { :medium => "", :thumb => ""},
:convert_options => {
:thumb => Proc.new { |instance| instance.thumnail_dimension },
:medium => Proc.new { |instance| instance.thumnail_dimension(300) }
}
def thumnail_dimension(size=100)
dimensions = Paperclip::Geometry.from_file(profile_picture.queued_for_write[:original].path)
min = dimensions.width > dimensions.height ? dimensions.height : dimensions.width
"-gravity Center -crop #{min}x#{min}+0+0 +repage -resize #{size}x#{size}^"
end

Resources