Question

I'm useing Joomla 1.5. I have the script for create image with text over It, but It doesn't work for me:

<?php
// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = "ARIAL.TFF";

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

I don't understand why this doesn't work. I found many examples in google, I used It, but always the same. error This error in english means: Image "http://juokoera.lt/a.php" can't be shown, because It have problems (errors).

I found in google, that can be fault by my hosting, I changed It, but the same problem. Help me, please if you can. Thank you very much.

UPDATED: I got the same error when, code looks like:

dasfasdf
dfas

<?php 
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Testing...';
$font = "ARIAL.TTF";
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im); ?>

How can I use additional text in the same php?

Was it helpful?

Solution

I think your problem is here:

$font = "ARIAL.TFF";

in fact, on your server a file named "ARIAL.TFF" doesn't exist, the font extension you are looking for is TTF, not TFF, and in fact "ARIAL.TTF" on your server exists and i just downloaded it, correct that line to:

$font = "ARIAL.TTF";

After that, you should be able to write text on your image

Hope this helps

UPDATE

After question update, i noticed that an header is sent after printing out some text.

Nothing has to be printed out before the header() function, in order for headers to be correctly sent.

In the PHP manual>header() it is the first thing that is explained:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top