Specifying a font with a font family using ImageMagick

Can I display a font using the font family in ImageMagick?

I tried everything I could and nothing works. I searched many times, and I can not find anything that could solve this problem.

I end up using PHP, but I also tried using ImageMagick on the command line without success. I am using ImageMagick 6.7.6 for linux. Here is what I have done so far.

  • I tested fonts that ImageMagick recognizes using the identify -list font command
  • I manually created a type.xml file based on the xml files that came into my system
  • I created a type.xml file using this script: imagick_type_gen
  • I tested several types of fonts: ttf, otf, pfb, ttc

Here is an example output from identify -list font for some fonts that I tested with:

 Font: DejaVu-Sans-Book family: DejaVu Sans style: Normal stretch: Normal weight: 400 glyphs: /usr/share/fonts/dejavu/DejaVuSans.ttf Font: Baskerville-Regular family: Baskerville style: Normal stretch: Normal weight: 400 glyphs: /usr/share/fonts/custom/Baskerville.ttc Font: Palatino-Roman family: Palatino style: Normal stretch: Normal weight: 400 glyphs: /usr/share/fonts/urw-fonts/p052003l.pfb Font: Honey-Script-Light family: Honey Script style: Normal stretch: Normal weight: 300 glyphs: /usr/share/fonts/custom/HoneyScript-Light.ttf 


If I specify the full font name, everything will work as expected:

 convert -pointsize 32 -font "Honey-Script-Light" label:'Testing' testing.png 

Working


However, when I try to use the font family (both are listed in identify -list font and are listed in type.xml), I get the default system font:

 convert -pointsize 32 -family "Honey Script" label:'Testing' testing.png 

Not working


I also tried to specify all the font options and it does not work again:

 convert -pointsize 32 -stretch Normal -style Normal -weight 300 -family "Honey Script" label:'Testing' testing.png 



When I do a similar process through PHP, it really returns an error as soon as I try to install the font family of the draw object.

 $draw->setFontFamily('Honey Script'); 

This is the error that throws:

 PHP Fatal error: Uncaught exception 'ImagickDrawException' with message 'Unable to set font family; parameter not found in the list of configured fonts 



Like the command line parameter, if I specify the full name of the font, the image will be displayed correctly.

 $draw->setFont('Honey-Script-Light'); 



From all that I have found on how to customize the font that will be used with ImageMagick, the fonts are really customized. Why can't I select fonts based on the font family?

Perhaps this is an undocumented error? Is something missing?

I understand that I can simply specify the full name of the font and make it with it, but there is a reason specific to my project that significantly improves the definition of the font family.

Why can a font family spec be an option if it doesn't work? This is what ImageMagick says about the font family option:

This option offers a font family that ImageMagick should try to use to render text. If a family can be found, it is used; if not, then the default font (for example, "Arial") or a family that is known to be similar (for example, "Courier" can be used if it asks for "System" but cannot be found).

Any ideas are greatly appreciated. I pretty much got stuck at this point.


EDIT:
The main reason I prefer to use font families rather than fully named fonts is that I handle the input. I get files from a third party that need to be processed. These files define fonts as:

 font-family="Honey Script" font-style="Normal" font-stretch="Normal" font-weight="300" 

If I needed to specify the exact font name based on this information, I would need to create a mapping for each possible combination of the font family and attributes with the exact font name. This will be very difficult to maintain as hundreds of fonts are added to the system.

It seems to me that there should be a way to get ImageMagick to get the exact font based on all available font information, as mentioned above.

If this is not possible, then why is ImageMagick able to specify a font family at all?

+8
php fonts imagemagick fontfamily
source share
2 answers

This is due to how font families work; for example, the Arial font family contains specific font definitions for bold, italics, underscores, etc. You must be specific when choosing a font to use in your images.

I assume that if you really want to, you can do the following:

  • Take surname font, replace dashes with spaces
  • If it works, great!
  • If not, add -Regular to the end and try again

But you should not do anything programmatic with the font family unless you are forced to; -)

My source of knowledge about imagination comes mainly from this guy: http://valokuva.org/?cat=1

+2
source share

Here is a method that outputs as an image (to the browser) all available Imagick fonts

  public function test_fonts(){ //load background $im = new Imagick(); $im->newimage(800, 10000, 'lightgray'); $y = 10; $i = 1; foreach($im->queryFonts() as $font){ $insert_image = new Imagick(); $insert_image->newImage(600, 30, 'whitesmoke'); $insert_image->setImageFormat("png"); $draw = new ImagickDraw(); $draw->setFont($font); $draw->setFontSize(25); $draw->setFillColor(new ImagickPixel('black')); $draw->setgravity(imagick::GRAVITY_NORTH); $insert_image->annotateImage($draw, 0, 0, 0, $i . '.' . $font ); $im->compositeImage( $insert_image, $insert_image->getImageCompose(),100, $y); $y += 30; $i++; } $im->setImageFormat('jpg'); header('Content-Type: image/jpg'); echo $im; } 

You should see something like this: enter image description here

+2
source share

All Articles