I've set up paperclip in rails and everything is working hunky-dory (i actually had to google that...:).
I've noticed however that Page Speed tells me I could losslessly compress my thumbnail and large images (the ones that paperclip produces) further. Is there an option I can put into my model which does this? I've noticed that mod_deflate doesn't compress images (I'm using Firefox).
You can add compression to paperclip processing using the paperclip-compression gem.
In your Gemfile:
gem "paperclip-compression", "~> 0.1.1"
(of course run bundle install)
In your model:
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:processors => [:thumbnail, :compression]
"jpegtran works by rearranging the compressed data (DCT coefficients), without ever fully decoding the image. Therefore, its transformations are lossless"
Note: if you are running on heroku, you'll need jpegtran, and optipng binaries added to your application. Here's a good article on running binaries on heroku.
You should do your own testing on various JPEG compression levels but I've noticed that I can bump ImageMagicks quality setting down to 75 and still not see any noticeable difference - with about a 30-40% file size savings.
My model looks like:
has_attached_file :photo,
:styles => {
:"185x138" => {
:geometry => "185x138>"
} },
:convert_options => {
:all => "-auto-orient",
:"185x138" => "-quality 75",
-quality 75 is for ImageMagick. If you're using a different processor you will need to adjust accordingly.
What about FFMPEG or AVCONV?
sudo apt-get install ffmpeg/avconv
= initializer
Paperclip.options[:command_path] = "/usr/bin/" # see `which ffmpeg`
= Modal
after_save :compress_with_ffmpeg
def compress_with_ffmpeg
[:thumb, :original, :medium].each do |type|
img_path = self.avtar.path(type)
Paperclip.run("ffmpeg", " -i #{img_path} #{img_path}")
end
end
Related
I have was seeing several discussions (on stackoverflow here and here, and as a bug on paperclip here) around the best way to tell the default_url of images using Paperclip and Rails so that they work fine in production with the asset pipeline. All solutions appear quite complicate.
Is there anything wrong in putting the default images in the public/ directory of the Rails app? Anything I need to worry down the line or that I am missing?
If I put images in the public/ directory and access them with the code below all appears to work correctly.
has_attached_file :image,
styles: {original: "1000x1000", medium: "530x530#", thumb: "300x300#"},
default_url: "/default-avatar_:style.png"
I generally, by convention alone include the missing styles images in the assets directory. You don't need any extra coding or complex mechanisms to have paperclip utilize them.
#Images within assets:
/app/assets/images/default-avatar_original.png
default-avatar_medium.png
default-avatar_thumb.png
# paperclip config in /app/models/user.rb
has_attached_file :image, :styles => {:original => ["1000x1000", :png],
:medium => ["530x530#", :png],
:thumb => ["300x300#", :png] },
:default_url => "/assets/default-avatar_:style.png"
Note that when specifying default_url, you exclude the /image/ directory.
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've searched around for different uploading options for rails and video and Paperclip seems to be pretty good, but are there any others people would recommend, it should have good tutorials and docs because i can't really find any great paperclip docs involving uploading video content.
We got Paperclip working with video a while back
Systems
You'll have the same ambiguity whether you use CarrierWave or Paperclip (Rails' two main "attachment" handlers)
Any upload system only handles the transfer of file data between your PC, your Rails app & your db. Each of them (from my understanding). E.G Paperclip only creates an ActiveRecord object from your file, saves the data to your server's public dir & creates a record in your db
Code
The question of video is one of using the right processor, rather than the right uploader:
#app/models/attachment.rb
has_attached_file :attachment,
styles: lambda { |a| a.instance.is_image? ? {:small => "x200>", :medium => "x300>", :large => "x400>"} : {:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10}, :medium => { :geometry => "300x300#", :format => 'jpg', :time => 10}}},
processors: lambda { |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] }
Extra
You'll need to use a video processor such as ffmpeg for Paperclip:
#GemFile
gem "paperclip-ffmpeg", "~> 1.0.1"
You may have to install ffmpeg on your system to get the processor to work locally (Heroku has ffmpeg). This will allow you to use the video_tag helper:
<%= video_tag(#model.attachment.url) %>
There's a good tutorial about using ffmpeg with Paperclip here
And another tutorial here
With rails Carrierwave and Paperclip are the best attachment handlers. Now
for all kinds of file upload like images, videos, other raw files etc. you can use Cloudinary with carrierwave. See this
http://cloudinary.com/documentation/rails_carrierwave
I am using Paperclip 3.4 and Paperclip-ffmpeg 0.9.4.
Here is my code in video.rb:
has_attached_file :file,
:url => '/:class/:id/:style.:extension',
:styles => { :thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 } },
:processors => [:ffmpeg], :swallow_stderr => false
When I try to upload a video, the error is
Command 'ffmpeg -ss 10 -i :source -y -vf scale=100:-1,pad=100:100:0:12.5:black -vframes 1 -f image2 :dest' returned 1. Expected 0
If I comment out or remove the styles line, the video is uploaded correctly. I tried changing the versions of the gems, to no avail.
Any pointers on why the styles hash is causing an issue?
Edit
Replacing '#' with '>' seems to solve the issue, but I am not sure what is the cause.
In the official paper clip gem documentation (under the "Resizing options" section) you can found the following description:
Default behavior is to resize the image and maintain aspect ratio (i.e. the :medium version of a 300×150 image will be 200×100). Some commonly used options are:
trailing #, thumbnail will be centrally cropped, ensuring the requested dimensions.
trailing >, thumbnail will only be modified if it is currently larger requested dimensions. (i.e. the :small thumb for a 120×80 original image will be unchanged)
and as it is pointed there you can check for more options here.
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
}