Zapisz obraz z adresu URL przez paperclip
Zaproponuj mi sposób na zapisanie obrazu z adresu URL za pomocą spinacza.
7 answers
Oto prosty sposób:
require "open-uri"
class User < ActiveRecord::Base
has_attached_file :picture
def picture_from_url(url)
self.picture = open(url)
end
end
Wtedy po prostu:
user.picture_from_url "http://www.google.com/images/logos/ps_logo2.png"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-29 09:50:37
W Paperclip 3.1.4 stało się jeszcze prostsze.
def picture_from_url(url)
self.picture = URI.parse(url)
end
Jest to nieco lepsze niż open (url). Ponieważ z open (url) dostaniesz "stringio.txt" jako nazwę pliku. Dzięki temu uzyskasz właściwą nazwę pliku na podstawie adresu URL. tj.
self.picture = URI.parse("http://something.com/blah/avatar.png")
self.picture_file_name # => "avatar.png"
self.picture_content_type # => "image/png"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-06 15:43:38
Najpierw pobierz obraz z curb
gem do TempFile
, a następnie po prostu Przypisz obiekt tempfile i zapisz swój model.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-29 08:28:14
Nie zadziałało, dopóki nie użyłem "open" dla parsowanego URI. po dodaniu "open" zadziałało!
def picture_from_url(url)
self.picture = URI.parse(url).open
end
Moja wersja paperclip to 4.2.1
Przed otwarciem Nie wykrywał właściwego typu zawartości, ponieważ nie był to plik. To powie image_content_type: "binary / octet-stream", i nawet jeśli nadpisuję go odpowiednim typem zawartości, to nie zadziała.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-21 20:06:27
To może Ci pomóc. Oto kod za pomocą spinacza i obrazek obecny w zdalnym adresie URL .
require 'rubygems'
require 'open-uri'
require 'paperclip'
model.update_attribute(:photo,open(website_vehicle.image_url))
W modelu
class Model < ActiveRecord::Base
has_attached_file :photo, :styles => { :small => "150x150>", :thumb => "75x75>" }
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-21 05:20:42
Jak to jest stara odpowiedź to jest nowsza:
Dodaj Zdalny adres URL obrazu do żądanego kontrolera w bazie danych
$ rails generate migration AddImageRemoteUrlToYour_Controller image_remote_url:string
$ rake db:migrate
Edytuj swój 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
* w Rails4 musisz dodać attr_accessible w kontrolerze.
Zaktualizuj swój formularz, jeśli pozwolisz innym przesłać obraz z adresu URL
<%= f.input :image_remote_url, label: "Enter a URL" %>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-26 18:57:17
Jest to metoda hardcore:
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)
Gdzie Uuid.uuid robi losowe ID.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-06 22:23:01