Write text to image with php

Posted on 2009-02-12
Last Modified: 2012-05-06
Hellow all,

I have this script thats displays a dynamic text in an image in the browser. This works great, but... When the text is longer than the designated image width the text gets chopped off.

Is there a way to check the text length and split the text into two lines, and also adjust the image height? How can this be done?

Thanks and greetz,


// -----------------------------------------------------------------------------------

// ---- html entities to html tags php 4 ---------------------------------------------

// -----------------------------------------------------------------------------------

function htmlspecialchars_decode_php4 ($str, $quote_style = ENT_COMPAT) {

    return strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));


$theTitle = $_GET['title'];

$im = imagecreatetruecolor(575, 40);

	// Create some colors

$background = imagecolorallocate($im, 31, 31, 31);

$frontColor = imagecolorallocate($im, 255, 205, 3);

$backColor = imagecolorallocate($im, 105, 16, 16);

imagefilledrectangle($im, 0, 0, 575, 40, $background);

// Replace path by your own font path

$font = '../font/Chernobyl-Regular.otf';

// Add the text

imagettftext($im, 22, 0, 3, 27, $backColor, $font, stripslashes(htmlspecialchars_decode_php4(urldecode($theTitle))));

imagettftext($im, 22, 0, 1, 25, $frontColor, $font, stripslashes(htmlspecialchars_decode_php4(urldecode($theTitle))));

imagejpeg($im, '', 100);



Open in new window

Question by:wal_toor
    LVL 2

    Accepted Solution

    Hi Walter,

    You'll need to work out how many characters you can fit within the width you've specified, then split your string up accordingly.

    There are a number of functions posted on that may help you solve this problem:

    Hope this is of some use to you

    LVL 8

    Author Closing Comment

    Thanks, got it working! I also used wordwrap to get it right.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now