The image "http://localhost/loginCaptcha.php" cannot be displayed because it contains errors

StackOverflow https://stackoverflow.com/questions/21886721

  •  13-10-2022
  •  | 
  •  

Question

I am trying to get a CAPTCHA to work. I have PHP files:

loginCaptcha.php

<?php
session_start();
header('Content-type: image/jpeg');

$text = $_SESSION['secure'];
$font_size = 30;

$image_width = 200;
$image_height = 40;

$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>

starter.php

<?php
session_start();
$_SESSION['secure'] = rand(1000,9999);
?>

<img src="loginCaptcha.php" />

When I run loginCaptcha.php the error comes from the title. When I run starter.php I get what looks like an undefined image. I checked and I have "gd" installed. I am using xampp so it came with it but I double checked anyway.

I read other solutions but they did not really help me.

Thanks.

No correct solution

OTHER TIPS

There is probably something wrong with your PHP code or something is bugging.

You could try removing the content-type header and see what your PHP outputs, there will most likely be a notice, error or warning which prevents your browser from reading a binary request because it contains ascii text

try after installing php gd

# apt-get install php5-gd

or

# sudo apt-get install php5-gd

you must restart the web service after installing php-gd.

OK. the only thing, that comes into my mind is to check absolutely minimal working example. So try to make a file called for examle image.php, and write there:

<?php
    header('Content-Type: image/jpeg');
    $image_width = 200;
    $image_height = 40;
    $image = imagecreate($image_width, $image_height);
    imagejpeg($image);
?>

And check if it is working (localhost/image.php or something like that). The browser should display something that is image. Be 100% sure, that Content-Type (Type) is from capital 'T'. Some browsers had problems with that (PHP Header - Content-type: image/jpeg - Not working for Internet Explorer).

Then please write in comment what happens. If there are any problems, please remove the header line. Then php should "error" something :). Hope it helps... I will update this post due to Your answers, as there is to little that we know, to answer it just like that :).

Best regards.

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