【发布时间】:2010-08-15 23:39:47
【问题描述】:
图片展示脚本如下:
<% if user.image %>
<%= image_tag user.image.url('100x20') %>
<% end %>
如何在网络上仅以 20 x 20 的尺寸显示照片的右侧。就是把左边的80%剪掉,只显示全图的20%。
非常感谢!
【问题讨论】:
标签: javascript ruby-on-rails ruby rubygems
图片展示脚本如下:
<% if user.image %>
<%= image_tag user.image.url('100x20') %>
<% end %>
如何在网络上仅以 20 x 20 的尺寸显示照片的右侧。就是把左边的80%剪掉,只显示全图的20%。
非常感谢!
【问题讨论】:
标签: javascript ruby-on-rails ruby rubygems
您可以在服务器端使用RMagick。你需要
> 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,(或从您的数据库中,我从您的帖子中不清楚)
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
【讨论】: