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" }
Related
I want to update my styles in paperclip, but when i run the command, a error is returned. Here is the code of my styles:
image.rb
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
validates_presence_of :image
has_attached_file :image, :processors => [:ffmpeg],
:styles => { :video => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100>", :format => 'jpg' },
:large => { :geometry => "400x300>", :format => 'jpg' },
:medium => { :geometry => "400x200>", :format => 'png' },
:icon => { :geometry => "36x36>", :format => 'jpg' },
:profile => { :geometry => "28x28>", :format => 'jpg' } },
:default_url => "/images/:style/missing.png"
end
Returned Error:
Regenerating Image -> image -> [:icon, :large, :medium, :profile, :thumb, :video]
rake aborted!
Cocaine::CommandNotFoundError
How i solve this error?
It seems to be that you have not installed the "paperclip-ffmpeg" gem.
See this link: https://github.com/owahab/paperclip-ffmpeg#requirements
When I try to upload a video using Paperclip, I get an error message can't dump File.
Model Video :
class Video < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:styles => {
:mp4 => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "300x300>", :format => 'jpg', :time => 5 }
}, :processors => [:ffmpeg]
validates_attachment_presence :avatar
validates_attachment_content_type :avatar,
:content_type => /video/,
:message => "Video not supported"
end
Added the convert_options code to existing Paperclip code in my user method. Now getting:
/Users/-----/----/-------/app/models/user.rb:148: syntax error, unexpected tASSOC, expecting keyword_end
:convert_options => { :small => '-quality 40' }
What am I missing?
User Model:
...
# Paperclip
has_attached_file :photo,
:styles => {
:small => ["50x50#", :jpeg],
:big => ["450x450#", :jpeg]
}
:convert_options => {
:small => '-quality 40'
}
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
...
You're missing a comma after your :styles => {...}.
has_attached_file :photo,
:styles => {
:small => ["50x50#", :jpeg],
:big => ["450x450#", :jpeg]
},
:convert_options => {
:small => '-quality 40'
}
I know that it's a lot of indenting but it helps me when I try to debug some thing.
Some editors highlight opening brackets with closing ones. It can also help.
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.
I was doing some image optimization on my site. Things are working awesome on development machine but in production mode I am getting an error ie:
ActionView::Template::Error (can't dup Symbol):
This is happening only in case of aws, If I remove this then things working perfectly fine:
A code I was using and was working perfectly:
has_attached_file :attachment, {
:styles => {
:medium=>"654x346>",
:small => "260x400>",
:thumb => "75x75#",
:facebook_meta_tag => "200x200#"
}, :include_updated_timestamp => false
}.merge(PAPERCLIP_STORAGE_OPTIONS)
But now I modified this and Its working perfectly fine in development environment but not working on production environment.
has_attached_file :attachment,
:styles => {
:medium => {
:geometry => "654x346>",
:quality => 60,
:format => 'JPG'
},
:small => {
:geometry => "260x400>",
:quality => 60,
:format => 'JPG'
},
:thumb => {
:geometry =>"75x75#",
:quality => 60,
:format => 'JPG'
},
:facebook_meta_tag => {
:geometry =>"200x200#",
:quality => 50,
:format => 'JPG'
}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
Other relevant settings:
PAPERCLIP_STORAGE_OPTIONS = {
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/amazons3.yml",
}
amazons3.yml
production:
access_key_id: XXXXxxxXXXXxxx
secret_access_key: XXXXxxxXXXXxxxXXXXxxxXXXXxxxXXXXxxxXXXXxxx
bucket: images.XXXXxxx.com
Here is the working code:
has_attached_file :attachment, {
:styles => {
:medium => ["654x346>", :jpg],
:small => ["260x400>", :jpg],
:thumb => ["75x75#", :jpg],
:facebook_meta_tag =>["200x200#", :jpg]
},
:convert_options => {
:medium => "-quality 60",
:small => "-quality 60",
:thumb => "-quality 60",
:facebook_meta_tag => "-quality 60"
}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
ActionView::Template::Error tends to be an error raised inside of a view (Rails re-wraps errors inside views inside ActionView::Template::Error exceptions).
To help you more I'd need to see the backtrace and the view involved.
This is not really an answer, but if this helps you find the cause you may as well accept it.