11
12

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.

sinatraとrmagickで画像にテキストを合成する簡単なサンプル

Posted at

やってみた

フォーム送信されたテキストをImageMagickで吹き出し画像と合成するための簡単なサンプルを作りました。
ImageMagickとrmagickのインストールが必要です。

# encoding: utf-8
require 'sinatra'
require 'rmagick'

class App < Sinatra::Base

  get '/' do
    slim :index
  end
    
  post '/' do
    message = params['message']
    image = Magick::ImageList.new('public/image.png')
    draw = Magick::Draw.new
    draw.annotate(image, 0, 0, 50, 100, message) do
      self.font = 'public/logotype.otf'
      self.fill = '#333333'
      self.align = Magick::LeftAlign
      self.stroke = 'transparent'
      self.pointsize = 30
      self.text_antialias = true
      self.kerning = 1
    end
    image.write('public/temp.png')
    slim :index
  end
  
end

結果

skitch.png

11
12
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
11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?