Reputation: 1614
I try to make Captcha via PHP GD. But unfortunately I encounter to a problem! PHP tell me:
The image “http://127.0.0.1/par.php” cannot be displayed because it contains errors.
My code is this
<?php
header ('Content-Type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
$red = imagecolorallocate($im, 255, 0, 0);
for ($i=0;i<=120;$i=$i+1){
for ($j=0;$j<=20;$j=$j+1){
imagesetpixel($im, $i,$j,$red);
}
}
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>
Upvotes: 4
Views: 12299
Reputation: 11
This problem is occurring in FireFox only and not Chrome. You should go ahead if it´s not causing other problems. Shouldn't remove the header() even though error display disapears.
Upvotes: 1
Reputation: 32350
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
You first hide the real error and TRY to display something...
which you can't display because you don't look for it,
and expose the image no matter if it really got generated.
Then you go on stackoverflow and hope someone can guess the error you might have simply suppressed using @
operator.
Make sure there is nothing before <?php
and if you have, remove ?>
at the end.
To make sure you have GD installed try this in a new php file:
<?php
if (extension_loaded('gd') && function_exists('gd_info')) {
echo "PHP GD library is installed on your web server";
}
else {
echo "PHP GD library is NOT installed on your web server";
}
Upvotes: 6
Reputation: 1756
the problem is in those for
change it for:
for ($i=0; $i < 120 ; $i++) {
for ($j=0; $j < 20 ; $j++) {
imagesetpixel($im, $i,$j,$red);
}
}
EDIT
this, is the code that i tested:
header ('Content-Type: image/png');
$im = imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 0, 14, 91);
$red = imagecolorallocate($im, 255, 0, 0);
for ($i=0; $i < 120 ; $i++) {
for ($j=0; $j < 20 ; $j++) {
imagesetpixel ($im ,$i,$j ,$red);
}
}
imagestring($im, 1, 5, 5,'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
result:
Upvotes: 3
Reputation: 906
Most probably your php outputs some warning or notices that mess up with your image data. Try to break your code before imagepng() function call to see if any errors or warnings are generated or try to stop all warnings and notices in your php.ini or with ini_set.
Upvotes: -2