How to use custom font

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

How to use custom font

Post by dt59 » 2018-09-25T16:25:17-07:00

Have been trying to use a custom font and after some research i found "creating a type.xml file and using the imagemagick_type_gen script " my question where do i put these the type.xml file in my php project and how do i run the perl script(imagemagick_type_gen), note am using PHP. Please help>>!! :( :( :(

User avatar
fmw42
Posts: 24941
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use custom font

Post by fmw42 » 2018-09-25T17:00:02-07:00

What platform? What version of Imagemagick? See https://imagemagick.org/script/resources.php for a list of locations where you can put it. I am not sure if that works properly with PHP Imagick. The Perl script needs to be run from a terminal window. I do not know much else about using it, especially if on Windows. I think you need to use type-windows.xml if on Windows.

That link says:
Under Unix and Linux, ImageMagick searches for each of the configuration files listed above by looking in the locations given below, in order, and loads them if found:

$MAGICK_CONFIGURE_PATH
$PREFIX/etc/ImageMagick-7
$PREFIX/share/ImageMagick-7
$XDG_CACHE_HOME/ImageMagick
$HOME/.config/ImageMagick
<client path>/etc/ImageMagick
Those are the locations. For example on my Mac, I have it at /usr/local/etc/ImageMagick-6/type.xml

But you can always just use the path to the font file.

What do you mean by custom font? Is it a ttf or otf font? Only certain types of fonts can be used in Imagemagick.

snibgo
Posts: 11600
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use custom font

Post by snibgo » 2018-09-25T17:06:14-07:00

@dt59: Please also say how you want to use the font, eg with "-annotate" or "label:" or "pango:" or an SVG input.
snibgo's IM pages: im.snibgo.com

dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use custom font

Post by dt59 » 2018-09-25T17:11:07-07:00

snibgo wrote:
2018-09-25T17:06:14-07:00
@dt59: Please also say how you want to use the font, eg with "-annotate" or "label:" or "pango:" or an SVG input.
with -annotate and -font .
though i have try to specify the absolute path but is not working and due to the architecture of my application i can not use the relative path. that's why am in search for another alternative.

dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use custom font

Post by dt59 » 2018-09-25T17:15:30-07:00

fmw42 wrote:
2018-09-25T17:00:02-07:00
What platform? What version of Imagemagick? See https://imagemagick.org/script/resources.php for a list of locations where you can put it. I am not sure if that works properly with PHP Imagick. The Perl script needs to be run from a terminal window. I do not know much else about using it, especially if on Windows. I think you need to use type-windows.xml if on Windows.

That link says:
Under Unix and Linux, ImageMagick searches for each of the configuration files listed above by looking in the locations given below, in order, and loads them if found:

$MAGICK_CONFIGURE_PATH
$PREFIX/etc/ImageMagick-7
$PREFIX/share/ImageMagick-7
$XDG_CACHE_HOME/ImageMagick
$HOME/.config/ImageMagick
<client path>/etc/ImageMagick
Those are the locations. For example on my Mac, I have it at /usr/local/etc/ImageMagick-6/type.xml

But you can always just use the path to the font file.

What do you mean by custom font? Is it a ttf or otf font? Only certain types of fonts can be used in Imagemagick.
fred, am not using the php Imagick am actually using the php exec() with imagemagick command.


Version: ImageMagick 7.0.6-3 Q16 x64 2017-07-24 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

User avatar
fmw42
Posts: 24941
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use custom font

Post by fmw42 » 2018-09-25T18:59:41-07:00

What is your command line? Perhaps you have specified it wrong. There should be no reason that you could not use the path to the font. What platform are you on? Is this your server/computer or a hosted server? You are missing fontconfig from your delegates. Try installing that and then run your command again with the full path.

dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use custom font

Post by dt59 » 2018-09-26T01:33:17-07:00

fmw42 wrote:
2018-09-25T18:59:41-07:00
What is your command line? Perhaps you have specified it wrong. There should be no reason that you could not use the path to the font. What platform are you on? Is this your server/computer or a hosted server? You are missing fontconfig from your delegates. Try installing that and then run your command again with the full path.
I think am using the right command line, am actually on a local server(my machine) .
Fred, please how do I correct my fontconfig from my delegates? or how do I install it? to correct the fonconfig problem.

User avatar
fmw42
Posts: 24941
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use custom font

Post by fmw42 » 2018-09-26T09:49:20-07:00

Sorry, but I use a Mac. So I do not know how to install delegates on Linux. I think you must install the delegate, then recompile Imagemagick. But some Linux distros usually come with all the nominal needed delegates installed with Imagemagick.

See
https://www.freedesktop.org/wiki/Software/fontconfig/
https://imagemagick.org/script/download.php
https://imagemagick.org/script/install-source.php#unix
https://imagemagick.org/script/advanced ... lation.php

dt59
Posts: 63
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: How to use custom font

Post by dt59 » 2018-09-26T10:40:55-07:00

fmw42 wrote:
2018-09-26T09:49:20-07:00
Sorry, but I use a Mac. So I do not know how to install delegates on Linux. I think you must install the delegate, then recompile Imagemagick. But some Linux distros usually come with all the nominal needed delegates installed with Imagemagick.

See
https://www.freedesktop.org/wiki/Software/fontconfig/
https://imagemagick.org/script/download.php
https://imagemagick.org/script/install-source.php#unix
https://imagemagick.org/script/advanced ... lation.php

fred i don't still understand. note:am using a window machine for now but i will be hosting the project later.

see my command line below

Code: Select all

magick C:\xampp\htdocs\namewish\storage\app\private\image\main-footer-bg_1537788924.jpg ( ( -size 500\x50 xc:none -fill red -font C:\xampp\htdocs\namewish\storage\app\private\Font\pound.ttf -pointsize 35 -gravity center -annotate +0+0 "Welcome My Friends" ) ( -size 500\x50 xc:none -fill red -font Font/pound.ttf -pointsize 35 -gravity center -annotate +0+0 "Your Friend Name 1 " ) ( -size 500\x50 xc:none -fill red -font C:\xampp\htdocs\namewish\storage\app\private\Font\pound.ttf -pointsize 35 -gravity center -annotate +0+0 "Your Friend Name 1 " ) -alpha set -background none -smush 10 +gravity -alpha set -background none -rotate 0 ) -geometry +50+200 -background none -compose over -composite C:\xampp\htdocs\namewish\storage\app\public\thumbnails\2.png

User avatar
fmw42
Posts: 24941
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use custom font

Post by fmw42 » 2018-09-26T14:12:50-07:00

So does that command work or not? If not, what happens? Can you post a link to your input image and to the font (pound.ttf), so that I can test your command.

I do not know how to install delegates on Windows. But one of the Windows users can probably help.

In one place in your command you use -font Font/pound.ttf rather than the full path to the font that you use in all the other places. Is that what you want?


Note that you do not need to create new transparent images to use -annotate. It will write directly to your input image in the location you specify with +X+Y in the annotate command. You do not need parentheses to do that. You can also put all the text into one -annotate command with newlines and then use -interline-spacing to adjust the spacing. Using negative values moves the lines closer together. Here is a command to do that. Put your font in there and your paths.

Code: Select all

magick path_to\main-footer-bg_1537788924.jpg -fill red -font path_to\pound.ttf -pointsize 35 -gravity northwest -interline-spacing -5  -annotate +50+200 "Welcome My Friends\nYour Friend Name 1 \nYour Friend Name 1 " path_to\2_new.png

snibgo
Posts: 11600
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to use custom font

Post by snibgo » 2018-09-26T16:33:07-07:00

In the font filename path, on Windows, use either forward slashes or double backslashes.

This fails:

Code: Select all

f:\web\im>%IMG7%magick -size 200x200 xc: -font f:\web\im\annifont.ttf -gravity Center -annotate 0 "Hi" x.png
magick: unable to read font `f:webimannifont.ttf' @ warning/annotate.c/RenderType/1009.
These both work:

Code: Select all

f:\web\im>%IMG7%magick -size 200x200 xc: -font f:\\web\\im\\annifont.ttf -gravity Center -annotate 0 "Hi" x.png

f:\web\im>%IMG7%magick -size 200x200 xc: -font f:/web/im/annifont.ttf -gravity Center -annotate 0 "Hi" x.png
IM is happy with single backslashes for image files, so why not for fonts? I don't know. Perhaps a bug.

(Tested with pre-built binary IM v7.0.7-28 on Windows 8.1.)
snibgo's IM pages: im.snibgo.com

Post Reply