0

I'm trying to create a dynamic image in php with some text in it but it says the image cannot be displayed because it contains errors. Here's my code

    <html>
<head>
    </head>
    <body>
<?php
$width = $_POST["width"];
$height = $_POST["height"];
$text = $_POST["text"];
$border = $_POST["border"];
$cornerangle = $_POST["angle"];

putenv('GDFONTPATH=' . realpath('.'));
header ('Content-Type: image/png');
$img = imagecreatetruecolor($width, $height);
$font = 'verdana';
$font_size = 20;
$angle = 45;
$text_box = imagettfbbox($font_size,$angle,$font,$text);
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[3]-$text_box[1];
//coord text
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
$textcolor = imagecolorallocate($img,255,255,255);
imagettftext($img, $font_size, 0, $x, $y, $textcolor, $font, $text);
imagepng($img);
imagedestroy($img);
?>
</body>
</html>

I have the font file (verdana.ttf) in the same folder as my php file. I tried $font = 'verdana.ttf'; and got the same error.

3
  • You should post your error messages. Commented Mar 19, 2014 at 14:09
  • try $font='./verdana.ttf' Commented Mar 19, 2014 at 14:13
  • The error is "The image 'localhost://test.php' cannot be displayed because it contains errors". I should mention that I am using WAMP but I do have GD and FreeType support in phpinfo(). Using imagestring instead of imagettftext works so it must be something with imagettftext. Commented Mar 19, 2014 at 14:17

1 Answer 1

1

You need to have the font in your web server and call it.

$font = 'path/verdana.ttf';

i tested your code, and work fine in my server.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it was a problem with my wamp. I tested it on another server and it worked fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.