how change font of captcha numbers? - php

hi i have form that have captcha and i want number of captcha show arabic or persian but when i change font of code this is not work. how can i make that?
<?php
session_start();
$string = '';
for ($i = 1; $i < 8; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= rand(0,6);
}
$_SESSION['random_code'] = $string;
$dir = 'style/font/';
$image = imagecreatetruecolor(170, 60);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir."Yekan.ttf",
$_SESSION['random_code']);
header("Content-type: image/png");
imagepng($image);
?>

Related

How can I write diagonal text in PHP

I want to import some timestamps from a table and I want to write them diagonal under the graph. How can I do that?
here is my code, but not for the import data.
header("Content-type: image/png");
$img = imagecreate(100, 75);
$bg = imagecolorallocate($img, 255, 255, 0);
$black = imagecolorallocate($img, 0, 0, 0);
$roles = array('anoniem', 'ingelogd', 'leerling', 'docent', 'rector');
$left = 0;
foreach ($roles as $role) {
imagestring ($img, 2, 0, $left, $role, $black);
$left+= 15;
}
$img2 = imagerotate($img, 45, $bg);
imagedestroy($img);
imagepng($img2);
imagettftext() is used to write text to an image resource at any angle. This isn't quite a drop-in replacement for imagestring() but it's not difficult to get working. Modifying your code gives me this:
$img = imagecreate(100, 75);
$bg = imagecolorallocate($img, 255, 255, 0);
$black = imagecolorallocate($img, 0, 0, 0);
$roles = array('anoniem', 'ingelogd', 'leerling', 'docent', 'rector');
$left = 10;
foreach ($roles as $role) {
imagettftext($img, 10, 45, $left, 50, $black, 'arial.ttf', $role);
$left+= 15;
}
header("Content-type: image/png");
imagepng($img);
And this seems to be what you want.
Before:
After:

Underline text using imagettftext

My question is, how do I underline all the text in the image?Code:
function createImage($text)
{
$text .= "\n";
$text = wordwrap($text, 40, "\n");
$newlines = substr_count($text, "\n");
if($newlines == 0)
{
$height = 30;
}
else
{
$height = 30*$newlines-$newlines*7;
}
putenv('GDFONTPATH=' . realpath('.'));
header('Content-Type: image/png');
$im = imagecreatetruecolor(315, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$purple = imagecolorallocate($im, 97, 26, 139);
imagefilledrectangle($im, 0, 0, 399, $height, $white);
$font = 'arialbd.ttf';
imagettftext($im, 11, 0, 10, 20, $purple, $font, $text);
imagepng($im);
imagedestroy($im);
}
createImage("Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow Stackoverflow ");
RESULT
http://i.imgur.com/Jdr7HPy.png
I want all the text to be underlined, I've found some solutions but they're only from left to right, not depending on words.
Use the Unicode underline combining character U+0332.
You will need a loop to or clever array merge to modify the text,
$e = explode(' ', $text);
for($i=0;$i<count($e);$i++) {
$e[$i] = implode('̲', str_split($e[$i]));
}
$text = implode(' ', $e);
You can do this by finding out dimensions of the text in pixels using imagettfbbox(), then use it as reference to draw lines beneath the text. I've played around with it and here's my try:
function createImage($text){
putenv('GDFONTPATH=' . realpath('.'));
$text = wordwrap($text, 40, "\n");
$padding = 10; // padding around text
$uOffs = 2; // distance between the line and the text above it
$uHeight = 1; // height of the under-line
$lines = explode("\n", $text); // split text in lines
$lineLengths = array(); // will store length of each line
$textSize = 11;
$font = 'arialbd.ttf';
// bounding box of all text
$textBoundingBox = array_map('abs', imagettfbbox($textSize, 0, $font, $text));
list($blx, $bly, $brx, $bry, $trx, $try, $tlx, $tly) = $textBoundingBox;
// calculate image dimensions based on the bounding box data
$x = max($brx, $trx) + ($padding * 4);
$y = max($blx, $bly) + ($padding * 4);
$img = imagecreatetruecolor($x, $y);
// determine length of each line of text
foreach($lines as $i => $line){
$box = imagettfbbox($textSize, 0, $font, $line);
$lineLengths[$i] = max($box[2], $box[4]);
}
$white = imagecolorallocate($img, 255, 255, 255);
$grey = imagecolorallocate($img, 128, 128, 128);
$black = imagecolorallocate($img, 0, 0, 0);
$purple = imagecolorallocate($img, 97, 26, 139);
imagefilledrectangle($img, 0, 0, $x - 1, $y - 1, $white);
imagettftext($img, $textSize, 0, $padding + min($tlx, $blx), $padding + min($tly, $bly), $purple, $font, $text);
// starting Y position of the under-line
$uY = $padding + min($tly, $bly);
// underline text...
foreach($lineLengths as $length){
imagefilledrectangle($img, $padding + min($tlx, $blx), $uY + $uOffs, $padding + $length, $uY + $uOffs + $uHeight, $purple);
$uY += 19;
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
}
Result:
This is a little more flexible that using Unicode chars because you have control over the line height, and position relative to the text above it.

Aligning php Generated Image dynamic text in center

i want to align the text generated on the image to the center of the image. for the moment, i dont know if it is possible to align it. below is the code.
$im = #imagecreatefromjpeg('poloroid.jpg');
// 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 = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);
thanks for the help guys.
$im = #imagecreatefromjpeg('poloroid.jpg');
// 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 = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';
$font_size = 20;
$angle = 45;
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
// Get image Width and Height
$image_width = imagesx($im);
$image_height = imagesy($im);
// Get Bounding Box Size
$text_box = imagettfbbox($font_size,$angle,$font,$text);
// Get your Text Width and Height
$text_width = $text_box[2]-$text_box[0];
$text_height = $text_box[7]-$text_box[1];
// Calculate coordinates of the text
$x = ($image_width/2) - ($text_width/2);
$y = ($image_height/2) - ($text_height/2);
// Add some shadow to the text
imagettftext($im, $font_size, 0, $x, $y+1, $grey, $font, $text);
// Add the text
imagettftext($im, $font_size, 0, $x, $y, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);
You can use stil/gd-text class. Disclaimer: I am the author.
<?php
use GDText\Box;
use GDText\Color;
$im = #imagecreatefromjpeg('poloroid.jpg');
$textbox = new Box($im);
$textbox->setFontSize(20);
$textbox->setFontFace('verdana.ttf');
$textbox->setFontColor(new Color(0, 0, 0)); // black
$textbox->setTextShadow(
new Color(0, 0, 0, 80), // black color, but 60% transparent
0,
-1 // shadow shifted 1px to top
);
$textbox->setBox(
0, // distance from left edge
0, // distance from top edge
imagesx($im), // textbox width, equal to image width
imagesy($im) // textbox height, equal to image height
);
// now we have to align the text horizontally and vertically inside the textbox
// the texbox covers whole image, so text will be centered relatively to it
$textbox->setTextAlign('center', 'center');
// it accepts multiline text
$textbox->draw($text);
$uploads_dir = 'uploaded_files/';
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
imagepng($im, $name, 9);
imagedestroy($im);
Demonstration:
I've updated your code a little:
function ImageTTFCenter($image, $text, $font, $size, $angle = 45)
{
$xi = imagesx($image);
$yi = imagesy($image);
$box = imagettfbbox($size, $angle, $font, $text);
$xr = abs(max($box[2], $box[4]));
$yr = abs(max($box[5], $box[7]));
$x = intval(($xi - $xr) / 2);
$y = intval(($yi + $yr) / 2);
return array($x, $y);
}
$im = #imagecreatefromjpeg('poloroid.jpg');
// 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 = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = 'uploaded_files/';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
//$name ="$fbid.png";
$name = $uploads_dir.$fbid.".png"; //this saves the image inside uploaded_files folder
list($x, $y) = ImageTTFCenter($im, $text, $font, 20)
// Add some shadow to the4 text
imagettftext($im, 20, 0, $x, $y+1, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, $x, $y, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
imagedestroy($im);
The ImageTTFCenter function will find the center coordinates of you image which you will tell imagettftext
foreach ($user as $key=>$value){
$bb = imagettfbbox($value['font-size'],0,$fontname,$value['name']);
$WW = abs($bb[2]-$bb[0]);
$XX = ($value['XPos']+$WW);
$HH = abs($bb[5]-$bb[3]);
$HH +=1;
$HHH += $HH;
imagettftext($im, $value['font-size'], 0, $value['XPos'], $value['YPos'], $color[$value['color']], $fontname, $value['name']);
$HHH += 1;
$WIDE = abs($bb[2]-$bb[0]);
$endpoint=$value['XPos']+$WIDE;
$bb2 = imagettfbbox($value['font-size'],0,$fontname,$value['name']);
$WW2 = abs($bb2[2]-$bb2[0]);
$x2pos= $endpoint-$WW2;
imagettftext($im, $value['font-size'], 0, $x2pos, $value['YPos'], $color[$value['color']], $fontname, $value['name']);
}

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

Add a border to text and color

I couldn't find a open source PHP Script that allow me to choose the font and then apply border around it.
I should also be able to select the color text/border and also apply text background shadow.
Which script can do this?
Following code will work for you be sure to donwload arial.ttf and place in directory
<?php
$fontsize = 36;
$fonttext = "test"." ";
$size = imagettfbbox($fontsize, 0, "arial.ttf", $fonttext);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image = imagecreate($xsize, $ysize);
$black = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $black);
$blue = imagecolorallocate($image, 0, 0, 255);
//$white = ImageColorAllocate($image, 255,255,255);
imagettftext($image, $fontsize, 0, abs($size[0]), abs($size[5]), $blue, "arial.ttf", $fonttext);
$bordercolors = imagecolorallocate($image, 255, 0, 0);
$x = 0;
$y = 0;
$w = $xsize - 1; //get width image and decrease 1px or points ?
$h = $ysize - 1; //get height image and decrease 1px or points ?
imageline($image, $x,$y,$x,$y+$h,$bordercolors); //left
imageline($image, $x,$y,$x+$w,$y,$bordercolors); //top
imageline($image, $x+$w,$y,$x+$w,$y+$h,$bordercolors); //right
imageline($image, $x,$y+$h,$x+$w,$y+$h,$bordercolors);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image);
?>
Most of the code is self explanatory .

Categories