blog.unresolved.xyz

rmagickで画像合成をするときにradiusをかける

Thu May 14 2020

    最近やけに画像合成をサーバサイドでやることがあるんだけど、角丸の画像合成ってできるのかなって気になったので調べた & できたのでメモしときたい。

    rmagickを用いた角丸画像合成の実装

    もう完全に動かしたコードのコピペだけどこんな感じ。ビジネス的な命名されてたところは雑に直したので動かなくなってるかもしれない。

    1class OgpGenerator < OgpGenerator::Base 2 include Magick 3 4 COMPOSIT_WIDTH = 300 5 COMPOSIT_HEIGHT = 200 6 7 def initialize(file_object:) 8 @file_object = file_object 9 end 10 11 def exec! 12 img = Magick::ImageList.new(Rails.root.join("app/javascript/images/og_image_background/v3.jpg")) 13 composit_image = Magick::ImageList.new("app/javascript/images/composit.png") 14 composit_image = rounded(flag_image, "#{COMPOSIT_WIDTH}x#{COMPOSIT_HEIGHT}>", 22) 15 img.write(@file_object.path) 16 img.destroy! 17 18 true 19 end 20 21 private 22 23 def rounded(source_image, geometry_string, radius = 10) 24 source_image.change_geometry(geometry_string) do |cols, rows, img| 25 # Make a resized copy of the image 26 thumb = img.resize(cols, rows) 27 28 # Set a transparent background: pixels that are transparent will be 29 # discarded from the source image. 30 mask = Image.new(cols, rows) { self.background_color = 'transparent' } 31 32 # Create a white rectangle with rounded corners. This will become the 33 # mask for the area you want to retain in the original image. 34 Draw.new.stroke('none').stroke_width(0).fill('white') 35 .roundrectangle(0, 0, cols, rows, radius, radius) 36 .draw(mask) 37 38 # Apply the mask and write it out 39 thumb.composite!(mask, 0, 0, Magick::CopyAlphaCompositeOp) 40 thumb 41 end 42 end 43end

    rounded メソッドに対象の画像とサイズ、角丸の大きさを渡して上から角丸になるように生成した画像を重ね合わせて合成した画像インスタンスを返してる。

    新しい画像インスタンスを作ってるので元のインスタンスは変わらないことに注意(なので代入してる)。

    ほとんど後述するSOFのコードのコピペです。

    参考にさせてもらいました