How to crop and show external image with specific size?

I get image url via API. It usualy have 360x200 but sometimes 233x350 etc…

I would like to crop them all to specific size 260x186. And show them using

<%= image_tag image_url if hotel.images.first %>

I dont want to store this images.

I get image url via API. It usualy have 360x200 but sometimes 233x350 etc…

I would like to crop them all to specific size 260x186. And show them using

<%= image_tag image_url if hotel.images.first %>

I dont want to store this images.

You can use rmagic to crop the images but there’s going to be a time-cost here if you don’t have the images stored. Probably the best bet is to have image_url hit your application, crop the image and then use send_file, then delete the image.

You might not want the request to go through your whole Rails stack here from a load POV. 10 images per page = an extra 10 requests to your application and these requests will take a bit of time to process the image so you could end up blocking actual requests. I’d setup a separate app using either using Sinatra, or a cut-down version of Rails with just the necessary stuff loaded. There’s also a big risk of caning your CPU here, if you have high volume.

Storage is cheap - why not store them?

tl;dr; Real-time image-cropping strikes me as a bad idea, but you could use rmagic on a separate application/server to do it.

Take a look at Dragonfly, too. You get an API to use ImageMagick against your photos -- to crop, scale, grayscale, whatever -- and the images are cached. It runs in Rack, so it's a ready-to-roll solution rather than you having to build a Sinatra app to do this. The only downside is that, out of the box, the URLs are amazingly long.

Walter

Can I just check that you do want to *crop* them, not just resize to a given size, in which case you could use the :size option of image_tag.

Colin

hi there, quick searching on Google brings me here.and Im working on image cropping ,Is the kooboo source codes written in .Net? If yes ,Plz give me some detail guide,thx in adv.as a beginner in image programming ,I just can't figure out which open source is best siuting me,like image crop sample codes (How to C#: Crop Image according to Specified Size) found by Google as well as your site.Anysuggestion is appreciated.

Absolutely no idea how you managed to get here.

This is about the Ruby on Rails web application development framework. Definitely nothing to with .NET, nor image processing.

I can't direct you to any .NET resources, but I can direct you to ImageMagick, which is the swiss army knife of image processing:

    http://www.imagemagick.org

Demonstrated below is the sample method for image cropping of an image processing library. Besides image cropping, image resizing, image scaling and flipping can also be hanlded with the simplest APIs.

int ApplyCrop(REImage img, int x, int y, int width, int height)