Javascript 如何用ruby脚本剪切照片并在web上显示?

Javascript 如何用ruby脚本剪切照片并在web上显示?,javascript,ruby-on-rails,ruby,rubygems,Javascript,Ruby On Rails,Ruby,Rubygems,照片显示sript如下所示: <% if user.image %> <%= image_tag user.image.url('100x20') %> <% end %> 我怎么能在网上只显示照片的右侧20 x 20。这意味着剪掉左边的80%,只显示整个画面的20% 非常感谢 您可以在服务器端使用。你需要 > gem install rmagick 或者使用您拥有的任何包管理器。一旦安装,您应该能够 要运行以下命令:

照片显示sript如下所示:

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

我怎么能在网上只显示照片的右侧20 x 20。这意味着剪掉左边的80%,只显示整个画面的20%

非常感谢

您可以在服务器端使用。你需要

> gem install rmagick
或者使用您拥有的任何包管理器。一旦安装,您应该能够 要运行以下命令:

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
这将仅显示图像的右上角

如果你想裁剪,你需要得到链接到图像,不管你喜欢什么。可能是使用Net::HTTP(或者从数据库中,我在您的帖子中不清楚)


您好,谢谢您,您能提供更多详细信息吗?我如何在服务器端使用RMagick?是一个选项吗?在显示时显示图像的一部分而不是在服务器上裁剪图像会有什么好处吗?
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