LoginSignup
15
19

More than 5 years have passed since last update.

railsでcarrierwaveとimagemagick使って画像圧縮&画像アップロードする。

Last updated at Posted at 2015-10-10

画像を圧縮して保存するためにcarrierwaveとimagemagickとpiet使ってみた。

環境

Macbook Pro (2.6 GHz Intel Core i5)
macOS X 10.10.5
rails 4.2.3
carrierwave 0.10.0
imagemagick 6.9.1-10
rmagick 2.15.4
piet 0.2.2

Gemfile

一通り追加してbundle installする。

Gemfile
  gem 'carrierwave'
  gem 'piet'
  gem 'rmagick'
$ bundle install

imagemagick

普通に最新をインストールしてrails起動したらrmagickとバージョン合ってねーと怒られた。

$ rails s
Uncaught exception: This installation of RMagick was configured with ImageMagick 6.9.1 but ImageMagick 6.9.2-3 is in use.

brew logで適切なバージョン(ここでは6.9.1)を探す。

$ brew log imagemagick

狙いのバージョンを取ってくる。

$ git checkout 20376e8fe12529cbac4cbe1a94654b90ae082335 /usr/local/Library/Formula/imagemagick.rb 

取れたか確認。

$ brew info imagemagick
imagemagick: stable 6.9.1-10 (bottled), HEAD
Tools and libraries to manipulate images in many formats
http://www.imagemagick.org
/usr/local/Cellar/imagemagick/6.9.2-3 (1448 files, 22M) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/imagemagick.rb
==> Dependencies
...

再インストールする。先にunlinkするのを忘れずに。

$ brew unlink imagemagick
Unlinking /usr/local/Cellar/imagemagick/6.9.2-3... 72 symlinks removed

$ brew install imagemagick
==> Downloading https://homebrew.bintray.com/bottles/imagemagick-6.9.1-10.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring imagemagick-6.9.1-10.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/imagemagick/6.9.1-10: 1447 files, 22M

これでOK。

optipng

homebrewでインストールした。

$ brew install optipng
==> Downloading https://downloads.sourceforge.net/project/optipng/OptiPNG/optipng-0.7.5/optipng-0.7.5.tar.gz
==> Downloading from http://jaist.dl.sourceforge.net/project/optipng/OptiPNG/optipng-0.7.5/optipng-0.7.5.tar.gz
######################################################################## 100.0%
==> Patching
patching file src/optipng/osys.c
==> ./configure --with-system-zlib --prefix=/usr/local/Cellar/optipng/0.7.5 --mandir=/usr/local/Cellar/optipng/0.7.5/share/man
==> make install
🍺  /usr/local/Cellar/optipng/0.7.5: 6 files, 156K, built in 14 seconds

jpegoptim

こちらも同様。

$ brew install jpegoptim
==> Downloading https://homebrew.bintray.com/bottles/jpegoptim-1.4.3.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring jpegoptim-1.4.3.yosemite.bottle.1.tar.gz
🍺  /usr/local/Cellar/jpegoptim/1.4.3: 6 files, 72K

carrierwaveの設定

こんな感じで。

app/aploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  include Piet::CarrierWaveExtension

  # resize_to_fit(): そのサイズに合うようにリサイズ。(大きくなることがある。)
  # resize_to_limit(): そのサイズより大きい画像のときに、そのサイズに合うようにリサイズ。(大きくなることはない。)
  version :icon do
    process resize_to_limit: [150, 150]
  end

  process convert: 'jpg'
  process optimize: [quality: 50]

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def filename
    'hogehoge.jpg' if original_filename.present?
  end
end

これでいい感じに圧縮かかるはず。
圧縮はそこそこ時間かかる。(400KBの画像を3枚に圧縮かけるのに1秒くらい)

15
19
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
15
19