Creating captcha with GD library, no result, - php

When I check the index page it does not show anything but a broken image.
These are the files I am using.
index.php
<?php
session_start();
$_SESSION['secure'] = rand(1000, 9999);
?>
<img src="./generate.php" />
generate.php
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 110;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $fon_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);

Related

PHP GD Image generation broken

I need help in my PHP image code. I'm trying to make Captcha, but all I get is broken image icon ?
This is my code:
<?php
header('Content-type: image/jpeg');
$text = "1111";
$font_size = 30;
$image_width = 110;
$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);
imagejpg($image);
?>
"font.ttf" is next to my php file, and names are matching
Use
imagejpeg($image);
Instead of
imagejpg($image);
As imagejpg() doesn't exist.
When debugging you should comment out the header to see the errors.

Captcha validation in PHP

I used the following code to create Captcha to my form. Captcha creating well. Now I want to change font-size, and Font character spaces. I don't know how to change in the following code.
<?php
session_start();
$code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(150, 35);
$bg = imagecolorallocate($im, 255, 255, 255);
$fg = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 5, 5, $bg);
imagestring($im, 5, 8, 8, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the code below to implement a simple captcha using gd library of PHP. As you are beginner, here is a sample code for quick testing, it covers font-sizing also:
<?php
session_start();
header('Content-type: image/jpeg');
$text = rand(1000, 9999);
$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);
for ($x=1; $x<=40; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text);
imagejpeg($image);
?>
In imagettftext function:
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed
Here is the good tutorial on how to build captcha in php -> link
you can change font using imagestring function

GD Library not working

The generate.php is working, however if I try to retrieve the image on my index.php using <img src="generate.php"/> I get a blank screen.
generate.php
<?php
header('Content-type: image/png');
if(isset($_GET['email'])){
$email = $_GET['email'];
}else{
$email = 'No email specified';
}
$email_length = strlen($email);
$font_size = 4;
$image_height = ImageFontHeight($font_size);
$image_width = ImageFontWidth($font_size) * $email_length;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_color = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $email, $font_color);
imagepng($image);
?>
index.php
Name:<br>
Dave
<br><br>
Email: <br>
<img src="generate.php"/>

Gd Library Image Style imagettftext

How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image

Create transparent png with text from scratch in php

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>

Categories