Imagemagick - Same命令在PHP或控制台中提供不同的输出

I'm trying to use ImageMagick on an Ubuntu 12.04 server to create simple business cards dynamically. This is done via a php page. I tried to install MagickWand, but it didn't really work so I'm now using ImageMagick syntax and executing it via PHP function exec().

I'm making good progress, and had the basics more or less done, the output image looked okay and am now finetuning, setting the positions right, changing fonts and sizes, etc.

Our graphic person wants the text to be in Arial with bold titles, so I installed msttcorefonts on the server. Since then, the text in the output image is italic, and it ignores whatever font I use.

The curious thing is: if I echo the command to the page, copy and paste it into the ubuntu console, the image that's created there uses the right fonts and is exactly what I want.

So, it seems that when I execute the command via console, everything is fine, but when I execute it in PHP via exec(), ImageMagick doesn't seem to recognize the fonts. Does anyone know why? Also, if it doesn't recognize the font, why does it default to italic, or, for that matter, why did the default change at all after installing the fonts?

This is the command I'm currently testing (with changed names and contact info):

 convert                                                     \
    -size 1051x697                                           \
     xc:none                                                 \
    -density 300                                             \
    -fill white                                              \
     draw "rectangle 0,0 1051,697"                           \
    -fill black                                              \
    -font Arial-Negreta                                      \
    -pointsize 9                                             \
    -draw "text 140,175 'Hotel Example'"                     \
    -font Arial-Normal                                       \
    -pointsize 6                                             \
    -draw "text 140,220 'Example Street 8'"                  \
    -draw "text 140,240 '12345 Exampletown'"                 \
    -draw "text 140,280 'Telefon: 123 45 67 89 0'"           \
    -draw "text 140,300 'Fax: 234 56 78 90'"                 \
    -draw "text 140,340 'http://www.example.de'"             \
    -draw "text 140,360 'hotel-example@example.de'"          \
    -stroke none -fill orange -draw "rectangle 1,32 1050,75" \
    -fill grey                                               \
    -draw "rectangle 1,75 1050,100"                          \
    -fill orange                                             \
    -draw "rectangle 1,626 1050,665"                         \
     test_front.png

Edit:

I just noticed that running the command from the Ubunut console also only works in my home directory, and not in my /var/www directory. As APiK pointed out, it's probably a user thing.

It looks like you installed the fonts only for a specific user. Try to run this command in console and PHP script:

# This will show you all the available fonts for ImageMagick
$ convert -list type

Or you can also specify TTF fonts directly:

$ convert -font /path/to/your/fonts.ttf

I doubt your font names Arial-Fett and Arial-Standard are recognized by ImageMagick.

To see which names for Arial font faces are known to convert, run this command first:

convert -list font | grep -i arial

On my system (a Mac OS Lion), I get this result:

  [....]
  Font: Arial-Narrow-Negreta-cursiva
    family: Arial Narrow
    glyphs: /Library/Fonts/Arial Narrow Bold Italic.ttf
  Font: Arial-Narrow-Normal
    family: Arial Narrow
    glyphs: /Library/Fonts/Arial Narrow.ttf
  Font: Arial-Negreta
    family: Arial
    glyphs: /Library/Fonts/Arial Bold.ttf
  Font: Arial-Negreta-cursiva
    family: Arial
    glyphs: /Library/Fonts/Arial Bold Italic.ttf
  Font: Arial-Normal
    family: Arial
    glyphs: /Library/Fonts/Arial.ttf
  [....]

With ImageMagick, you can only apply these two methods to specify the font to use:

  • ...either use exactly the names that appear after the Font: tag,

  • ...or use exactly the path+name to the specific font file that appears after the glyphs: tag (if filename or path use spaces, enclose it in quotes!).

So on my system, for your -font Arial-Standard I'd have to use either of

-font Arial-Normal
-font /Library/Fonts/Arial.ttf

Instead of your -font Arial-Fett I'd have to use either of

-font Arial-Negreta
-font "/Library/Fonts/Arial Bold.ttf"

On your system this may be different of course (esp. if you're on Windows or Linux). That's why you have to verify using convert -list font...