Is it possible to render a font using the font family in ImageMagick?
I have tried everything I can think of and nothing seems to work. I have done a lot of searching and I can't seem to find anything that addresses this specific issue.
I am ultimately using PHP, but I have also tried using ImageMagick on the command line with no success. I am using ImageMagick 6.7.6 on linux. Here is what I have done so far.
identify -list font
commandHere is the example output from identify -list font
for some fonts I have 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 works as expected:
convert -pointsize 32 -font "Honey-Script-Light" label:'Testing' testing.png
However, when I try to use the font family (both listed by identify -list font
and listed in type.xml) I get the default system font:
convert -pointsize 32 -family "Honey Script" label:'Testing' testing.png
I have also tried specifying all of the font's parameters and it again does not work:
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 actually returns an error as soon as I try to set the font family of the draw object.
$draw->setFontFamily('Honey Script');
This is the error that gets thrown:
PHP Fatal error: Uncaught exception 'ImagickDrawException' with message 'Unable to set font family; parameter not found in the list of configured fonts
Just like the command line option, if I specify the full font name, the image is rendered properly.
$draw->setFont('Honey-Script-Light');
From everything I have found about how to configure a font to be used with ImageMagick, the fonts are indeed configured. Why can't I select the fonts based on the font family?
Is this possibly an undocumented bug? Is there something I am missing?
I realize that I could simply specify the full font name and be done with it, but there is a reason specific to my project that make it much better to specify the font family.
Why would specifying the font family be an option if it does not work? This is what ImageMagick says regarding the font family option:
This setting suggests a font family that ImageMagick should try to use for rendering text. If the family can be found it is used; if not, a default font (e.g., "Arial") or a family known to be similar is substituted (e.g., "Courier" might be used if "System" is requested but not found).
Any ideas are very welcome. I am pretty much stuck at this point.
EDIT:
The main reason I would prefer to use font families rather than fully named fonts is because of the input I am processing. I receive files from a third party that need to be processed. These files specify fonts as:
font-family="Honey Script" font-style="Normal" font-stretch="Normal" font-weight="300"
If I had to specify the exact font name based on that information I would need to create a mapping for every possible combination of font family and attributes to the exact font name. That would be very difficult to maintain as hundreds of fonts are added to the system.
It seems to me that there must be a way to get ImageMagick to retrieve the exact font based on all the available details about the font as listed above.
If that is not possible then why would ImageMagick have the option to specify font family at all?
Here is method that outputs as image(into 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:
This is due to how font families work; for instance, the "Arial" font family comprises specific font definitions for bold, italic, underlines, etc. You must be specific when choosing a font to use in your images.
I'm guessing that if you really wanted to, you could do the following:
-Regular
to the end and try againBut you shouldn't do anything programmatic with font family unless you're forced to ;-)
My source of imagick knowledge mostly comes from this guy: http://valokuva.org/?cat=1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With