【问题标题】:Using rgmagick and getting error for file not found使用 rgmagick 并获取文件未找到错误
【发布时间】:2013-10-07 15:26:50
【问题描述】:

使用 rmagick 查找图像的颜色..

img = Magick::Image.read("#{Rails.root}/public#{@design.photo.url(:profile)}").first

这是错误

unable to open image `/tmt/public/system/designs/photos/000/000/005/profile/5.jpg?1380037883': No such file or directory @ error/blob.c/OpenBlob/2646

该文件指向图像的确切位置,也许扩展名 (?1380037883) 将其丢弃,但我似乎无法解决问题。你们都知道是怎么回事吗?也许您已经使用 rgmagick 看到过类似的问题...这是我的 gemfile

gem 'rmagick', :require => 'RMagick'

如果我指向我的 app/assets/images 目录中的图像,它可以完美运行...只是当我使用变量查找用户上传的图像时出现此错误...

例如,这非常有效

img = Magick::Image.read("#{Rails.root}/app/assets/images/ruby.png").first

【问题讨论】:

  • 只需使用img = Magick::Image.read @design.photo.path(:profile) 即可返回文件的完整路径
  • 这不会返回任何东西
  • @design.photo.url(:profile) 是回形针代码,对吧?如果是,那么@design.photo.path 应该返回文件的完整路径。
  • 是的回形针..好电话....最终起作用的是img = Magick::Image.read(@design.photo.path).first

标签: ruby-on-rails ruby-on-rails-4 rmagick


【解决方案1】:

您应该使用.path 方法而不是.url

  • .url 返回文件的相对路径
  • .path 返回文件的绝对路径

在你的情况下:

img = Magick::Image.read( @design.photo.path ).first

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多