【问题标题】:Rails, reading image from url with rmagickRails,使用 rmagick 从 url 读取图像
【发布时间】:2017-12-06 20:42:13
【问题描述】:

我正在尝试使用 Rmagick 和 carrierwave 从 url 读取图像。

require 'open-uri'
require 'rubygems'
require 'Rmagick'


image = Magick::ImageList.new  
urlimage = open("https://www.jewelinfo4u.com/images/Gallery/ruby.jpg") # Image Remote URL 
image.from_blob(urlimage.read)

puts '****************'
puts image
puts '****************'

但它给出了错误;

no decode delegate for this image format `JPEG' @ error/blob.c/BlobToImage/353

编辑:

我有图片模型,我选择 mount_uploader 作为 :image;

class Picture < ActiveRecord::Base
  belongs_to :car
  mount_uploader :image, ImageUploader
end

这是 image_uploader.rb 以便能够在保存到 S3 Amazon 之前编辑图像。;

# encoding: utf-8
#require File.join(Rails.root, "lib", "carrier_wave", "delayed_job") # Upload pics with delayed job
class ImageUploader < CarrierWave::Uploader::Base
#include CarrierWave::Delayed::Job # Upload pics with delayed job


  include CarrierWave::RMagick


  storage :fog
  include CarrierWave::MimeTypes
  process :set_content_type


  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end


  version :slider do
    process :adjust_image
    process :optimize
  end

.... 结束

【问题讨论】:

    标签: ruby-on-rails url rmagick


    【解决方案1】:

    如果你使用的是carrierwave,你应该这样尝试——

    假设你有图像模型(类)

    image = Image.new
    image.remote_image_url = "https://www.jewelinfo4u.com/images/Gallery/ruby.jpg"
    image.save
    

    它将从 URL 下载图像并相应地存储。

    这里是 Github URL(仅供参考)-https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Upload-remote-image-urls-to-your-seedfile

    【讨论】:

    • 然后我得到这个错误它没有保存它。 <:errors:0x007f975afe1e58 id: nil name: car_id: created_at: updated_at: image: position:>, @messages={:image=> [“使用 rmagick 操作失败,可能不是图像?原始错误:此图像格式 `JPEG' 没有解码委托@error/constitute.c/ReadImage/504”]}>
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多