【问题标题】:how to save image from url in paperclip rails如何从回形针导轨中的url保存图像
【发布时间】:2014-07-08 08:53:09
【问题描述】:

如何在回形针导轨中保存 url 中的图像?

我有模型文章:

require "open-uri"
class Article < ActiveRecord::Base
attr_accessible :feed_id, :url, :name, :author, :image, :image_caption, :text
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

def picture_from_url(url)
  self.image = URI.parse(url)
  self.save
end

def self.add_articles       
      article = create!(
        :feed_id => 1,
        :name => "test",
        :author => "udin",
        :image_caption => "test caption",
        :text => "bla..bla..bla",
      )

 image_url = "http://img.antaranews.com/new/2013/10/big/20131030047.jpg"
      article.picture_from_url(image_url)
    end
  end

如何将图片网址保存到我的公用文件夹?

【问题讨论】:

  • 如果我尝试使用open-uri打开(image_url),图像只是保存到tmp...如何将图像文件保存到公共?
  • 我想将图像文件从 url 保存到我的本地目录 @marvwhere

标签: ruby-on-rails paperclip open-uri


【解决方案1】:

使用 gem minimagick 解决我的问题

file = MiniMagick::Image.open(image_url)
save_to_local = file.write  "public/assets/file_name.jpg"

【讨论】:

    猜你喜欢
    • 2011-05-02
    • 2014-04-11
    • 1970-01-01
    • 2012-09-29
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    相关资源
    最近更新 更多