Paperclip save images as jpg with white background - imagemagick

I'd like to know how to convert png and gif files with alpha channel to jpg with white background with paperclip
I tried this but, it doesn't work
has_attached_file(
:photo,
:whiny => false,
:styles => {
:medium => ["300x300>", :jpg],
:thumb => ["100x100>", :jpg]
},
:convert_options => { :all => '-alpha white -background white'}
)
It saves the file with the gray background.

here the solution
has_attached_file :photo,
:styles => {
:medium => ["300x300>",:jpg],
:thumb => ["100x100>", :jpg]
},
:convert_options => {
:all => '-background white -flatten +matte'
}

-alpha remove -background white is preferable. white is not a valid argument for -alpha.

Related

How to save image as progressive image using paperclip?

How to save image as progressive image using paperclip/Imagemagick?
Code I am using:
has_attached_file :photo, {
:styles => {
:medium => ["200x200#", :jpg] ,
:small => ["192x256#", :jpg],
:thumb => ["50x50#", :jpg],
:small_thumb =>["30x30#", :jpg]
},
:convert_options => {
:medium => "-quality 60",
:small => "-quality 60",
:thumb => "-quality 60",
:small_thumb => "-quality 60"
},
:default_url => "/assets/default_:style.png" }.merge(PAPERCLIP_STORAGE_OPTIONS)
Simply add -interlace Line in the convert_options of your choice.
Doc states explicitly:
Use Line or Plane to create an interlaced PNG or GIF or progressive JPEG image.

How to set the dimensions for portrait images with the Paperclip gem

I am using the paperclip gem to upload images,I want to upload both landscape and portrait images.Can any please help me how to set the dimension for both the images.
My code is:
has_attached_file :media,
:styles => {:yplarge=>"440x300>"},
:path => ":rails_root/public/system/:class/:id/:style/:basename.:extension",
:url => "/system/buzz_pictures/:id/:style/:basename.:extension"
validates_attachment_size :media, :less_than => 2.megabytes,
:message => "Please attach a smaller picture."
validates_attachment_content_type :media,
:content_type=>['image/jpeg', 'image/png', 'image/gif']
This code is working for landscape images but not for portrait.
Just add another style:
:styles => {
:yplarge=>"440x300>",
:portrait=>"300X440>"
}
Change the values as needed. Note that if the image is smaller than the given dimensions then it won't resize it. To change that behavior replace the the > with #. This will force the image to be resized to the specified dimensions.
Refer to the Paperclip documentation for using your different styles:
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
the solution below will save 2 styles, portrait rotated by 90 if original in landscape, and vice versa.
has_attached_file :media,
:styles => {:landscape => Proc.new { |a| { :geometry => "440x300>", :rotation => 90 unless a.landscape? } },
:portrait => Proc.new { |a| { :geometry => "300x440>", :rotation => 90 if a.landscape? } } }
:path => ":rails_root/public/system/:class/:id/:style/:basename.:extension",
:url => "/system/buzz_pictures/:id/:style/:basename.:extension",
:processors => [:rotator]
def landscape?
Paperclip::Geometry.from_file(to_file(:original)).horizontal?
end
module Paperclip
class Rotator < Thumbnail
def transformation_command
if rotate_command
super + rotate_command
else
super
end
end
def rotate_command
if #options[:rotation]
" -rotate #{ #options[:rotation] }"
end
end
end
end

How do I pass a variable watermark path to a paperclip watermark processor?

I have watermarks, galleries, and photos models.
Gallery belongs_to Watermark
Photo belongs_to Gallery
class Photo < ActiveRecord::Base
before_save :save_dimensions, :set_orientation
belongs_to :gallery
has_attached_file :image,
:processors => [:watermark],
:styles => {
:thumbnail => ["80x80>"],
:small => {
:geometry => "200x200>",
:watermark_path => "#{gallery.watermark.image.path(:small)}",
:position => "Center"
},
:medium => {
:geometry => "400x400>",
:watermark_path => "#{gallery.watermark.image.path(:medium)}",
:position => "Center"
},
:large => {
:geometry => "600x600>",
:watermark_path => "#{gallery.watermark.image.path(:large)}",
:position => "Center"
}
},
:path => ":rails_root/public/images/galleries/:gallery_id/:id/:style_:basename.:extension",
:url => "galleries/:gallery_id/:id/:style_:basename.:extension"
validates_attachment_presence :image
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png']
attr_protected :image_file_name, :image_content_type, :image_file_size
end
With this I get undefined local variable or method 'gallery'. If I try like this with :gallery_watermark_path defined using Paperclip.interpolations it silently fails.
:watermark_path => ":gallery_watermark_path"
The :gallery_watermark_path doesn't get processed by Paperclip.interpolations:
development.log:
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '80x80>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-1mcqvox' 2>/dev/null
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '200x200>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-60zlb' 2>/dev/null
[paperclip] composite '-gravity' 'Center' ':gallery_watermark_path' '/tmp/stream20110620-30644-1j6i9in20110620-30644-60zlb' '/tmp/stream20110620-30644-1j6i9in20110620-30644-60zlb' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::PaperclipError: There was an error processing the watermark for stream20110620-30644-1j6i9in>
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '400x400>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-1ronidq' 2>/dev/null
[paperclip] identify -format %wx%h '/tmp/stream20110620-30644-1j6i9in.jpg[0]' 2>/dev/null
[paperclip] convert '/tmp/stream20110620-30644-1j6i9in.jpg' '-resize' '600x600>' '-auto-orient' '/tmp/stream20110620-30644-1j6i9in20110620-30644-1ikfy72' 2>/dev/null
In short, how do I pass the watermark variable to the processor?
You could use a proc for the styles:
has_attached_file :image,
:processors => [:watermark],
:styles => proc { |attachment|
{
:thumbnail => ["80x80>"],
:small => {
:geometry => "200x200>",
:watermark_path => attachment.instance.gallery.watermark.image.path(:small),
:position => "Center"
},
:medium => {
:geometry => "400x400>",
:watermark_path => attachment.instance.gallery.watermark.image.path(:medium),
:position => "Center"
},
:large => {
:geometry => "600x600>",
:watermark_path => attachment.instance.gallery.watermark.image.path(:large),
:position => "Center"
}
}
},
[...]
But sometimes, the proc is called before the instance get all its attributes (so sometimes gallery may be nil because you may have assigned image before gallery_id).
The watermark_path needs to be a Pathname, try to use Rails.root.join like this:
has_attached_file :image,
:processors => [:watermark],
:styles => {
:thumbnail => ["80x80>"],
:small => {
:geometry => "200x200>",
:watermark_path => Rails.root.join("#{gallery.watermark.image.path(:small)}")
:position => "Center"
},
:medium => {
:geometry => "400x400>",
:watermark_path => Rails.root.join("#{gallery.watermark.image.path(:medium)}")
:position => "Center"
},
:large => {
:geometry => "600x600>",
:watermark_path => Rails.root.join("#{gallery.watermark.image.path(:large)}"),
:position => "Center"
}
}
[...]
I hope that helps you,
cheers.

How can I reduce the quality of an uploading image using Paperclip?

I am running Ruby on Rails 3 and I would like to reduce the quality of an uploading image using the Paperclip plugin/gem. How can I do that?
At this time in my model file I have:
has_attached_file :avatar,
:styles => {
:thumb => ["50x50#", :jpg],
:medium => ["250x250#", :jpg],
:original => ["600x600#", :jpg] }
that will convert images in to the .jpg format and will set dimensions.
Try using convert_options.
has_attached_file :avatar,
:styles => { :thumb => '50x50#' },
:convert_options => { :thumb => '-quality 80' }
From the paperclip wiki, there's an option for quality:
class User < ActiveRecord::Base
has_attached_file :photo,
:styles => {
:small => {
:geometry => '38x38#',
:quality => 40,
:format => 'JPG'
},
:medium => {
:geometry => '92x92#',
:quality => 50
}
end
As James says, once you figure out the correct arguments to pass to ImageMagick's convert by experimenting on the command line, you can pass these in to Paperclip through the convert_options option as in James' example.
If you have multiple arguments, pass them in as an array. Here's an example which I laboured over for a while:
:convert_options => {:medium => ["-shave", "2x2", "-background", "white",
"-gravity", "center", "-extent",
"530x322", "+repage"],
:small => ["-shave", "1x1"] }
Except -quality, the -strip option of ImageMagick can remove all profile and other fluff from the image which may reduce more size
has_attached_file :photo,
:styles => {
:thumb => "100x100#" },
:convert_options => {
:thumb => "-quality 75 -strip" }

Paperclip Resize to fit a rectangular box

I have a rectangular image for example 30x800 pixels
How I can scale it with paperclip to preserve the aspect ratio to a 100x100 pixel image with borders filling the empty area ?
an example : http://www.imagemagick.org/Usage/thumbnails/pad_extent.gif
has_attached_file :image, :styles => { :thumb => "100x100>" },
:convert_options => {:thumb => "-gravity center -extent 100x100"}
Or with not white background
has_attached_file :image, :styles => { :thumb => "100x100>" },
:convert_options => {:thumb => "-background red -gravity center -extent 100x100"}

Resources