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.
Related
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 !
Using paper clip, how can I change the aspect ratio of uploaded image.
Which is easier? Doing with jcrop or paperclip ?
I think paperclip would be nice but not sure where/how to keep the config options.
As for the paperclip you can easy do this by specifying convert options directly in your model. For example:
has_attached_file :photo,
:preserve_files => true,
:styles => { :medium => "800x800>",
:small => "300x300>",
:thumb => "150x150>" },
:convert_options => { :medium => "-quality 70 -interlace Plane -strip",
:small => "-quality 70 -interlace Plane -strip",
:thumb => "-quality 70 -interlace Plane -strip" },
:default_url => "/images/missing.png"
you can use that way any ImageMagick's conver option.
All supported options are described here.
BTW: if you want it to be processed in background than add this to your Gemfile:
gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'delayed_paperclip'
gem 'daemons'
and this to the model:
process_in_background :photo, queue: 'paperclip_processing'
to run/stop a daemon:
RAILS_ENV=production bin/delayed_job -n 2 start
RAILS_ENV=production bin/delayed_job stop
and to see the progress and manage the queue this is great:
gem 'delayed_job_web'
Enjoy.
You'll need to use ImageMagick to get Paperclip to crop images. Paperclip only handles the upload process - it doesn't crop or store the images for you
I would recommend looking at how to use Paperclip with ImageMagick, and then you'll have to find a way to populate your models' styles option with ImageMagick commands:
has_attached_file :image, styles: { medium: "[[imagemagick code]]" }
ImageMagick change aspect ratio without scaling the image
I'm trying to apply styles to an image uploaded by paperclip, to save the original and a thumbnail.
has_attached_file :image, styles => { :thumb => "100x100", :original => "100%"},
:path => "images/:id/:style_:basename.:extension"
Is not working, it only saves the original, even I delete the :original => "100%" part.
And I've ImageMagick installed and the gem rmagick.
:thumb => "100x100" s/b :thumb => "100x100>"
you forgot the >
see doc:
https://github.com/thoughtbot/paperclip
remove this :original => "100%"
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
}
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!