Question

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.

Was it helpful?

Solution

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.

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