Reputation: 12867
I have created a simple captcha generator, but won't show captcha-image :
index.php :
<?php
session_start();
$_SESSION['captcha'] = rand(1000, 9999);
?>
<img src="generator.inc.php" />
generator.inc.php :
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['captcha'];
$font_size = 30;
$image_width = 160;
$image_height = 50;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 200, 200, 200);
$font_color = imagecolorallocate($image, 20 , 20 , 20);
imagettftext($image, $font_size, 0, 15, 30, $font_color, 'BYekan.ttf', $text);
imagejpeg($image);
?>
Please tell me what am i doing wrong?
P.S :
I'm using Lamp server on Linux.
Upvotes: 0
Views: 2090
Reputation: 12867
Probably GD library is not installed.
Also check in php.ini
and make sure GD line be uncommented.
Upvotes: 1
Reputation: 8838
My suggestion would be to try 2 things:
I have read and seen before that people suggest to place the content type header('Content-type: image/jpeg');
just before the imagejpeg($image);
. To back this up here is an article profing it: http://php.net/manual/en/function.imagejpeg.php
My other suggestion would be to base64_encode the file. If header('Content-type: image/jpeg');
produces an error suggestion "header() must be called before any actual output is sent" then base64_encode allows one to create an image with specifying the content type. Remove the header code and then add the following code when imagejpeg($image);
is called.
ob_start();
imagejpeg($image); //your code
$img = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpeg;base64,'.base64_encode($img).'" />';
Upvotes: 0
Reputation: 8415
I remove the header
function call in your script and found an error that you have not defined the $image_height
variable. In your script, you duplicate the $image_width
assignment.
Upvotes: 0