17
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RMagick で正方形のサムネイルを作成する

Posted at

main_back.png

「RMagick 正方形」でググると Railsでアップロードした画像からRMagickで正方形のサムネイルを作成 - Qiita という記事が出てきますが、こちらは正方形の白い画像の上に縮小した画像を貼った状態となり、あまり期待される操作だと思わなかったのでこちらにメモ

f1618d2dd8dc86c8c6435afa1062bc4c.png

RMagick 版

def create_square_image(rmagick, size)
  narrow = rmagick.columns > rmagick.rows ? rmagick.rows : rmagick.columns
  rmagick.crop(Magick::CenterGravity, narrow, narrow).resize(size, size)
end

MiniMagick 版

def create_square_image(magick, size)
  narrow = magick[:width] > magick[:height] ? magick[:height] : magick[:width]
  magick.combine_options do |c|
   c.gravity "center"
   c.crop "#{narrow}x#{narrow}+0+0"
  end
  magick.resize "#{size}x#{size}"
end

82d4dde70cfab5cb16619688d41d4cd8.png
こうなります

17
18
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
17
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?