In Rails, while using the carrierwave gem, can we optimize images that are being uploaded? Optimize means compress and shrink the file size?
Yes, it is possible. You should write own image processor and use ImageMagick or MiniMagick command to optimize images as you wish.
For example here is article about optimization of images with MiniMagick.
The second way is related to using such gems as carrierwave-imageoptimizer it provide image optimization out of the box.
Related
I used to get an image from the frontend and store it in the backend.
How can I compress that image before storing it to the database?
Is there any gem for compressing an image?
or any other way to do that?
Take a look at carrierwave gem. It is a wrapper for ImageMagick, that lets you process uploaded files before storing them (e.g. resize, convert, optimize etc).
I want to upload some images and crop that without losing their quality in ruby on rails.For uploading images i have seen gems like paperclip,refile and CarrierWave.Which one will be better ?
Choosing between CarrierWave, Paperclip and Refile can be difficult at
times, but I would always recommend CarrierWave. It is the most
powerful and customizable of the three. It keeps your code organized
and clean, and is easy to test.
Refile isn't ready for prime time yet as it is still experiencing
growth pains and has DoS issues, and Paperclip is fairly simplistic.
For more information please see this acrticle: Best Rails image uploader - Paperclip vs. Carrierwave vs. Refile
In a Rails 4 app, we have some big images on our homepage (the dimensions are like 2400px on width) and naturally, their loading is quite slow.
What are the options to speed up loading them? One way is to decrease their quality => their size => faster loading.
But is there a Rails way how to pre-cache/compress them?
Thank you.
There is no "Rails way" to do this. This is a problem no matter what framework/language you're using and is best solved with a content delivery network (aka CDN)
One good practice is to run all images through ImageOptim before the final commit into your codebase. This tool does lossless compression, meaning the pixels don't change and it still reduced file size.
That's not a "Rails" solution but it is the best way I know to reduce filesize (About 10% usually) without sacrificing even any quality at all.
To give googler's some perspective on this, here are the facts:
There is no core Rails function to achieve this with assets
There are ways to achieve it with uploaded images
The problem you have is that since assets are manually added to Rails, there's no pre-processing you can do. If you wanted to optimize images for different environments, you'd have to create different resolutions of the images, and then use some logic to define them:
- assets
-- images
--- backgrounds
---- original.jpg
---- medium.jpg
---- small.jpg
You'd then be able to use a helper method to define the image you want:
#app/helpers/application_helper.rb
class ApplicationHelper
def size size, image
assets_path("#{File.basename(image)}/#{size.to_s}#{File.extname(image)}")
end
end
Ref: Get file name and extension in Ruby
This would allow you to call:
<%= image_tag size(:original, "backgrounds.jpg") %>
If you wanted to make this dynamic, you'd have to use CSS and set a background image, with corresponding #media queries assigning the different images as you need.
If you're talking about uploaded images, you'll be best looking into how Paperclip does it:
ImageMagick must be installed and Paperclip must have access to it. To ensure that it does, on your command line, run which convert (one of the ImageMagick utilities).
Paperclip uses ImageMagick to split images into different sizes. These are then stored in the /public/system folder, and is why you call #model.image.url(:original) when you're using it.
What I described above is basically how Paperclip works, excepting automatically using ImageMagick to make different image sizes
I want do same working like php timthumb do in ror I tried gems but not found result as I want.
Images are taking much time to load.
for example code in php :
Can check what I required on :
http://www.darrenhoyt.com/demo/timthumb/
I want same way in ror?
You can try minimagick gem to compress image size and quality.
For example:
image = MiniMagick::Image.open(YOUR_IMAGE)
image.resize "130x100" ## The thumbnail size
image.write(YOUR_THUMB)
When you upload an imagine you want to have different sized versions that have been processed from the original.
Carrier wave to upload the image and resize it
https://github.com/carrierwaveuploader/carrierwave
You can resize as documented here: Carrier Wave - Versions
To be more precise here are associated tutorials for what you want using carrierwave and various libs for the conversions: Image Dimensions
You can even change the image type to keep a standard and do more image processing with libraries. Here is how: Image Processing
Google's app engine has this speedy image resizing api which appears to perform significantly faster than the rails paperclip resizing alternative.
Anyone know of any rails/heroku friend image resizing api's that could work with paperclip to be a faster resizing solution?
Thanks
We've used Transloadit and it works well:
http://transloadit.com/
When you do images on heroku, you're usually storing them up on S3.
You can upload the file directly to S3, then use delayed job to process the file in the background. Your users will see a zippier/faster processing time.
Paperclip itself doesn't include image-resizing code; it simply plugs into tools like ImageMagick and MiniMagick. Have you tried some of these other engines?