简体   繁体   English

通过回形针从URL保存图像

[英]Save image from URL by paperclip

请为我推荐一种通过回形针从URL保存图像的方法。

In Paperclip 3.1.4 it's become even simpler. 在Paperclip 3.1.4中,它变得更加简单。

def picture_from_url(url)
  self.picture = URI.parse(url)
end

This is slightly better than open(url). 这比open(url)略好。 Because with open(url) you're going to get "stringio.txt" as the filename. 因为使用open(url),您将获得“ stringio.txt”作为文件名。 With the above you're going to get a proper name of the file based on the URL. 通过上面的内容,您将基于URL获得文件的正确名称。 ie

self.picture = URI.parse("http://something.com/blah/avatar.png")

self.picture_file_name    # => "avatar.png"
self.picture_content_type # => "image/png"

Here is a simple way: 这是一个简单的方法:

require "open-uri"

class User < ActiveRecord::Base
  has_attached_file :picture

  def picture_from_url(url)
    self.picture = open(url)
  end
end

Then simply : 然后简单地:

user.picture_from_url "http://www.google.com/images/logos/ps_logo2.png"

It didn't work for me until I used "open" for parsed URI. 在我对解析的URI使用“打开”之前,它对我不起作用。 once I added "open" it worked! 一旦我添加了“打开”,它就起作用了!

def picture_from_url(url)
  self.picture = URI.parse(url).open
end

My paperclip version is 4.2.1 我的回形针版本是4.2.1

Before open it wouldn't detect the content type right, because it wasn't a file. 在打开之前,它不是正确的内容类型,因为它不是文件。 It would say image_content_type: "binary/octet-stream", and even if I override it with the right content type it wouldn't work. 它会说image_content_type:“ binary / octet-stream”,即使我用正确的内容类型覆盖它也不起作用。

首先将带有curb宝石的图像下载到TempFile ,然后只需分配tempfile对象并保存模型。

As those are old Answer's here's a newer one: 由于这些都是旧答案,因此这里是一个较新的答案:

Add Image Remote URL to your desired Controller in the Database 将图像远程URL添加到数据库中所需的控制器

$ rails generate migration AddImageRemoteUrlToYour_Controller image_remote_url:string
$ rake db:migrate

Edit your Model 编辑模型

attr_accessible :description, :image, :image_remote_url
.
.
.
def image_remote_url=(url_value)
  self.image = URI.parse(url_value) unless url_value.blank?
  super
end

*In Rails4 you have to add the attr_accessible in the Controller. *在Rails4中,您必须在Controller中添加attr_accessible。

Update your form, if you allow other to upload an Image from a URL 如果您允许其他人从URL上传图像,请更新您的表单

<%= f.input :image_remote_url, label: "Enter a URL" %>

It may helpful to you. 它可能对您有帮助。 Here is the code using paperclip and image present in remote URL . 这是使用回形针和远程URL中存在的图像的代码。

require 'rubygems'
require 'open-uri'
require 'paperclip'
model.update_attribute(:photo,open(website_vehicle.image_url))

In model 在模型中

class Model < ActiveRecord::Base
  has_attached_file :photo, :styles => { :small => "150x150>", :thumb => "75x75>" }
end

This is a hardcore method: 这是一个硬核方法:

original_url = url.gsub(/\?.*$/, '')
filename = original_url.gsub(/^.*\//, '')
extension = File.extname(filename)

temp_images = Magick::Image.from_blob open(url).read
temp_images[0].write(url = "/tmp/#{Uuid.uuid}#{extension}")

self.file = File.open(url)

where Uuid.uuid just makes some random ID. 其中Uuid.uuid只是使一些随机ID。

Into official documentation is reported here https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL 进入官方文档的报告在这里https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL

Anyway it seems not updated, because in last version of paperclip something has changed and this line of code is no more valid: 无论如何,它似乎没有更新,因为在回形针的最新版本中,某些内容已更改,并且此行代码不再有效:

user.picture = URI.parse(url)

It raise an error, in particular this error is raised: 它引发一个错误,特别是引发此错误:

Paperclip::AdapterRegistry::NoHandlerError: No handler found for #<URI:: ...

The new correct syntax is this one: 新的正确语法是以下一种:

url = "https://www.example.com/photo.jpeg"
user.picture = Paperclip.io_adapters.for(URI.parse(url).to_s, { hash_digest: Digest::MD5 })

Also we need to add these lines into config/initializers/paperclip.rb file: 另外,我们需要将这些行添加到config / initializers / paperclip.rb文件中:

Paperclip::DataUriAdapter.register
Paperclip::HttpUrlProxyAdapter.register

Tested this with paperclip version 5.3.0 and it works. 在回形针版本5.3.0对此进行了测试,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM