Search code examples
ruby-on-railsrubyresizecropminimagick

Resize / crop images for android devices (Rails)


I need to resize/crop images in different resolutions for Android App.

I know, that it's a common question, but I could not find a solution that would suit for me.

The problem is consist in the fact that different devices had different diagonal. The administrator upload to CMS only one image with size 668x1024 (for example). Then I need to send a picture to the device with such resolutions: 400x600, 540x960, etc. I can't just resize each image, because on the phone would appear a background-lines.

So, I'm or violating the aspect ratio (that's not good because picture becomes ugly) or should to crop the image. But because of the different proportions it's hard to do.

I tried mini-magick and g1nn13-image-science, but in the first one I can't take the current images width and height (and because of it I can't realize the cropping algorithm) and the second one gives me an error when I try to require image_science: "no such file to load -- inline"

All help and advices appreciated: some decisions about How To do it (maybe not to do cropping, but some another decision?) or gems that could help me.

Thanks.

ANSWER: I used fastimage gem for finding picture's current height and width and the method of minimagick gem image.shave.

Example:

current_w, current_h = FastImage.size(object.attach.path)
if current_w < current_h
  remove_h = ((current_h - height)/2).round
  image.shave("0x#{remove_h}")
  remove_w = ((current_w - width)/2).round
  image.shave("#{remove_w}x0")
end

Solution

  • There is good gem called dragonfly for rack apps.

    Basically you save base image, and then you can pass resolution in the request, and it saves new size in the cache.

    Here is some introduction to this gem

    Overview:

    Android ==(400x600)=> Rails(ImagesController#show) ===> image_url(400x600).jpg