I have the following code validation php script:
if(empty($_POST['captcha_code'])) {
$error = 1;
$code[3] = 'color:#FF0000;';
} else {
include_once "formfiles/captcha.php";
$randomnr = new Securimage();
$valid = $randomnr->check($_POST['captcha_code']);
if(!$valid) {
$error = 1;
$code[3] = 'color:#FF0000;';
$code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
}
}
and this captcha php code:
<?php
Securimage();
exit();
function Securimage()
{
$randomnr = rand(1000, 9999);
$_SESSION['randomnr2'] = md5($randomnr);
$im = imagecreatetruecolor(100, 38);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 150, 150, 150);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 200, 35, $black);
//path to font - this is just an example you can use any font you like:
$font = dirName(__FILE__).'/calvin.ttf';
imagettftext($im, 20, 4, 22, 30, $grey, $font, $randomnr);
imagettftext($im, 20, 4, 15, 32, $white, $font, $randomnr);
imagegif($im);
imagedestroy($im);
}
?>
After submitting my form I always get an awkward bit of code starting with gif. Where is my fault? Can somebody please help me?
You need to decalare header about the content type to browser recognize it as image. Try this
header('Content-Type: image/gif');
Securimage();
exit();
Related
I'm trying to create a PNG with some text and a scaled picture in it. Here is the code for just the text, it works fine:
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);
// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");
// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "assets/fonts/arial.ttf", "DOWN DOWN DOWN");
header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>
With the code above you get the following picture, which is correct:
Now I'm trying to have a small picture in it, So I'm loading the picture from a JPEG file (adidas.jpg). Here's the code
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);
// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");
// image
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white = imagecolorallocate($pic, 255, 255, 255);
imagefill($label,0,0,$white);
imagedestroy($pic);
// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "arial.ttf", "DOWN DOWN DOWN");
header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>
And this is what I get:
To my surprise the "down" text disappeared. Why is that? The text added before the picture is fine, the text added after it turns to black for some reason
Your code is bit messy, "DOWN.." text will appear if you remove second:
$color = imagecolorallocate($label, 255, 255, 255);
You dont fill the original image, you try it later but with wrong color ($white is from $pic, not $label).
I cleaned it up:
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
$black = imagecolorallocate($label, 0, 0, 0);
$white = imagecolorallocate($label, 255, 255, 255);
imagefill($label, 0, 0, $black);
imagettftext($label, 50, 0, 0, 150, $white, "arial.ttf", "UP UP UP");
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white2 = imagecolorallocate($pic, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $white, "arial.ttf", "DOWN DOWN DOWN");
ob_end_clean();
header('Content-type: image/png');
imagepng($label);
imagedestroy($src);
imagedestroy($pic);
imagedestroy($label);
die();
?>
I'm making my own 'captcha' form and I now have a page that generates the image:
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 160, 160, 160);
imagefilledrectangle($im, 0, 0, 200, 50, $white);
$captcha = "SOMErandomTEXT";
$font = 'Chewy.ttf';
imagettftext($im, 20, 0, 0, 20, $gray, $font, $captcha);
imagepng($im);
imagedestroy($im);
?>
Now I also have another page that shows this image, inside the form. Now I want to get the value $captcha from the page shown above on the other page. How can I do it?
Using sessions did the job for me.
PHP Sessions
i m getting this error on mozilla
"The image
“*/form%20and%20its%20validation/New%20folder/captcha/captcha.php”
cannot be displayed because it contains errors."
fonts.ttf is in the same folder.
and this code showing nothing on chrome. what is wrong in this code?
i want to include this in my form page ..
$rnum='';
//generating random number
$rnum = rand(100000,9999999);
//creating image with size 300*60
$imgo = imagecreatetruecolor(300,60);
$white = imagecolorallocate($imgo, 255, 255, 255);
$grey = imagecolorallocate($imgo, 128, 128, 128);
$red = imagecolorallocate($imgo, 200, 100,90);
$black = imagecolorallocate($imgo, 0, 0, 0);
imagefilledrectangle($imgo, 0, 0, 200, 35, $black);
//getting font
$cfont= 'font.ttf';
imagettftext($imgo, 35, 0, 22, 24, $red, $cfont, $rnum);
header ("Content-type: image/png");
imagepng($imgo);
imagedestroy($imgo);
This is 99% because an error in the script which get's printed to the browser, too.
You can capture the error and print it instead of the corrupt image:
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
ini_set('xmlrpc_errors', true);
ob_start();
$rnum='';
//generating random number
$rnum = rand(100000,9999999);
//creating image with size 300*60
$imgo = imagecreatetruecolor(300,60);
$white = imagecolorallocate($imgo, 255, 255, 255);
$grey = imagecolorallocate($imgo, 128, 128, 128);
$red = imagecolorallocate($imgo, 200, 100,90);
$black = imagecolorallocate($imgo, 0, 0, 0);
imagefilledrectangle($imgo, 0, 0, 200, 35, $black);
//getting font
$cfont= 'font.ttf';
imagettftext($imgo, 35, 0, 22, 24, $red, $cfont, $rnum);
$errors = ob_get_clean();
ob_end();
if ($errors) {
echo $errors;
} else {
header ("Content-type: image/png");
imagepng($imgo);
}
imagedestroy($imgo);
If it doesn't work or help, you can simply remove header ("Content-type: image/png"); from your script and then try again.
I'm trying to use the GD graphics library to make this captcha image, and the box shows up how Id like it to, but the text isn't being displayed over the image at all. I can't figure out why it's not showing up.
<?php
session_start();
putenv('GDFONTPATH=' . realpath('.') );
$font = 'Molle';
header('Content-Type: image/png');
$im = imagecreatetruecolor(260, 40);
$white = imagecolorallocate($im, 255, 255,255);
$grey = imagecolorallocate($im, 215, 215, 215);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 3, 3, 255, 34, $grey);
$text = 'Testing';
$_SESSION["captcha"] = $text;
imagettftext( $im, 20, 0, 16, 26, $white, $font, $text );
imagettftext( $im, 20, 0, 15, 25, $black, $font, $text );
imagepng($im);
imagedestroy($im);
?>
Is there any reason why imagecolorallocatealpha() would only be making the text grey?
<?php
header('Content-Type: image/png');
function checkImg($imgname) {
$im = #imagecreatefrompng($imgname);
if(!$im) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
return $im;
}
$hr = 48;
$tOne = "VALID FOR";
$tTwo = $hr." HOURS";
$img = checkImg('img.png');
$font = 'helr67w.ttf';
$size = 9;
$red = imagecolorallocatealpha($img, 255, 0, 0, 75);
imagettftext($img, $size, 0, 225, 132, $red, $font, $tOne);
imagettftext($img, $size, 0, 225, 144, $red, $font, $tTwo);
imagepng($img);
imagedestroy($img);
?>
In your code you don't set the image to support an alpha channel. I can imagine that is causing the issue:
function checkImg($imgname) {
$im = #imagecreatefrompng($imgname);
if(!$im) {
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
// Turn off alpha blending and set alpha flag
imagealphablending($im, true);
imagesavealpha($im, true);
return $im;
}
See imagesavealpha PHP Manual and imagealphablending PHP Manual