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
Related
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
I have a custom processor for my Paperclip styles: cropper.rb. Though it is not called and return NameError (uninitialized constant Paperclip::Cropper) error.
It has been discussed here : Rails3 and Paperclip but a while ago. It was concerning Rails 3 back then.
I am under Rails 5 (update from Rails 4.x)
Profilepic.rb
class Profilepic < ApplicationRecord
belongs_to :professionnels
has_attached_file :image, styles: { big: "1200x1200", medium: "400x400", small: "250x250"}
validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, size: {less_than: 10.megabytes}
has_attached_file :finalimage, styles: { medium: "500x500", small: "200x200"}, whiny: false, use_timestamp: false, processors: [:cropper]
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
end
lib/paperclip_processors/cropper.rb
module Paperclip
class CustomCropper < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
if target.crop_w && target.crop_x
#current_geometry.width = target.crop_w
#current_geometry.height = target.crop_h
end
end
def target
#attachment.instance
end
def transformation_command
# call crop processing only if user inputs are there
if target.crop_w && target.crop_x
crop_command = [
"-crop",
"#{target.crop_w}x" \
"#{target.crop_h}+" \
"#{target.crop_x}+" \
"#{target.crop_y}",
"+repage"
]
crop_command + super
else
super
end
end
end
end
OK spent a day to realize the correct lib subfolder is actually paperclip and not paperclip_processors although Paperclip Git does mention both as valid and automatically loaded.
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]
}
}
I am trying to implement the steps to check and resize images with paperclip based on this blog post: http://www.techdarkside.com/how-to-re-size-images-that-are-too-large-on-the-fly-with-paperclip-and-rails
Here is what I have in place...
class Question < ActiveRecord::Base
# subclasses
class Question::Image < Asset
has_attached_file :attachment,
:url => "/uploads/:class/:attachment/:id_partition/:basename_:style.:extension",
:styles => Proc.new { |attachment| attachment.instance.styles },
:styles => Proc.new { |attachment| attachment.instance.resize }
attr_accessible :attachment
# http://www.ryanalynporter.com/2012/06/07/resizing-thumbnails-on-demand-with-paperclip-and-rails/
def dynamic_style_format_symbol
URI.escape(#dynamic_style_format).to_sym
end
def styles
unless #dynamic_style_format.blank?
{ dynamic_style_format_symbol => #dynamic_style_format }
else
{ :medium => "300x300>", :thumb => "100x100>" }
end
end
def dynamic_attachment_url(format)
#dynamic_style_format = format
attachment.reprocess!(dynamic_style_format_symbol) unless attachment.exists?(dynamic_style_format_symbol)
attachment.url(dynamic_style_format_symbol)
end
def resize
if self.attachment_file_size > 2000000
"300x300>"
else
" "
end
end
end
I'm thinking the issue is with the reuse of the :styles symbol, however I'm not sure how to work both the styles method AND the resize method into a single Proc statement.
Here is what I ended up with thanks to #janfoeh suggestion. I did need to add :originalto the options in style to get this to work. I also bumped the max file size up to 5mb.
class Question < ActiveRecord::Base
# subclasses
class Question::Image < Asset
has_attached_file :attachment,
:url => "/uploads/:class/:attachment/:id_partition/:basename_:style.:extension",
:styles => Proc.new { |attachment| attachment.instance.styles }
attr_accessible :attachment
# http://www.ryanalynporter.com/2012/06/07/resizing-thumbnails-on-demand-with-paperclip-and-rails/
def dynamic_style_format_symbol
URI.escape(#dynamic_style_format).to_sym
end
def styles
unless #dynamic_style_format.blank?
{ dynamic_style_format_symbol => #dynamic_style_format }
else
{ :original => resize, :medium => "300x300>", :thumb => "100x100>" }
end
end
def dynamic_attachment_url(format)
#dynamic_style_format = format
attachment.reprocess!(dynamic_style_format_symbol) unless attachment.exists?(dynamic_style_format_symbol)
attachment.url(dynamic_style_format_symbol)
end
def resize
if self.attachment_file_size > 5000000
"1000x1000>"
else
" "
end
end
end
Trying to set max width to 1280 when above 600Kb and not a .gif:
has_attached_file :main_image,
:styles => {:original => "" },
:convert_options => {
:original => lambda { |instance| (instance.main_image_file_name.index(/\.gif/,-4).nil? && instance.main_image.size > 600000) ? "-resize 1280>" : "" }
},
:path => ":rails_root/public/system/:class/:attachment/:id_:basename.:extension",
:url => "/system/:class/:attachment/:id_:basename.:extension"
Getting this in my logs:
Command :: convert '/var/folders/hn/2q5wpxh52nn_3nk9wwskr48w0000gn/T/5827912cb359afd550a72bf4f54b109020141119-76993-1i6nij6.png[0]' -auto-orient -resize 1280> '/var/folders/hn/2q5wpxh52nn_3nk9wwskr48w0000gn/T/5827912cb359afd550a72bf4f54b109020141119-76993-1i6nij620141119-76993-q0m833'
sh: 1280: Bad file descriptor
[paperclip] An error was received while processing: #<Paperclip::Error: There was an error processing the thumbnail for 5827912cb359afd550a72bf4f54b109020141119-76993-1i6nij6>
Two issues here:
Paperclip w/ Rails will often require you to add \\ in front of < or > characters, as in 1280x\\>
Also, Paperclip always needs those x's, even when just doing width (1280x\\> and not 1280\\>).
Here's my full solution for setting max width to 1280 when above 600Kb and not a .gif:
has_attached_file :main_image,
:styles => {:original => "" },
:convert_options => {
:original => lambda { |instance| (instance.main_image_file_name.index(/\.gif/,-4).nil? && instance.main_image.size > 600000) ? "-resize 1280x\\>" : "" }
},
:path => ":rails_root/public/system/:class/:attachment/:id_:basename.:extension",
:url => "/system/:class/:attachment/:id_:basename.:extension"