RMagick 无法读取远程图像

发布于 2024-12-02 12:51:41 字数 928 浏览 1 评论 0 原文

我的环境是Linux centos,并使用ruby 1.8.7,代码如下:

require 'rubygems'
require 'RMagick'
Magick::Image.read("http://image.domain.com/image.darenhui.com/images/random_bg/01.jpg")[0]

它抛出如下错误:

in `read': no decode delegate for this image format `//image.domain.com/images/random_bg/01.jpg' @ error/constitute.c/ReadImage/532 (Magick::ImageMagickError), 

但如果我从本地读取:

    require 'rubygems'
    require 'RMagick'
    Magick::Image.read("/local/staticimages/random_bg/01.jpg")[0]

一切正常。 我运行识别-列表格式并参见下文:

     JPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (62)
     JPG* JPEG      rw-   Joint Photographic Experts Group JFIF format (62)

但是当我通过身份测试“http://image.domain.com/image.darenhui.com/images/random_bg/01.jpg”时失败,但“/”成功local/staticimages/random_bg/01.jpg"

有人能给我一些线索吗?先感谢您。

My env is Linux centos, and use ruby 1.8.7, and the code is here below:

require 'rubygems'
require 'RMagick'
Magick::Image.read("http://image.domain.com/image.darenhui.com/images/random_bg/01.jpg")[0]

it throws error like below:

in `read': no decode delegate for this image format `//image.domain.com/images/random_bg/01.jpg' @ error/constitute.c/ReadImage/532 (Magick::ImageMagickError), 

but if i read from local like:

    require 'rubygems'
    require 'RMagick'
    Magick::Image.read("/local/staticimages/random_bg/01.jpg")[0]

everything is ok.
I run identify -list format and see below:

     JPEG* JPEG      rw-   Joint Photographic Experts Group JFIF format (62)
     JPG* JPEG      rw-   Joint Photographic Experts Group JFIF format (62)

but when i test by identity for "http://image.domain.com/image.darenhui.com/images/random_bg/01.jpg" to fail, but success for "/local/staticimages/random_bg/01.jpg"

Can someone give me some clue? thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷你仙 2024-12-09 12:51:41

Magick::Image.read 不支持 URL 链接。但是您可以使用 ruby​​ open 方法读取远程图像:

 require 'rubygems'
 require 'RMagick'
 require "open-uri"


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

# crop = image.crop(10,10,200,300) You can crop this image too
# crop.write('C:/nameofyourNewImage.jpg') If you want to save the image you can use this line in windows 

Magick::Image.read does not support URL links. But you can read the remote image using ruby open method:

 require 'rubygems'
 require 'RMagick'
 require "open-uri"


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

# crop = image.crop(10,10,200,300) You can crop this image too
# crop.write('C:/nameofyourNewImage.jpg') If you want to save the image you can use this line in windows 
人间不值得 2024-12-09 12:51:41

快进到 2014 年 5 月,我能够……

require 'rubygems'
require 'RMagick'
include Magick
image = ImageList.new("http://www.jewelinfo4u.com/images/Gallery/ruby.jpg")

而且它确实有效。感觉很好。

(在 ImageMagick 6.8.8-9 Q16 上测试)

编辑:但似乎在 Heroku Cedar 堆栈上不起作用(ImageMagick 6.5.7-8 2012-08-17 Q16)。 :/

Fast forward to May 2014, and I'm able to just...

require 'rubygems'
require 'RMagick'
include Magick
image = ImageList.new("http://www.jewelinfo4u.com/images/Gallery/ruby.jpg")

And it just works. Feels good.

(Tested on ImageMagick 6.8.8-9 Q16)

EDIT: Does not seem work on Heroku Cedar stack (ImageMagick 6.5.7-8 2012-08-17 Q16) though. :/

七月上 2024-12-09 12:51:41

据我所知 Magick::Image.read 不支持 URL,仅支持文件/文件句柄 - 请参阅 http://www.imagemagick.org/RMagick/doc/image1.html#read 其中说

从指定文件中读取所有图像。

它由不支持 URL 的 C 函数 ReadImage 实现 - 请参阅第 394 行的 ImageMagick 源代码 此处

如果您想从 URL 打开图像,您需要以某种方式下载图像 - 到文件或流...然后您可以将该文件或流提供给 Magick...

As far as I can see Magick::Image.read does NOT support URLs, only files/file handles - see http://www.imagemagick.org/RMagick/doc/image1.html#read where it says that it

Reads all the images from the specified file.

It is implemented by the C function ReadImage which does not support URLs - see the ImageMagick source at line 394 here.

IF you want to open an image from a URL you need to download the image somehow - to a file or a stream... then you can feed that file or stream to Magick...

成熟的代价 2024-12-09 12:51:41

您可以使用 open-uri 从 url 读取文件,然后将其输入到 Rmagick 的 Image::from_blob 中。

示例:

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

image_url = 'http://example.com/image.jpg'

image = Magick::Image.from_blob(open(image_url).read).first

请注意,您还应该注意处理 open HTTP 请求引起的异常。即,如果您收到 404 或非图像响应,会发生什么情况?

You can read a file from a url with open-uri, then feed that into Rmagick's Image::from_blob.

Example:

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

image_url = 'http://example.com/image.jpg'

image = Magick::Image.from_blob(open(image_url).read).first

Note that you should additionally take care to handle exceptions arising from the open HTTP request. Ie, what happens if you get a 404, or a non-image response?

他是夢罘是命 2024-12-09 12:51:41

rmagick (4.1.2) 开始,Image.read 支持 URL 链接。就我而言,我正在读取图像,特别是来自 AWS S3 的 png 文件。这是通过本地主机。更新到最新版本应该可以解决此问题。

As of rmagick (4.1.2), Image.read does support URL links. In my case, I was reading an image, specifically a png file from AWS S3. This was via localhost. Updating to the latest version should fix this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文