Rails reduce image size on some % - ruby-on-rails

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 !

Related

rails paperclip - changing aspect ratio of uploaded image

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

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>"
}

Can't resize uploaded image

I'm using paperclip gem for uploading and resizing images. This setup works fine. I'm able to display the uploaded images. The problem comes when I try to resize the uploaded image.
Here is snippet from the model file
has_attached_file :photo,
:size => {:small => "150x150>"}
When I try to upload the image I get this error.
Photo /var/folders/gm/gm-SegRMHuOkSlYtTMkO8U+++TI/-Tmp-/file.jpg is not recognized by the 'identify' command.
I'm sure that the file is jpg. Here is the output of the file command
file.jpg: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJ"
I'm not sure but in our application we do the same thing and it works. Our code looks like this:
has_attached_file :image,
:styles => {:small => "280x173#", :medium => "635x393#"},
:convert_options => {:all => "-quality 80"},#,
:default_style => :medium,
:default_url => "/images/study/nophoto.jpg"
validates_attachment_size :image, :less_than => 10.megabyte
validates_attachment_content_type :image, :content_type => ['image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg']
The difference I see, is that you might have to provide convert_options to be able to resize.
Have you tried any other jpg file, maybe with a simpler path also?

How can I optimize images uploaded using Paperclip & 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
}

Resources