I created an image 500X200 with white background and text on it.
How can I wrap "Text" if it will be long in GD.
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill ( $image, 0, 0, $white );
...
imagettftext($image, 18, 0, $x, 100, $black, $font, "text");
GD supports rotation, vertical and horizontal alignments, configurable line heights and leading, word wrap (obviously), character wrap, and text outlines. While this frankly belongs in a class (or a namespace) due its size, it’s in a function. You’re free to trim away unneeded functionality.
Example usage:
$size = 14;
$angle = 34;
$left = 10;
$top = 40;
$color = imagecolorallocate($im, 255, 0, 0);
$fontfile = "arial.ttf";
$text = "Test";
$opt = array('width' => 300,'line_height' => 15,'orientation' =>array(ORIENTATION_TOP, ORIENTATION_LEFT),
'align' => ALIGN_LEFT,
'v_align' => VALIGN_TOP,
'outlines' = array(
array(5, imagecolorallocate($im, 0, 255, 0)),
array(2, imagecolorallocate($im, 0, 0, 255)),
),
// More options
);
imagettftextboxopt($im, $size, $angle, $left, $top, $color, $fontfile, $text, $opt);
UPDATE:
And also refer this question
Related
I've tried the above code for creating arc with transparent white background, but it is always shown in distorted manner. Please help me how to make them smooth curved one's?
$im = imagecreatetruecolor(300, 300);
$backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $backgroundColor);
$white = imagecolorallocate($im, 255,255,255);
imagecolortransparent($im, $white);
// Allocate A Color For The Text
$dark_blue = imagecolorallocate($im, 30, 65, 71);
imagesetthickness($im, 2);
//creation of thick arc
$thickness = 40;
for ($i = $thickness; $i > 0; $i--) {
imagearc($im, 150, 220, 180 - $i, 180 - $i, 180, 0, $dark_blue);
}
Increase the image size and the arc thickness, then gradually the arc smoothness keeps getting antialiased. I'm adding my code below for half ring/half donut image/half circle :
enter image description here
function scoreHalfDoughnutImage()
{
$score = 70;
$maxScore = 100;
$color = '#FC950C';
$imagePath = "doughnutContent2Image_".$score.".jpg";
// Create Image From Existing File
$im = imagecreatetruecolor(550, 340);// Create a 550x340 image
//imagealphablending($img,true);
$backgroundColor = imagecolorallocatealpha($im, 255, 255, 255, 127);//blending colors
imagefill($im, 0, 0, $backgroundColor);//creating white background
// Switch antialiasing on for one image
$white = imagecolorallocate($im, 255,255,255);
$black = imagecolorallocate($im, 0, 0, 0);
// for making transparent bg of white bg
imagecolortransparent($im, $white);
// Allocate A Color For The Text
$orange = imagecolorallocate($im, 252, 149, 12);
$dark_blue = imagecolorallocate($im, 30, 65, 71);
imagesetthickness($im, 4);
//creation of thick arc
$thickness = 40;
for ($i = $thickness; $i > 0; $i--) {
imagearc($im, 275, 273, (180 - $i)*3, (180 - $i)*3, 180, 0, $dark_blue);
imagearc($im, 275, 273, (180 - $i)*3, (180 - $i)*3, 180, 180+($score*1.8), $orange);
}
//imageantialias($im, true);
imagesetthickness($im, 1);
$text='NA';
// Setup the title on the center circle
if($score>=0)
$text=$score;
// Set Path to Font File
$font_path = 'lib/reports/MotivationEvaluation/font/arial.ttf';
// Print Text On Image
$textWidth = imagettfbbox(40, 0, $font_path, $text);
$x = 235;
if($textWidth[2] < 84)
{
$x = 250;
}
if($text!=='NA')
{
imagettftext($im, 45, 0, $x, 240, $dark_blue, $font_path, $text);
}
else
{
imagettftext($im, 35, 0, $x, 240, $dark_blue, $font_path, $text);
}
// Print Text On x-axis n y-axis
imagettftext($im, 32, 0, 25, 315, $dark_blue, $font_path, 0);
imagettftext($im, 32, 0, 25+450, 315, $dark_blue, $font_path, 100);
//imagealphablending($im, true); //not needed as we created the image
with alpha
imagesavealpha($im, true);
// Send Image to Browser
imagejpeg($im,$imagePath);
imagedestroy($im);
return $imagePath;
}
Please have a look on the following link, you will get the deboss text:
http://www.wristbandtoday.com/wristband/gd/load.php?route=font&size=30&name=1391601784Artbrush.ttf&effect=debossed&color=222222&text=Debossed
I know how to emboss the text:
$emboss = array(array(2, 0, 0), array(0, -1, 0), array(0, 0, -1));
imageconvolution($im, $emboss, 3, 235);
This is the following code to make the text by php and get emboss effect:
header('Content-Type: image/png');
// The text to draw
$text = "Ebossed";
// Replace path by your own font path
$font = WWW_ROOT.DS.'fonts/uploads/9559122015-03-27Artbrush.ttf';
$fontSize = 32;
$text_angle = 0;
$text_padding = 0;
$the_box = $this->_calculateTextBox($text, $font, $fontSize, $text_angle);
$imgWidth = $the_box["width"] + $text_padding;
$imgHeight = $the_box["height"] + $text_padding;
$im = imagecreatetruecolor($imgWidth, $imgHeight);
// 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, $imgWidth-1, $imgHeight-1, $white);
// Add some shadow to the text
imagettftext($im, $fontSize, $text_angle, 0, 32, $grey, $font, $text);
// Add the text
imagettftext($im, $fontSize, $text_angle, 0, 32, $black, $font, $text);
$emboss = array(array(2, 0, 0), array(0, -1, 0), array(0, 0, -1));
imageconvolution($im, $emboss, 3, 235);
imagepng($im,WWW_ROOT.DS.IMAGES_URL.'test/newtes.png'); // image path
imagedestroy($im);
How do U create the Deboss effect?
For Deboss Effect please apply the following line of code:
$debosseffect = array
(
array(0, 0, 1),
array(0, -1, 1),
array(0, 0, -1)
);
Thanks
Finally I got the solution for getting deboss effect
$image = imagecreatefromjpeg(WWW_ROOT.DS.IMAGES_URL.'cliparts/'.$img);
$sharpenMatrix = array
(
array(0, 0, 1),
array(0, -1, 1),
array(0, 0, -1)
);
// calculate the sharpen divisor
$divisor = 3;
$offset = 235;
// apply the matrix
imageconvolution($image, $sharpenMatrix, $divisor, $offset);
header('Content-Type: image/png');
imagepng($image, null, 9);
imagegd($image);
imagedestroy($image);
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 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);
?>
I'm really having a problem finding out how to fix this. I cannot seem to change the background from black. How is it possible?
$string = "foo";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = #imagecreatetruecolor ($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $string, $textcolor);
imagegif($im, 'somefile.gif', 8);
imagedestroy($im);
Use:
imagefill($im, 0, 0, $bg);
Incorporating user279470's answer:
$string = "foo";
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = #imagecreatetruecolor ($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bg);
$textcolor = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 5, 0, 0, $string, $textcolor);
imagepng($im, 'somefile.png', 8);
imagedestroy($im);