Shalafister's
Shalafister's

Reputation: 881

Rails, reading image from url with rmagick

I am trying to read the image from url with Rmagick and carrierwave.

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 '****************'

But it gives error;

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

EDIT:

I have picture model and I chose mount_uploader as :image;

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

Here is image_uploader.rb to be able to edit the image before saving to 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

.... end

Upvotes: 1

Views: 1520

Answers (1)

LHH
LHH

Reputation: 3323

If you are using carrierwave, you should try like this -

so let's say, you have Image model(class)

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

it will download image from URL and store accordingly.

Here is Github URL(just for reference) - https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Upload-remote-image-urls-to-your-seedfile

Upvotes: 1

Related Questions