I have this code
<?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... a very long text';
// 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);
?>
the problem here is, when I have a very long text, the other won't show.
I've searched that it can be done by
imagettfbbox()
but I don't know how to apply it here.
Any help?
try this code:
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
// Create the image
$im = imagecreatetruecolor($img_width, $img_height);
// 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, $img_width, $img_height, $white);
// The text to draw
$text = 'Testing... a very long text bla.. ';
// Replace path by your own font path
$font = 'arial.ttf';
$fontSize = $img_width / strlen($text) * 1.8;
// Add some shadow to the text
imagettftext($im, $fontSize, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, $fontSize, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
New update code :
<?php
// Set the content-type
header('Content-type: image/png');
$img_width = 400;
$img_height = 30;
$font = 'arial.ttf';
// Create the image
$text = 'Testing... a very long text.. Testing... a very long text.. Testing... a very long text.. Testing... a very long text..';
$curTextLen = strlen($text);
$limit = 35;
$totalLine = ceil($curTextLen / $limit);
$img_height = $img_height * $totalLine;
$im = imagecreatetruecolor($img_width, $img_height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);
for($i = 1; $i <= $totalLine; $i++){
$y = $i * 27;
$textN = substr($text,($limit * ($i-1)),$limit);
imagettftext($im, 20, 0, 11, $y, $grey, $font, $textN);
// Add the text
imagettftext($im, 20, 0, 10, $y, $black, $font, $textN);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Related
this is my code:
header('Content-Type: image/jpeg');
$image_width = 400;
$image_height = 30;
$im = imagecreatetruecolor($image_width, $image_height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = utf8_encode("test 123 abc");
$font = 'myfont.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagejpeg($im);
imagedestroy($im);
can anyone help me find where i am wrong.
background is comming in white i want to remove white and put transparent.please help
this is an image: http://funbusy.com/fbtest/user_image.php
?php
// Create the image
$im = imagecreatetruecolor(400, 25);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 22, 125, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = ucwords('tanuja sree');
// Replace path by your own font path
$font = 'OpenSans-Italic.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()
ob_start();
imagepng($im);
$image_data = ob_get_clean();
$image_code = '<img id="image_code" src="data:image/png;base64,'. base64_encode($image_data) . '">';
imagedestroy($im);
?>
Try adding this piece of code after imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagecolortransparent($im, $white);
imagesavealpha($im,true);
imagealphablending($im, true);
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']);
}
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);
?>