PHP: Deboss the text with font - php

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);

Related

PHP - GD - how wrap a text

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

imagecopy PHP for progress bar doesn't work

I have a problem making the imagecopy function work in PHP.
I don't even know where to start debugging; images is a new fruit for me.
Portion where I have problem:
imagecopy($png, $bgr, 0, 0, 0, 0, 0, 0, $width, $height);
imagecopy($png, $bgr, 0, 0, 0, 0, 0, 0, imagesx($pbuble), imagesy($pbuble));
Full code (my plan is first to copy on top of $png <- $bgr <- $pbuble):
<?php
//Check for correct GET variables
if (is_numeric($_GET['max']) AND is_numeric($_GET['val'])) {
$max = $_GET['max'];
$val = $_GET['val'];
} else {return false;}
//Create empty placeholder
$png = imagecreate(380, 37);
$black = imagecolorallocate($png, 0, 0, 0);
imagecolortransparent($png, $black);
//Create background image with transparency
$bgr = imagecreatefrompng("progress-bgr.png");
imageAlphaBlending($bgr, true);
imageSaveAlpha($bgr, true);
//Calculate width of progress bar atm
$width = imagesx($bgr);
$height = imagesy($bgr);
$pb_width = $val * $width / $max;
//Percentage buble
$pbuble = imagecreatefrompng("percentage-bubble.png");
imageAlphaBlending($pbuble, true);
imageSaveAlpha($pbuble, true);
$perct = $val * 100 / $max;
$txt = $perct . "%";
$txt_color = imagecolorallocate($pbuble, 255, 255, 255);
$font = "dejavusans-webfont.ttf";
$font_size = 7;
$bbox = imagettfbbox($font_size, 0, $font, $txt);
$x = $bbox[0] + (imagesx($pbuble) / 2) - ($bbox[4] / 2);
//$y = $bbox[1] + (imagesy($pbuble) / 2) - ($bbox[5] / 2);
imagettftext($pbuble, $font_size, 0, $x, 14, $txt_color, $font, $txt);
//
imagecopy($png, $bgr, 0, 0, 0, 0, 0, 0, $width, $height);
imagecopy($png, $bgr, 0, 0, 0, 0, 0, 0, imagesx($pbuble), imagesy($pbuble));
//Output image
header("Content-type: image/png");
imagepng($png);
//
imagedestroy($png);
imagedestroy($bgr);
imagedestroy($pbuble);
?>
Stupid me :)
imagecopy($png, $bgr, 0, 0, 0, 0, 0, 0, $width, $height);
has two extra parameters.
should be:
imagecopy($png, $bgr, 0, 0, 0, 0, $width, $height);

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']);
}

php - fit text on text-to-image

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);
?>

Categories