在 Heroku 上的 ImageMagick 中使用自定义字体

Dan*_*ell 5 fonts ruby-on-rails rmagick imagemagick heroku

我正在将一些功能构建到需要 ImageMagick 的 Rails 应用程序中。我目前正在使用 RMagick gem 在图像上覆盖自定义文本。我希望能够为用户提供一系列可供选择的字体,但是我发现 GhostScript 附带的一组库存字体(我的系统上有这些字体)相当有限。

我想知道的是如何导入/加载自定义字体(例如 Lato)以在本地和 Heroku 上工作的方式与 ImageMagick 一起使用。具体来说,我正在构建一个图像标题(请参阅下面的代码)并希望能够指定不在列表中的字体。

这是我的示例代码:

font = 'Lato'
text = Magick::Image.read("caption:#{caption_text.to_s}") {
    self.size = "#800x600"
    self.colorspace = RGBColorspace
    self.background_color = "Transparent"
    self.depth = 8
    self.format = 'PNG'
    self.fill = '#333333'
    self.font = font
}.first
Run Code Online (Sandbox Code Playgroud)

如果我运行,identify -list font我会得到 ImageMagick 在我的系统上可以访问的以下字体列表。

?  Desktop identify -list font

Path: /usr/local/Cellar/imagemagick/6.9.3-0_2/etc/ImageMagick-6/type-ghostscript.xml
  Font: AvantGarde-Book
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/a010013l.pfb
  Font: AvantGarde-BookOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/a010033l.pfb
  Font: AvantGarde-Demi
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/a010015l.pfb
  Font: AvantGarde-DemiOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/a010035l.pfb
  Font: Bookman-Demi
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/b018015l.pfb
  Font: Bookman-DemiItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 600
    glyphs: /usr/local/share/ghostscript/fonts/b018035l.pfb
  Font: Bookman-Light
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 300
    glyphs: /usr/local/share/ghostscript/fonts/b018012l.pfb
  Font: Bookman-LightItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 300
    glyphs: /usr/local/share/ghostscript/fonts/b018032l.pfb
  Font: Courier
    family: Courier
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n022003l.pfb
  Font: Courier-Bold
    family: Courier
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n022004l.pfb
  Font: Courier-BoldOblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n022024l.pfb
  Font: Courier-Oblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n022023l.pfb
  Font: fixed
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019003l.pfb
  Font: Helvetica
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019003l.pfb
  Font: Helvetica-Bold
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019004l.pfb
  Font: Helvetica-BoldOblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019024l.pfb
  Font: Helvetica-Narrow
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019043l.pfb
  Font: Helvetica-Narrow-Bold
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019044l.pfb
  Font: Helvetica-Narrow-BoldOblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n019064l.pfb
  Font: Helvetica-Narrow-Oblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019063l.pfb
  Font: Helvetica-Oblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n019023l.pfb
  Font: NewCenturySchlbk-Bold
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/c059016l.pfb
  Font: NewCenturySchlbk-BoldItalic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/c059036l.pfb
  Font: NewCenturySchlbk-Italic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/c059033l.pfb
  Font: NewCenturySchlbk-Roman
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/c059013l.pfb
  Font: Palatino-Bold
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/p052004l.pfb
  Font: Palatino-BoldItalic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/p052024l.pfb
  Font: Palatino-Italic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/p052023l.pfb
  Font: Palatino-Roman
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/p052003l.pfb
  Font: Symbol
    family: Symbol
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/s050000l.pfb
  Font: Times-Bold
    family: Times
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n021004l.pfb
  Font: Times-BoldItalic
    family: Times
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/local/share/ghostscript/fonts/n021024l.pfb
  Font: Times-Italic
    family: Times
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n021023l.pfb
  Font: Times-Roman
    family: Times
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/local/share/ghostscript/fonts/n021003l.pfb
Run Code Online (Sandbox Code Playgroud)

如果我在 Heroku 上运行 bash,我会得到以下列表:

?  Desktop heroku run bash
Running bash on staging-myapp... up, run.5220
~ $ identify -list font

Path: /etc/ImageMagick/type-ghostscript.xml
  Font: AvantGarde-Book
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/a010013l.pfb
  Font: AvantGarde-BookOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/a010033l.pfb
  Font: AvantGarde-Demi
    family: AvantGarde
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/a010015l.pfb
  Font: AvantGarde-DemiOblique
    family: AvantGarde
    style: Oblique
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/a010035l.pfb
  Font: Bookman-Demi
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/b018015l.pfb
  Font: Bookman-DemiItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 600
    glyphs: /usr/share/fonts/type1/gsfonts/b018035l.pfb
  Font: Bookman-Light
    family: Bookman
    style: Normal
    stretch: Normal
    weight: 300
    glyphs: /usr/share/fonts/type1/gsfonts/b018012l.pfb
  Font: Bookman-LightItalic
    family: Bookman
    style: Italic
    stretch: Normal
    weight: 300
    glyphs: /usr/share/fonts/type1/gsfonts/b018032l.pfb
  Font: Courier
    family: Courier
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022003l.pfb
  Font: Courier-Bold
    family: Courier
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022004l.pfb
  Font: Courier-BoldOblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022024l.pfb
  Font: Courier-Oblique
    family: Courier
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022023l.pfb
  Font: fixed
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019003l.pfb
  Font: Helvetica
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019003l.pfb
  Font: Helvetica-Bold
    family: Helvetica
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019004l.pfb
  Font: Helvetica-BoldOblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019024l.pfb
  Font: Helvetica-Narrow
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019043l.pfb
  Font: Helvetica-Narrow-Bold
    family: Helvetica Narrow
    style: Normal
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019044l.pfb
  Font: Helvetica-Narrow-BoldOblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019064l.pfb
  Font: Helvetica-Narrow-Oblique
    family: Helvetica Narrow
    style: Oblique
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019063l.pfb
  Font: Helvetica-Oblique
    family: Helvetica
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019023l.pfb
  Font: NewCenturySchlbk-Bold
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059016l.pfb
  Font: NewCenturySchlbk-BoldItalic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059036l.pfb
  Font: NewCenturySchlbk-Italic
    family: NewCenturySchlbk
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/c059033l.pfb
  Font: NewCenturySchlbk-Roman
    family: NewCenturySchlbk
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/c059013l.pfb
  Font: Palatino-Bold
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/p052004l.pfb
  Font: Palatino-BoldItalic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/p052024l.pfb
  Font: Palatino-Italic
    family: Palatino
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/p052023l.pfb
  Font: Palatino-Roman
    family: Palatino
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/p052003l.pfb
  Font: Symbol
    family: Symbol
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/s050000l.pfb
  Font: Times-Bold
    family: Times
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021004l.pfb
  Font: Times-BoldItalic
    family: Times
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021024l.pfb
  Font: Times-Italic
    family: Times
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021023l.pfb
  Font: Times-Roman
    family: Times
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021003l.pfb

Path: System Fonts
  Font: Century-Schoolbook-Bold
    family: Century Schoolbook
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059016l.pfb
  Font: Century-Schoolbook-Bold-Italic
    family: Century Schoolbook
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/c059036l.pfb
  Font: Century-Schoolbook-Italic
    family: Century Schoolbook
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/c059033l.pfb
  Font: Century-Schoolbook-Roman
    family: Century Schoolbook
    style: Normal
    stretch: Normal
    weight: 500
    glyphs: /usr/share/fonts/type1/gsfonts/c059013l.pfb
  Font: DejaVu-Sans-Bold
    family: DejaVu Sans
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
  Font: DejaVu-Sans-Bold-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf
  Font: DejaVu-Sans-Book
    family: DejaVu Sans
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
  Font: DejaVu-Sans-Condensed
    family: DejaVu Sans
    style: Normal
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed.ttf
  Font: DejaVu-Sans-Condensed-Bold
    family: DejaVu Sans
    style: Normal
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Bold.ttf
  Font: DejaVu-Sans-Condensed-Bold-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-BoldOblique.ttf
  Font: DejaVu-Sans-Condensed-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Oblique.ttf
  Font: DejaVu-Sans-ExtraLight
    family: DejaVu Sans
    style: Normal
    stretch: Normal
    weight: 200
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-ExtraLight.ttf
  Font: DejaVu-Sans-Mono-Bold
    family: DejaVu Sans Mono
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf
  Font: DejaVu-Sans-Mono-Bold-Oblique
    family: DejaVu Sans Mono
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono-BoldOblique.ttf
  Font: DejaVu-Sans-Mono-Book
    family: DejaVu Sans Mono
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf
  Font: DejaVu-Sans-Mono-Oblique
    family: DejaVu Sans Mono
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSansMono-Oblique.ttf
  Font: DejaVu-Sans-Oblique
    family: DejaVu Sans
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf
  Font: DejaVu-Serif-Bold
    family: DejaVu Serif
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf
  Font: DejaVu-Serif-Bold-Italic
    family: DejaVu Serif
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif-BoldItalic.ttf
  Font: DejaVu-Serif-Book
    family: DejaVu Serif
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf
  Font: DejaVu-Serif-Condensed
    family: DejaVu Serif
    style: Normal
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed.ttf
  Font: DejaVu-Serif-Condensed-Bold
    family: DejaVu Serif
    style: Normal
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-Bold.ttf
  Font: DejaVu-Serif-Condensed-Bold-Italic
    family: DejaVu Serif
    style: Italic
    stretch: SemiCondensed
    weight: 700
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-BoldItalic.ttf
  Font: DejaVu-Serif-Condensed-Italic
    family: DejaVu Serif
    style: Italic
    stretch: SemiCondensed
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerifCondensed-Italic.ttf
  Font: DejaVu-Serif-Italic
    family: DejaVu Serif
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/truetype/dejavu/DejaVuSerif-Italic.ttf
  Font: Dingbats-Regular
    family: Dingbats
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/d050000l.pfb
  Font: Nimbus-Mono-Bold
    family: Nimbus Mono
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022004l.pfb
  Font: Nimbus-Mono-Bold-Oblique
    family: Nimbus Mono
    style: Oblique
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n022024l.pfb
  Font: Nimbus-Mono-Regular
    family: Nimbus Mono
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022003l.pfb
  Font: Nimbus-Mono-Regular-Oblique
    family: Nimbus Mono
    style: Oblique
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n022023l.pfb
  Font: Nimbus-Roman-No9-Medium
    family: Nimbus Roman No9
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021004l.pfb
  Font: Nimbus-Roman-No9-Medium-Italic
    family: Nimbus Roman No9
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n021024l.pfb
  Font: Nimbus-Roman-No9-Regular
    family: Nimbus Roman No9
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021003l.pfb
  Font: Nimbus-Roman-No9-Regular-Italic
    family: Nimbus Roman No9
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n021023l.pfb
  Font: Nimbus-Sans-Bold
    family: Nimbus Sans
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019004l.pfb
  Font: Nimbus-Sans-Bold-Condensed
    family: Nimbus Sans
    style: Normal
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019044l.pfb
  Font: Nimbus-Sans-Bold-Condensed-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Condensed
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019064l.pfb
  Font: Nimbus-Sans-Bold-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/n019024l.pfb
  Font: Nimbus-Sans-Regular
    family: Nimbus Sans
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019003l.pfb
  Font: Nimbus-Sans-Regular-Condensed
    family: Nimbus Sans
    style: Normal
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019043l.pfb
  Font: Nimbus-Sans-Regular-Condensed-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Condensed
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019063l.pfb
  Font: Nimbus-Sans-Regular-Italic
    family: Nimbus Sans
    style: Italic
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/n019023l.pfb
  Font: Standard-Symbols-Regular
    family: Standard Symbols
    style: Normal
    stretch: Normal
    weight: 400
    glyphs: /usr/share/fonts/type1/gsfonts/s050000l.pfb
  Font: URW-Bookman-Demi-Bold
    family: URW Bookman
    style: Normal
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/b018015l.pfb
  Font: URW-Bookman-Demi-Bold-Italic
    family: URW Bookman
    style: Italic
    stretch: Normal
    weight: 700
    glyphs: /usr/share/fonts/type1/gsfonts/b018035l.pfb
  Font: URW-Bookman-Light
    family: URW Bookman
    style: Normal
    stretch: Normal

Dan*_*ell 5

我想到了!做起来非常容易,如果不是太明显而无法在文档中提及的话。如果有更好的方法来做到这一点,请分享,我会接受你的答案。

当创建一个新的caption(这可能也适用于其他类型的文本)时,ArialBold如果它在你的/etc/ImageMagick/type-ghostscript.xml. 如果它没有在那里列出,您也可以将绝对文件路径作为字符串传递。确保使用双引号。ImageMagick 将接受 TTF 和 OTF 字体(可能是其他字体,但我还没有测试过)。

因此,如果我运行以下代码,它会选择Arial Bold Italic字体,即使它不在我的字体映射中。

require 'RMagick'
include Magick

caption_text = "This is a test"
font = "/Library/Fonts/Arial\ Bold\ Italic.ttf"
text = Magick::Image.read("caption:#{caption_text}") {
    # this wraps the text to fixed width
    self.size = "800x600"
    self.colorspace = RGBColorspace
    self.background_color = "Transparent"
    self.depth = 8
    self.format = 'PNG'
    self.fill = '#333333'
    self.font = font
}.first
text.write('text.png')
Run Code Online (Sandbox Code Playgroud)

这也应该是 Heroku 安全的,因为我可以轻松地在我的资产/字体文件夹中包含自定义字体。

  • 实际上是用 Heroku 搞定的 - 只需在项目的根目录中有一个 `.fonts` 文件夹,然后将字体转储到那里,并将它们直接引用到 Image 对象上,例如 `Magick::Image.read('text') { self.font = "SF-Pro-Text-Light"}` (3认同)