[Wolves] GD add text to picture

James Turner james at turnersoft.co.uk
Thu Nov 21 08:42:55 UTC 2013


On 20/11/13 15:26, Chris Ellis wrote:
> On Wed, Nov 20, 2013 at 3:08 PM, Wayne <waynelists at machx.co.uk
> <mailto:waynelists at machx.co.uk>> wrote:
> 
>     Hi,
> 
>     Why isn't this working (well it creates image but no text!)
> 
>     $origImage from elsewhere..... then
> 
> 
>     /* black for the text */
>     $black = imagecolorallocate($origImage, 0, 0, 0);
> 
>     /* put the text on the image */
>     imagettftext($origImage, 72, 0, 0, 0, $black,
>     "/home/j9accomm/public_html/j9photobooth/arial.ttf", "Hello World");
> 
>     /* save the image */
> 
>     imagejpeg($origImage,'uploads/thumbs/'."result.jpg");
> 
> 
> 
>     i've tried all manner of font paths etc in the font bit to no avail.
> 
> 
>     cheers
> 
> 
> Hi Wayne
> 
> From a quick glance at the docs:  http://us2.php.net/imagettftext
> 
> The x and y co-ordinate is the baseline of the text, as such I suspect
> you are drawing the text off the top of the image.
> 
> Try:
> 
> imagettftext($origImage, 72, 0, 0, 72, $black,
> "/home/j9accomm/public_html/j9photobooth/arial.ttf", "Hello World");
> 
> 
> Failing that, try isolating out a test case to provide your code.

I confirm Chris' conclusion. Everything was fine other than the text
being positioned above the top edge of the image.

Runnable/self-contained version of Wayne's code that incorporates Chris'
fix:

<?php

$origImage = imagecreatefromjpeg("original.jpg");

/* black for the text */
$black = imagecolorallocate($origImage, 0, 0, 0);

/* put the text on the image */
imagettftext($origImage, 72, 0, 0, 72, $black,
	"/usr/share/fonts/truetype/msttcorefonts/Arial.ttf",
	"Hello World");

/* save the image */

imagejpeg($origImage,"result.jpg");

?>

Ubuntu will need "php5-gd" package installed in order to use image
functions; font path reflects placement of Arial.ttf from
"msttcorefonts" package. Put the above in a directory alongside
original.jpg, then run code with php -q <filename>.

Regards,

James




More information about the Wolves mailing list