LoginSignup
4
3

More than 5 years have passed since last update.

MiniMagickで日本語が表示されない

Posted at

はじめに

MiniMagicで画像の上に文字を書き出したいが、日本後が出力されない。
以下の例だとtestてすてすと出て欲しいが、testしか表示されない。

def make_picture()
    color = "black"
    draw = "text 10,10 '#{sentense}'"
    font = ".fonts/GenEiGothicN-U-KL.otf"
    base = "app/assets/images/ogotte.png"

    i = MiniMagick::Image.open(base)
    #image.combine_options do |i|
      i.font "Arial.ttf"
      i.fill color
      i.gravity 'center'
      i.draw "text 200,200 'testてすてす'"
      i.pointsize '100'
      i.write "app/assets/images/22.png"  
  end
end

調査

フォントが日本語に対応していないと表示されないという記事が多い。しかし、
今回はi.font "GenEiGothicN-U-KL.otf"で日本語対応のフォントを指定している。

実行しているクエリを確認したが、
-type .fonts/GenEiGothicN-U-KL.otfで フォントはちゃんと指定されているようだった。
絶対パスでも試したがダメだった。

iMagick::Error (`gm mogrify -type .fonts/GenEiGothicN-U-KL.otf /var/folders/j0/d210w9lx3qg27c3321by91840000gn/T/mini_magick20180710-5786-a661kl.png` 

当初セットアップ時に以下のUnable to read fontというエラーが発生して、フォントが読み込みできていなかったので、brew install ghostscript したことを思い出す。

 iniMagick::Error (`gm mogrify -draw text 200,200 'testてすてす' /var/folders/j0/d210w9lx3qg27c3321by91840000gn/T/mini_magick20180710-7111-1tu4c2k.png` failed with error:
   gm mogrify: Unable to read font (/usr/local/share/ghostscript/fonts/n019003l.pfb) [No such file or directory].
   ):

brew uninstall ghostscriptでghostscriptをアンインストールしてみと、
同じエラーが発生する。指定しているフォントではなく、
/usr/local/share/ghostscript/fonts/n019003l.pfbを使用してしまっていた。

解決

乱暴だがghostscriptを再度インストール後、/usr/local/share/ghostscript/fonts/n019003l.pfbをn019003l.pfb_bkにリネーム。
GenEiGothicN-U-KL.otfn019003l.pfbにリネームして、/usr/local/share/ghostscript/fonts/に配置したところ日本語が表示された。

4
3
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
4
3