リア充爆発日記

You don't even know what ria-ju really is.

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

エビコレ+ アマガミ(通常版)

エビコレ+ アマガミ(通常版)