Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick and imagick_type_gen don't recognize added fonts

Ok, I'm running a CentOS server, PHP 5.4.440 and ImageMagick 6.5.4 and I'm trying to add additional fonts, as an example let's say Lobster. Here's what I've done so far:

Uploaded Lobster.ttf to /home/myusername/fonts/

From SSH, ran "perl imagick_type_gen > fonts.xml". This resulted in a large font file, but contained NO references to either Lobster.otf or Lobster.ttf

Instead, I generated my own type.xml and saved it to /home/myusername/.magick/. Here's the markup:

<?xml version="1.0"?>
<typemap>
    <type format="otf" name="Lobster" fullname="Lobster" family="Lobster" glyphs="/home/myusername/fonts/Lobster.otf" />
</typemap>

Via SSH, I edited /user/lib64/ImageMagick-6.5.4/config/type.xml and inserted the node that references the custom type.xml:

<typemap>
    <include file="type-ghostscript.xml" />
    <include file="/home/myusername/.magick/type.xml" />
</typemap>

Rebooted the server.

At this point, I can list the fonts using this:

$image = new Imagick();
$fonts = $image->queryfonts();
foreach($fonts as $font) {
    echo $font . '<br />';
}

And Lobster shows up in the list. However, converting an SVG to PNG isn't using the Lobster font. I've seen other questions in here similar to mine and they seem to work. What do I need to do to get the system to recognize the added font?

Here's the code I'm using to render what should be an image using Lobster as the font:

//Setup SVG to be read by Imagick.
$SVG = '<?xml version="1.0" encoding="utf-8"?>';
$SVG .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
$SVG .= '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="158px" height="92px" viewBox="0 0 158 92" enable-background="new 0 0 158 92" xml:space="preserve">';
$SVG .= '<text transform="matrix(1 0 0 1 32 58)" font-family="Lobster" font-style="normal" font-size="20px" font-weight="400">Lobster</text>';
$SVG .= '</svg>';

$image = new Imagick();

//Convert SVG to JPG
$image->readImageBlob($SVG);
$image->setImageFormat("jpeg");

//Save the thumbnail.
$save_path = 'lobster-test.jpg';
$image->writeImage($save_path);

echo '<img src="'.$save_path.'">';

I'm at a complete loss here. Help!

like image 248
Pat Friedl Avatar asked May 06 '15 18:05

Pat Friedl


2 Answers

A full solution is discussed here: http://www.multipole.org/discourse-server/viewtopic.php?t=25630

But here's the short version:

  1. Make a directory for your custom uploaded fonts in /usr/share/fonts/default - I created a directory called "custom".
  2. Copy the fonts you want to install to /usr/share/fonts/default/custom
  3. Run the command fc-cache -v /usr/share/fonts

After this, ImageMagick will use the fonts in the SVG. The one Caveat is that you MUST use the font-family declaration embeded in the font. For example, Lobster.otf actually lists "Lobster 1.4".

If you've got the font installed and use the proper font-family name, you'll get your fonts rendered!

like image 150
Pat Friedl Avatar answered Oct 21 '22 22:10

Pat Friedl


You need to install the fonts on your server, that's the only way I managed to resolve this issue.

You can check Eric Green's answer on this.

like image 26
Inc33 Avatar answered Oct 22 '22 00:10

Inc33