Paperclipでオレオレmasker
ImageMagickの参考サイト
http://www.imagemagick.org/Usage/thumbnails/#border_overlay
記念の残しておく。maskのための画像は別途用意しておくこと。
paperclipのgithubのドキュメントとthumbnailプロセッサーのソースを読んでがんばった。
https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb
辛かった。
辛かった点としては、imagemagickのコマンドではうまくいったjpg->pngでの一発変換がうまくいかなかったため、プロセッサーを2段階でかませばいいんだ、というところに思考がなかなかいかず、那由多個くらいあるimagemagickのオプションをいじくりまくっていたことだ。
has_attached_file :avatar, styles: {original: { mask: "200", geometry: "200x200#", format: :png, processors: [:thumbnail, :masker]}, thumb: { mask: "100", geometry: "100x100#", format: :png, processors: [:thumbnail, :masker]}},
module Paperclip class Masker < Processor def initialize file, options = {}, attachment = nil super @format = File.extname(@file.path) @basename = File.basename(@file.path, @format) end def make source = @file destination = Tempfile.new([@basename, @format]) destination.binmode parameters = [ ':source', ':mask', '-alpha', 'set', '-gravity', 'center', '-compose', 'DstIn', '-composite', ':dest' ].join(" ") begin mask_path = File.expand_path("lib/paperclip_processors/alpha_mask_#{options[:mask]}.png") convert(parameters, source: source.path, mask: mask_path, dest: File.expand_path(destination.path)) rescue PaperclipCommandLineError raise PaperclipError, "There was an error during the mask for #{@basename}" if @whiny end destination end end end
- 出版社/メーカー: 角川ゲームス
- 発売日: 2011/03/31
- メディア: Video Game
- 購入: 18人 クリック: 703回
- この商品を含むブログ (181件) を見る