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
Related
PHP code:
<?php
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text="hello";
$font_size=25;
$width=200;
$height=200;
$image=imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, 'consolas.ttf', $text);
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
?>
When the image is generated, only lines are drawn but text("hello") is not drawn on it.
The font is also in the same directory, I have also kept it in another directory and added the complete path no luck.
I'm debugging this issue and found that font file is not accessible.
I have checked by turning on debugging as below:
error_reporting(E_ALL);
ini_set("display_errors", 1);
And commented lines below.
header('content-type:image/png');
imagepng($image);
Got error:
Warning: imagettftext(): Could not find/open font in
/var/www/html/index.php on line 24
Soultion:
Path to font file should be realpath on server. So the code should be:
imagettftext($image, $font_size, 0, 15, 30, $black, realpath('consolas.ttf'), $text);
Also change in render image without giving $imgSrc and changing it to
imagepng($image);
instead of
$imgSrc="out.png";
imagejpeg($image, $imgSrc);
Finally complete code will look like:
ob_start();
session_start();
$_SESSION['secure'];
header('content-type:image/png');
$text = "hello";
$font_size = 25;
$width = 200;
$height = 200;
$image = imagecreate($width, $height);
imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
for ($x = 1; $x <= 30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $black);
}
imagettftext($image, $font_size, 0, 15, 30, $black, realpath('Consolas.ttf'), $text);
imagepng($image);
This question already has answers here:
imagejpeg(); not generating image on host server , working fine on localhost
(2 answers)
Closed 9 years ago.
Ok, here is the problem. This code works on localhost, but not on a live site
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x<=30; $x++){
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $line_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);
?>
The localhost and the webhosting are both running the same php version
Take a look at this picture before you post anything, please!:
http://s2.postimg.org/etamdsk95/help_me.png
try to look at error.log (APACHE), it could also be
possible that there is a session problem, you could
try to hide all errors by using "error_reporting(0);
try this:
<?php
#session_start();
error_reporting(0);
#header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
$line_color = imagecolorallocate($image, 0, 0, 0);
for ($x=1; $x<=30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $line_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font2.ttf', $text);
imagejpeg($image);
?>
but the best, would be to fix the error, so look # the error.log
http://php.net/manual/en/function.error-reporting.php
if you got any error message, you could tell us - so we will try to help you :)
there are some error at your code, but it's not the PHPGD lib error, please notice code below:
<?php
//session_start();
header('Content-Type: image/jpeg');
//$_SESSION['secure']; disabled, this will lead to notice: undefined index
$text = "he123";
$font_size = 30;
$image_width = 120;
$image_height = 40;
$image = imagecreate($image_width, $image_height) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 255, 255, 255); //add background color
$text_color = imagecolorallocate($image, 255, 0, 0); //red for the text
$line_color = imagecolorallocate($image, 0,255,0); // green for the line
for($x=1; $x<=30; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $line_color);
}
// add ./ to the font name
imagettftext($image, $font_size, 0, 15, 30, $text_color, "./font2.ttf", $text);
imagejpeg($image);
imagedestroy($image); // remember to clear the chace :p
?>
here's the output
This Script work on localhost (Windows), but not on the Server (Debian):
I have uploaded the files 1:1. Please help me, Thanks :)
<?php
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr, $user)
{
// Load image
$img = imagecreatefrompng($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 0, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
$white = ImageColorAllocate ($img, 255, 255, 255);
ImageTTFText ($img, 35, 0, 15, 350, $white, "Big.ttf",
$user);
// Don't forget to output a correct header
header('Content-Type: image/jpg');
// Save the image (overwrite)
imagepng($img);
imagedestroy($img);
}
$img_src = 'playerimages/streamnew.png';
$box = imagettfbbox ( 35 , 0 , "Big.ttf" , $_GET['username'] );
$width = abs($box[4] - $box[0]);
$height = abs($box[5] - $box[1]);
$x1= 10;
$y1= 310;
$x2 = 25+$width;
$y2=360;
red_rectangle($img_src,$x1,$y1,$x2,$y2,50, $_GET['username']);
?>
THE ANSWER: $_SERVER['DOCUMENT_ROOT']."/big.ttf"
I have the following code for my captcha. The lines in the image are generated but the 4 digit number is not displayed. I assume there is some problem in
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
function of code. I tried setting the path to the font file using $_server['DOCUMENT_ROOT']; yet didn't work.
here's the code:
<?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);
for($x=1; $x<=30; $x++){
$x1 = rand(1,100);
$x2 = rand(1,100);
$y1 = rand(1,100);
$y2 = rand(1,100);
imageline($image, $x1, $x2, $y1, $y2, $text_color);
}
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
imagejpeg($image);
?>
Below code Perfectly working in my pc
Remember font path should be correct.
output-
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
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);
?>