Can't display the Captacha strings/number

I’m creating a simple captcha but cant seem to display random numbers on the captcha my output is always like this:
Annotation%202019-11-17%20174804
here is my code:

Blockquote<?php

//start the session
session_start();

header(‘Content-type:image/png’);

//font style for text
//$font = ‘LaBelleAurore.ttf’;

//set the box width and height for the captcha
$image = imagecreatetruecolor(300, 100);

//settomg background color for the box of the captcha white
$background = imagecolorallocate($image, 255, 255, 255);

//some Colors
$white = imagecolorallocate($image, 255, 255, 255);
$yellow = imagecolorallocate($image,255,255,0 );
$black = imagecolorallocate($im, 0,0,0 );

//fill in the captcha box white
imagefilledrectangle($image, 0,0,300,100,$white);

//generate randoms line for the captcha
$line_color = imagecolorallocate($image, 0,0,0);
for ($i=0;$i<=4;$i++){
imageline($image,0,rand(0,100),300,rand(0,100),$line_color);
}

//random dots for more complex captcha
$dots = imagecolorallocate($image, 0,0,255);
for ($i = 0; $i < 2000; $i++){
imagesetpixel($image,rand(0,300),rand(0,100), $dots);
}

//settiing the length of the numbers in the captcha to 4
$length = 4;

//generate random numbers
$text = mt_rand(100,9999);

$_SESSION[“captcha”] = $text;

imagettftext ($image, 18, 0, 35, 30, $black, $text);

imagepng($image);
imagedestroy($image);

?>

any advice ?
thanks in advance

You screwed up your script. The header part will let your browser think that you deliver a binary image file.
To make sure that you do not have any errors:

  • comment the header rule
  • be sure that you have the PHP errors enabled

The script will show some random characters which is normal but you must solve the errors first and then uncomment the header function.

Also you must download a TrueTypeFont. To do that browse to: https://www.fontsquirrel.com/fonts/list/popular and download a font of your choice, (Open Sans in my example). Unpack the zipfile in the same directory of your PHP script or another directory but make sure that you give the imagettftext function an ABSOLUTE FILE PATH!

<?php

session_start();

header('Content-type:image/png');  // COMMENT TO DEBUG !!!

//font style for text
//$font = ‘LaBelleAurore.ttf’;

//set the box width and height for the captcha
$image = imagecreatetruecolor(300, 100);

//settomg background color for the box of the captcha white
$background = imagecolorallocate($image, 255, 255, 255);

//some Colors
$white = imagecolorallocate($image, 255, 255, 255);
$yellow = imagecolorallocate($image,255,255,0 );
$black = imagecolorallocate($image, 0,0,0 );

//fill in the captcha box white
imagefilledrectangle($image, 0,0,300,100,$white);

//generate randoms line for the captcha
$line_color = imagecolorallocate($image, 0,0,0);
for ($i=0;$i<=4;$i++){
    imageline($image,0,rand(0,100),300,rand(0,100),$line_color);
}

//random dots for more complex captcha
$dots = imagecolorallocate($image, 0,0,255);
for ($i = 0; $i < 2000; $i++){
imagesetpixel($image,rand(0,300),rand(0,100), $dots);
}

//settiing the length of the numbers in the captcha to 4
$length = 4;

//generate random numbers
$text = mt_rand(100,9999);

$font = __DIR__ . '\OpenSans-LightItalic.ttf';

$_SESSION['captcha'] = $text;

imagettftext ($image, 48, 0, 75, 70, $black, $font, $text);

imagepng($image);
imagedestroy($image);
Sponsor our Newsletter | Privacy Policy | Terms of Service