LoginSignup
9
10

More than 3 years have passed since last update.

【Rails】ImageMagick(RMajick)を導入する方法

Posted at

ImageMajickとは

ImageMagick(イメージマジック)は画像を操作したり表示したりするためのソフトウェア。
GIF、JPEG、PNG、PDF、TIFFなど100種類以上の画像ファイルフォーマットに対応しており、プログラム上からの画像の変換・編集などに必要な多数の機能を備えている。
「画像のアップロード → サムネイル生成 」など、画像を加工・変形するWEBサービスを組みたい場合に使う。

Imagemagickの使い方日本語マニュアル

導入

諸々の画像処理をするため、ImageMajickを使いたい。

ただこれはRubyのものではなく、
RubyだとRMagickというgemで使えるようになるらしいのでこちらを入れる。

# 追加
gem 'rmagick'

# => bundle install

ただ、以下のようなエラーが起きる。

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-4.0.0/ext/RMagick
/Users/uenoyuuki/.rbenv/versions/2.5.0/bin/ruby -r ./siteconf20190916-2480-i0d2pg.rb extconf.rb
checking for brew... yes
checking for clang... yes
checking for pkg-config... yes


ERROR: Can't install RMagick 4.0.0. Can't find ImageMagick with pkg-config


*** 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
        --without-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=/Users/uenoyuuki/.rbenv/versions/2.5.0/bin/$(RUBY_BASE_NAME)

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

  /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/rmagick-4.0.0/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/rmagick-4.0.0 for inspection.
Results logged to /Users/uenoyuuki/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/extensions/x86_64-darwin-18/2.5.0-static/rmagick-4.0.0/gem_make.out

An error occurred while installing rmagick (4.0.0), and Bundler cannot continue.
Make sure that `gem install rmagick -v '4.0.0' --source 'https://rubygems.org/'` succeeds before bundling.

先にImagaMajick自体のインストールが必要らしい。

OSがAmazon-Linuxなので、yumを使ってインストールする。

$ sudo yum -y install ImageMagick ImageMagick-devel
# 再度追加
gem 'rmagick'

# => bundle install

成功…!

失敗談

ImageMagickを入れるまでの失敗として、
今までパッケージマネージャをノリで使っていたため、
それぞれの違いをわかっておらず、変に時間がかかった。

# Amazon-Linuxでは使えない

# Mac OS
$ brew install imagemagick@6

# Ubunts
$ sudo apt-get install imagemagick libmagick++-dev

なんのOSか分からない!なんてことはないはずだが、
最悪これでもOSを確認できる。

$ cat /etc/*-release

参考

Active StorageのVariantの指定方法いろいろ
RailsでRMagickを使う
[Gem] RMagick をCentOSでインストール
Cloud9でrmagickを使おうとしたらエラーって解決した話
aptコマンドのインストールができない

9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10