【问题标题】:Getting "unable to load module" when trying to scale image with Rails / ImageMagick尝试使用 Rails / ImageMagick 缩放图像时出现“无法加载模块”
【发布时间】:2018-01-22 19:48:35
【问题描述】:

我想缩放通过表单上传的上传图片。我有一台 Mac(High Sierra)并且正在使用 Rails 5。我目前正在使用此代码和“rmagick”gem 来处理它

@person = Person.new(person_params)
if @person.image

  cur_time_in_ms = DateTime.now.strftime('%Q')
  file_location = "/tmp/file#{cur_time_in_ms}.ext"
  File.binwrite(file_location, @person.image.read)
  file = File.open(file_location, "rb")
  contents = file.read
  # Scale image appropriately
  img = Magick::Image::read(file_location).first
  @person.image = img.resize_to_fit(1000000, Rails.configuration.max_img_height)

但是当我上传图片时,我得到了错误

unable to load module `/usr/local/Cellar/imagemagick/6.9.2-4/lib/ImageMagick//modules-Q16/coders/png.la': file not found @ error/module.c/OpenModule/1300

上线

img = Magick::Image::read(file_location).first

我跑了

brew update && brew install imagemagick

没有任何错误,所以我不确定问题是什么。

编辑:响应给出的答案,这是我尝试运行“捆绑安装”时发生的情况

checking for presence of MagickWand API (ImageMagick version >= 6.9.0)... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/local/Cellar/ruby/2.4.2_1/bin/$(RUBY_BASE_NAME)
extconf.rb:104:in ``': No such file or directory - MagickWand-config (Errno::ENOENT)
    from extconf.rb:104:in `configure_compile_options'
    from extconf.rb:16:in `initialize'
    from extconf.rb:548:in `new'
    from extconf.rb:548:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/nataliab/.rvm/gems/ruby-2.4.0/extensions/x86_64-darwin-17/2.4.0/rmagick-2.16.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/rmagick-2.16.0 for inspection.
Results logged to /Users/nataliab/.rvm/gems/ruby-2.4.0/extensions/x86_64-darwin-17/2.4.0/rmagick-2.16.0/gem_make.out

An error occurred while installing rmagick (2.16.0), and Bundler cannot continue.
Make sure that `gem install rmagick -v '2.16.0'` succeeds before bundling.

In Gemfile:
  rmagick

【问题讨论】:

  • 你的路径中有一个双 //.. : ImageMagick//modules-Q16
  • 看看这个链接:Using Image Magick with rails - 他说的是设置一些环境变量来安装gem。也许这就是问题所在(请注意 - 文章已有 10 年历史)
  • 你需要控制器顶部的宝石,对吧?
  • 该错误可能是由多种问题引起的。您可以尝试删除 rmagick gem remove rmagick,然后使用 x11 安装 imagemagick,即 brew install imagemagick --with-x11 并重新安装 rmagick
  • @Dipil,谢谢您的建议,但尝试这样做会导致相同的错误。我什至在运行 brew install 然后重新安装 gems 后重新启动了整个系统。

标签: ruby-on-rails image resize imagemagick ruby-on-rails-5


【解决方案1】:

似乎无法加载libpng 模块。我建议尝试从源重新安装imagemagick,然后重新安装并链接libpng

brew uninstall imagemagick
brew install imagemagick --build-from-source
brew uninstall libpng
brew install libpng
brew link libpng

【讨论】:

  • 嗨,我运行了这些步骤,但是当需要启动我的服务器时,我收到一个错误,抱怨没有“rmagick”。所以我运行了“捆绑安装”并得到了你在编辑我的问题时看到的错误。
  • 尝试重新安装 gem 以及 gem uninstall rmagick &amp;&amp; gem install rmagick
猜你喜欢
  • 2020-04-30
  • 2014-07-28
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2018-03-05
  • 1970-01-01
  • 2013-02-23
相关资源
最近更新 更多