How can I optimize images uploaded using Paperclip & Rails? - ruby-on-rails

Does anyone know how to optimize image size through paperclip?
In my Graphic model I have the following:
has_attached_file :graphic,
:styles => {
:home => ['120x90',:jpg],
:thumb => ['70x70',:jpg]
}
By optimize I mean, reduce the filesize of each of :home & :thumb graphics once paperclip creates them, Google's speed test tells me that I should be able to reduce these by 70 - 90%.
I think I can do this by creating a perclip processor, but not really sure where to start.
Kind of stumped on this one, any help / hints much appreciated!
(Rails 2.3)

There's a gem that allows you to do this easily with Paperclip:
https://github.com/janfoeh/paperclip-optimizer

And this gem to image compression processor for Paperclip:
https://github.com/emrekutlu/paperclip-compression

Check out the quality option. I've read that 75 is the best setting for balancing quality and the resulting size of the image.
has_attached_file :photo,
:styles => {
:small => {
:geometry => '38x38#',
:quality => 40,
:format => 'JPG'
},
:medium => {
:geometry => '92x92#',
:quality => 50
}

Related

Rails reduce image size on some %

i need any solution for reducing image size
I am using paperclip
has_attached_file :image, :styles => { :small =>{ :geometry =>"200 x 123>"}}
For example i have original image 1 Mb and after some magic i want image 300 Kb
and then upload it on aws
You can use the convert_options
has_attached_file :image,
:convert_options => { :thumb => '-quality 50' }
or you can try this one
has_attached_file :image => { :quality => :better }
Refer: Paperclip doc
Take a look at this
https://github.com/janfoeh/paperclip-optimizer
It's a gem made for image optimization.
If you need a better optimization, use Google PageSpeed module for apache or Nginx !

AutoSmusher for amazon S3 to optimise images?

Any ruby implementation with paperclip to autosmush amazon s3 images?
PS: I googled and got this: https://github.com/grosser/smusher and works pretty nicely on my local machine. But to use something like this on amazon s3. It will be great to have an automated process for this and just smush newly created content. Any ideas?
Code I am using to some what optimise user uploaded images.
has_attached_file :attachment, {
:styles => {
:medium => ["654x5000000>", :jpg],
:small => ["260x50000000>", :jpg],
:thumb => ["75x75#", :jpg],
:facebook_meta_tag =>["200x200#", :jpg]
},
:convert_options => {
:medium => "-quality 80 -interlace Plane",
:small => "-quality 80 -interlace Plane",
:thumb => "-quality 80 -interlace Plane",
:facebook_meta_tag => "-quality 80 -interlace Plane"
},
:s3_headers => { 'Cache-Control' => 'max-age=315576000', 'Expires' => 10.years.from_now.httpdate }
}.merge(PAPERCLIP_STORAGE_OPTIONS)
But still images can be optimised. I got the gem smusher but confused how to use it. My current page ranking as per GTmetrics is here.
I have also been looking into lossless image compression with rails, and so far I think the best gem I have found is here. The instructions with the gem say to add a :processors option that gets passed a hash containing multiple processing options, such as :compression, which this gem defines. It also requires jpgtran and optipng to be installed, which I'm not sure if Amazon S3 has.

Paperclip, copy image without reprocessing it

When I copy a paperclip attachment it applies the default cropping defined in the style and ignores the cropper I have installed for cropping on the site via user input (CropperJs). What I need is just a raw copy of the image as it was cropped on the source image. I solve this by bluntly copying the file right now, but is there a better way?
has_attached_file :avatar,
:name => 'avatar',
:styles => {
:cropped => {
:geometry => "55x55#",
:processors => [:cropper]
},
:large => "600x600>"
}

Paperclip change images path after upgrade to rails 3.2

i have a problem with paperclip (3.0.2) after upgrade to rails 3.2 (from 3.0.10).
Originally the path of one image was:
"http://localhost:3000/system/photos/94/small/AudiLogo.jpg?1335392139"
and after the upgrade this kind of images never show again!, but if i upload a new picture this will display fine on page, but the new path that use is:
"localhost:3000/system/products/photos/000/000/094/smal/AudiLogo.jpg?1335392139"
Whats happend in the upgrade ? There's any solution for convert the olds path to new ?
I try with "rake paperclip:refresh:missing_styles" but dosen't works.
The paperclip config section it's this.
has_attached_file :photo,
:processors => lambda { |a|
if a.external?
[:thumbnail]
else
[:thumbnail,:watermark]
end
},
:styles => {
:slider => { :geometry => "350x312#", :format => :jpg, :watermark_path => "#{Rails.root}/public/images/watermark.png", :position => "NorthEast" },
:small => "100x50>",
:medium => "200>x200",
:thumb => "100x100>",
:big => { :geometry => "640x480>", :format => :jpg, :watermark_path => "#{Rails.root}/public/images/watermark.png" }
},
:default_url => "/images/noimage.png"
Thanks in advance.
I had the same problem. You can fix this by creating a file like config/initializers/paperclip.rb and put
Paperclip::Attachment.default_options.merge!(
:path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
:url => "/system/:attachment/:id/:style/:basename.:extension"
)
I just had a similar upgrade and routed around my problem this way:
has_attached_file :image,
:url => "/images/photos/:id/:basename_:style.:extension",
:path => ":rails_root/public/images/photos/:id/:basename_:style.:extension",
Assuming the "small" vs "smal" difference between original and current path is a typo, the other obvious change is the addition of the two numeric segments after the "/photos/".
".../photos/000/000/094/smal/AudiLogo.jpg?1335392139"
I suspect this is coming from an id_partition being used for the path. Are you setting a different default path interpolation in some other place?
Looking at Paperclip's code I see the id_partition method that would be responsible for this but still have not found any documentation pointing in the direction of a change in the default behavior. I did't get to follow the code in the gem to determine if it is a bug or undocumented change.

How to pass additional convert options to paperclip on Heroku?

class User < ActiveRecord::Base
has_attached_file :photo, :styles => { :square => "100%", :large => "100%" },
:convert_options => {
:square => "-auto-orient -geometry 70X70#",
:large => "-auto-orient -geometry X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
validates_attachment_size :photo,
:less_than => 5.megabyte
end
Works great on local machine, but gives me an error on Heroku: There was an error processing the thumbnail for stream.20143
The thing is I want to auto-orient photos before resizing, so they resized properly.
The only working variant now(thanks to jonnii) is resizing without auto-orient:
...
as_attached_file :photo, :styles => { :square => "70X70#", :large => "X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
...
How to pass additional convert options to paperclip on Heroku?
UPD
I discover, the trouble in "-auto-orient" option. It seems like this option is broken in version of ImageMagick used by Heroku. I created custom paperclip image processor inherited from paperclip's standard thumbnail:
module Paperclip
class Ao < Thumbnail
def transformation_command
super + " -auto-orient"
end
end
end
It works perfect on local machine, but fails on Heroku.
These are the sizes I use. They all work fine on heroku:
SIZES = {
:original => "640x480>",
:thumb => "150x150#",
:mini => "60x60#",
:micro => "30x30#"
}
Make sure your gem version of paperclip is the same as heroku's. You can specify the specific gem version in your .gems file and in your environment.rb to make sure they line up.
I'm not sure exactly why your convert_options are causing problems, but if I remember correctly paperclip uses ImageScience directly and your chosen options might be incompatible with the read only heroku file system.
If this is critical and you need an answer right now I'd raise a support ticket on heroku. If you get a response make sure you post it back here!

Resources