0
0

More than 1 year has passed since last update.

MiniMagick での画像のリサイズとサムネイル 

Posted at

アップロード時に画像のリサイズとサムネイルの作成をする

参考にした記事は以下

開発環境

ruby 2.6.5
Ruby on Rails 5.2.5

前提

minimagic がインストールされている
carrierwave を導入している

リサイズ

uploadrs/image_uploader.rb
  # include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

コメントにされている上記の

include CarrierWave::MiniMagick

をコメント解除
その後

uploadrs/image_uploader.rb
 process resize_to_fit: [200, 200]

このワンラインをいれることでアップロード時に自動的にリサイズされるようになる
(縦横比そのままの 200*200)

サムネイル

uploadrs/image_uploader.rb
version :thumb do
    process resize_to_fit: [50, 50]
end

これをいれることで、好きなview ファイル内で

好きなView
 <%= image_tag(@user.user_image_url(:thumb)) %>

とするとサムネイルとして表示出来る
これの意味は

[モデル名][画像パス保存の属性名]_urlのメソッドには引数にversionのシンボルが渡せます.
(引用元参照)

また、サムネイルで画像を丸くするときには CSS で border-radius をあてる

0
0
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
0
0