Which Plugins/Gems Should I Use to Dynamically Generate Thumbnails On-The-Fly in Rails 3

Which plugins/gems should I use to dynamically generate thumbnails on-the-fly in Rails 3?

You should rmagick coupled with Paperclip. In this way you can specify the dimensions (and many other attributes of the images), and make thumbnails on the fly.

On top of this, I would also add in delayed_job which will background the process of the images so if any of them are provided by the client, they won't have to wait for it to complete client-side.

rMagick : http://github.com/rmagick/rmagick

Paperclip: http://github.com/thoughtbot/paperclip

delayed_job: http://github.com/collectiveidea/delayed_job

Generating automatic thumbnails of recent posts

Will the content come from your app? If yes, then I reckon you are looking for a way to layout your list of objects like how they did it in kickstarter and indiegogo. There are different libraries that you can use. Here are some of the libraries which you can use:


masonry: http://masonry.desandro.com/

isotope: https://github.com/desandro/isotope

packery: http://packery.metafizzy.co/

These libraries will help you to create brick / grid layouts.

Or, do you want to create previews from links? Then maybe this gem can help you as well: https://github.com/gottfrois/link_thumbnailer

Hope this helps!

Shrine file uploads - One uploader for all file types OR one uploader per file type?

It's recommended to have an uploader per file type, because you will want to handle attachment logic differently depending on the file type, and even choose different storage service.

For example, for images you might want to either process thumbnails yourself, or generate them on-the-fly using a service like Cloudinary.

On the other hand, videos need to be processed differently using different tools, and since that can take much longer you might want to use services like Zencoder.

In general you might want to load different Shrine plugins depending on the size of files you're uploading, how long does processing take, where are files stored etc. You can still have a BaseUploader which would have some common attachment logic, and then have ImageUploader, VideoUploader, AudioUploader inherit from it.

These uploaders can also then branch into multiple uploaders depending on requirements, for example PhotoUploader or CoverUploader. But you definitely want to have individual uploaders for different file types.


For your second question, you can override the Shrine attachment setter:

class Post
include FileUploader[:file]

def file=(value)
super
self.file_type = (file ? file.mime_type : nil)
end
end

Tools to make CSS sprites?

This will do 90% of the work for you: CSS Sprite Generator. You'll still need to edit the rules yourself, but the tool will give you the code fragments you need for the new CSS file.

Create thumbnails on the fly with cache or on upload?

Use the second option, if you don't have too much storage and first if you don't have too much CPU.

Or you can combine these: generate and store the image at the first open of the php thumbnails generator and nex time just give back the cached image.

With this solution you'll have only the necessary images and if you want you can delete sometimes the older ones.

How should I go about providing image previews of sites while using Google's Web Search API?

So as you pointed out in your question, there are two approaches that I can see to your issue:

  1. Use an external service to render and host the images.
  2. Render and host the images yourself.

I'm no expert in field, but my Googling has so far only returned services that allow you to generate thumbnails and not full-size screenshots (like the few mentioned here). If there are hosted services out there that will do this for you, I wasn't able to find them easily.

So, that leaves #2. For this, my first instinct was to look for a ruby library that could generate an image from a webpage, which quickly led me to IMGKit (there may be others, but this one looked clean and simple). With this library, you can easily pass in a URL and it will use the webkit engine to generate a screenshot of the page for you. From there, I would save it to wherever your assets are stored (like Amazon S3) using a file attachment gem like Paperclip or CarrierWave (railscast). Store your attachment with a field recording the original URL you passed to IMGKit from WSAPI (Web Search API) so that you can compare against it on subsequent searches and use the cached version instead of re-rendering the preview. You can also use the created_at field for your attachment model to throw in some "if older than x days, refresh the image" type logic. Lastly, I'd put this all in a background job using something like resque (railscast) so that the user isn't blocked when waiting for screenshots to render. Pass the array of returned URLs from WSAPI to background workers in resque that will generate the images via IMGKit--saving them to S3 via paperclip/carrierwave, basically. All of these projects are well-documented, and the Railscasts will walk you through the basics of the resque and carrierwave gems.

I haven't crunched the numbers, but you can against hosting the images yourself on S3 versus any other external provider of web thumbnail generation. Of course, doing it yourself gives you full control over how the image looks (quality, format, etc.), whereas most of the services I've come across only offer a small thumbnail, so there's something to be said for that. If you don't cache the images from previous searches, then your costs reduces even further, since you'll always be rendering the images on the fly. However I suspect that this won't scale very well, as you may end up paying a lot more for server power (for IMGKit and image processing) and bandwidth (for external requests to fetch the source HTML for IMGKit). I'd be sure to include some metrics in your project to attach some exact numbers to the kind of requests you're dealing with to help determine what the subsequent costs would be.

Anywho, that would be my high-level approach. I hope it helps some.

dynamic image sizes for uploaded images in rails3

You can look at something like Dragonfly to process your images instead of Paperclip. It puts the sizing in the view instead of the model and utilizes some caching mechanisms to help with performance.

Image resizing + hosting (language independent)

My company launched a service like this : cloudimage.io, but a few others exists too:

  • http://cloudimage.io
  • http://cloudinary.com
  • http://imageresizing.net
  • http://imgix.com

For cloudimage.io, if you have an image such as http://mywebsite.com/photo.jpg, you can simply resize it at 400px for mobile like this : http://cloudimage.io/t/resize/400/mywebsite.com/photo.jpg.

Then the mobile will download the images directly in the right size. On a mobile application, if your app is used worldwide, having your images stored on a worldwide CDN is usually better for the user experience.

You'll find a lot of examples on the above websites.

Should I create a slug on the fly or store in DB?

You might need to take another thing into consideration, what if you want the user/yourself to be able to define their own slugs. Maybe the algorithm isn't always sufficient.

If so you more or less need to store it in the database anyhow.

If not I don't think it matters much, you could generate them on the fly, but if you are unsure whether you want to change them or not let them be in the database. In my eyes there is no real performance issue with either method (unless the on-the-fly generation is very slow or something like that).

Choose the one which is the most flexible.



Related Topics



Leave a reply



Submit