Draw text not working

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
evolveimg

Draw text not working

Post by evolveimg »

Hi I'm new to Imagemagick and I'm struggling to add text to images. I've been messing with the following php code with no success:

echo exec("/usr/bin/convert xc:yellow -resize 200x15! -gravity \"Center\" -draw \"text 0,0 'Test Text'\" $blank_image");

The image defined in $black_image becomes a 200x15 yellow rectangle with no text. I get no errors. I have no idea what I'm doing wrong. Please help. Thanks.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Draw text not working

Post by Bonzo »

Code: Select all

echo exec("/usr/bin/convert xc:yellow -resize 200x15! -gravity \"Center\" -draw \"text 0,0 'Test Text'\" $blank_image");
You do not need the echo

You MAY be able to use convert and not /usr/bin/convert

You are not resizing so you need -size not -resize

Center does not need the \"

You could add the font details e.g. font size and colour

Try:

Code: Select all

exec("/usr/bin/convert -size 200x15 xc:yellow -gravity Center -draw \"text 0,0 'Test Text'\" $blank_image");
evolveimg

Re: Draw text not working

Post by evolveimg »

Still only a yellow rectangle with that code and this:

exec("/usr/bin/convert -size 200x15 xc:yellow -gravity Center -draw \"text 0,0 'Test Text'\" -fill black -pointsize 14 -font Times-Roman $blank_image");
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Draw text not working

Post by Bonzo »

What version are you using ?

Code: Select all

<?php
echo "<pre>";
system("convert -version");  
echo "</pre>";
?> 
Try adding the error output and see what it comes up with.

Code: Select all

<?php
$array=array();
echo "<pre>";
exec("/usr/bin/convert -size 200x15 xc:yellow -gravity Center -draw \"text 0,0 'Test Text'\" $blank_image 2>&1", $array); 
echo "<br>".print_r($array)."<br>";
echo "</pre>"; 
?> 
I tend to use -annotate rather than -draw these days

Code: Select all

<?php
$array=array();
echo "<pre>";
exec("/usr/bin/convert -size 200x15 xc:yellow -gravity Center -fill black -annotate +0+0 \" Test Text \" $blank_image 2>&1", $array); 
echo "<br>".print_r($array)."<br>";
echo "</pre>"; 
?> 
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Draw text not working

Post by anthony »

evolveimg wrote:exec("/usr/bin/convert -size 200x15 xc:yellow -gravity Center -draw \"text 0,0 'Test Text'\" -fill black -pointsize 14 -font Times-Roman $blank_image");
NOTE you will need to set -fill, -pointsize and -font BEFORE the operation that is using in (the -draw).

however instead creating an empty yellow box and drawing into it you could -annotate into it whcih does not have the quotes within quotes problem.

Also why create a empty yellow box! create a label of that -size! If pointsize is NOT set IM will attempt to find a point size that best fits an image of that size!

Code: Select all

exec("/usr/bin/convert -size 200x15 -background yellow -gravity Center -font Times-Roman label:'Test Text' $blank_image");
See IM examples, Text Handling
http://www.imagemagick.org/Usage/text/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
evolveimg

Re: Draw text not working

Post by evolveimg »

Ah thank you. I'm getting this error:

Array
(
[0] => convert: unable to read font `/usr/share/fonts/default/Type1/n021003l.pfb'.
)

1

What do I do?
evolveimg

Re: Draw text not working

Post by evolveimg »

Also this is my version:
Version: ImageMagick 6.0.7 04/20/08 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2004 ImageMagick Studio LLC
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Draw text not working

Post by Bonzo »

Your version of Imagemagick is probably about 4 years old but I do not know if that is a problem.

I would simplify things and upload a ttf font into the same folder as the code and try that, change this to the actual font name font.ttf

Code: Select all

<?php
$array=array();
echo "<pre>";
exec("/usr/bin/convert -size 200x15 xc:yellow -gravity Center -fill black -pointsize 16 -font font.ttf -annotate +0+0 \" Test Text \" output.jpg 2>&1", $array); 
echo "<br>".print_r($array)."<br>";
echo "</pre>"; 
echo "<img src=\"output.jpg\">";
?> 
Last edited by Bonzo on 2009-05-07T10:15:10-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Draw text not working

Post by fmw42 »

Too old!!!!! (450 versions behind) You really need to upgrade.
Post Reply