After some research I was able to add styles based on my image_class
column.
Model.rb
has_attached_file :image,
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: {...}
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
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
.