views:

37

answers:

1

Hi there, the photo display sript is below:

    <% if user.image %>
      <%= image_tag user.image.url('100x20') %>
    <% end %>

How can I only showing the right side of the photo by 20 x 20 on the web. it mean cut off the 80% on the left show only 20% of the whole picture.

Many thanks!

+2  A: 

You can use RMagick on the server-side. You'll need to

> gem install rmagick

Or use whatever package manager you have. Once it's installed you should be able to run the following:

require 'RMagick'

def cropImage(input_filename)
    original = Magick::ImageList.new(filename)
    # NorthEast says take from the top, right, corner. Start
    # at 20,20 and make the final image 20x20.
    crop = original.crop(NorthEast, 20,20,20,20)
    output_filename = "cropped-foo.jpg"
    crop.write(output_filename)
    return  output_filename
end

This will show only the top-right corner of an image.

If you want to crop you'll need to get the linked-to image, however you'd like. Perhaps using Net::HTTP, (or from your DB, it's unclear to me from your post)

Net::HTTP.start("EXAMPLE.COM") { |http|
  resp = http.get("/path/to/X.jpg")
  open("orig.jpg", "wb") do |file|
    file.write(resp.body)
  end

   cropped_filename = cropImage("orig.jpg")
  #put the resulting file in some location where 
  #you can get to it and add that link to your 
  #template. 
end
Paul Rubel
Hi there, thanks for that, could you give more details how can I use RMagick on the server side please?
xuanyinwen
edited to add a bit more
Paul Rubel
Are [CSS Masks](http://webkit.org/blog/181/css-masks/) an option? Would showing a portion of an image at display time instead of cropping the image on the server offer any advantages?
Andy Atkinson