【问题标题】:how to cut out the photo and display on web in ruby script?如何在 ruby​​ 脚本中剪切照片并在网络上显示?
【发布时间】: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


    【解决方案1】:

    您可以在服务器端使用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
    

    【讨论】:

    • 您好,谢谢您,您能否详细说明如何在服务器端使用 RMagick?
    • CSS Masks 是一个选项吗?在显示时显示图像的一部分而不是在服务器上裁剪图像有什么好处吗?
    猜你喜欢
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    相关资源
    最近更新 更多