note 60183 added to function.imagettfbbox
| From: | peisenmann at gmail dot com | Date: | Thu, 29 Dec 2005 02:41:31 +0000 |
| Subject: | note 60183 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100893@lists.php.net to get a copy of this message | ||
Another function for centered text string.
What it does: Generate a truecolor .png image of a text string. The image will be just large enough
encompass the text and a 2 px border and the text will be centered in it.
It is called from any other page like so...
<img src="linkImg.php?text=php.net is great&border=out" /> // Text with
#&+"'\<> will need to be escaped, but I've found spaces don't cause
errors. I haven't tested this with any other languages.
The following code is the file named linkImg.php
The file was not designed to have anything else here with it, and the open and close php tags should
the the very first and very last characters of the page respectively, as outside whitespace can be a
little evil sometimes.
<?php
//Obtain text and border via GET
//Border can be out, in, or flat
$text = $_GET['text'];
$border = $_GET['border'];
$font = "fontpath"; //(str) "fonts/sasquatchlives.ttf"
$fontsize = font size; //(int) pixels in GD 1, or points in GD 2
//Register box
$box = imagettfbbox ($fontsize, 0, $font, $text);
//Find out the width and height of the text box
$textW = $box[2] - $box[0];
$textH= $box[3]-$box[5];
//Add padding
$paddingx = 10;
$paddingy = 10;
//Set image dimentions
$width = $textW+$paddingx;
$height= $textH+$paddingy;
//Bottom left corner of text
$textx = $paddingx/2;
$texty = $height - $paddingy/2;
//Shadow offset (pixels)
$shadoffx = 1;
$shadoffy = 1;
//Create the image
$img = imagecreatetruecolor($width,$height);
//Define some colors
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$lightgrey = imagecolorallocate($img,200,200,200);
$grey = imagecolorallocate($img,100,100,100);
//Define Text (fg) and background (bg) colors
$bgcol = imagecolorallocate($img,192,213,196); //Celadon (light pastel green)
$fgcol = imagecolorallocate($img,243,104,88); //Peach
// Fill image with background color
imagefill($img,0,0,$bgcol);
//Write Shadow
imagettftext($img, $fontsize, 0, $textx+$shadoffx, $texty+$shadoffy, $grey, $font, $text);
//Write Text
imagettftext($img, $fontsize, 0, $textx, $texty, $fgcol, $font, $text);
//Bordering
//Embossed border (button-looking)
if ($border == "out")
{
imageline ($img,0,0,$width,0,$white);imageline ($img,0,0,0,$height,$white);
imageline ($img,1,1,$width,1,$lightgrey);imageline ($img,1,1,1,$height-1,$lightgrey);
imageline ($img,0,$height-1,$width-1,$height-1,$black);imageline
($img,$width-1,$height-1,$width-1,0,$black);
imageline ($img,2,$height-2,$width-2,$height-2,$grey);imageline
($img,$width-2,$height-2,$width-2,2,$grey);
}
//Flat border
if ($border == "flat")
{
imageline ($img,0,0,$width,0,$white);imageline ($img,0,0,0,$height,$white);
imageline ($img,1,1,$width,1,$grey);imageline ($img,1,1,1,$height-1,$grey);
imageline ($img,0,$height-1,$width-1,$height-1,$white);imageline
($img,$width-1,$height-1,$width-1,0,$white);
imageline ($img,2,$height-2,$width-2,$height-2,$grey);imageline
($img,$width-2,$height-2,$width-2,2,$grey);
}
//Engraved border (pushed button)
if ($border == "in")
{
imageline ($img,0,0,$width,0,$black);imageline ($img,0,0,0,$height,$black);
imageline ($img,1,1,$width,1,$grey);imageline ($img,1,1,1,$height-1,$grey);
imageline ($img,0,$height-1,$width-1,$height-1,$white);imageline
($img,$width-1,$height-1,$width-1,0,$white);
imageline ($img,2,$height-2,$width-2,$height-2,$lightgrey);imageline
($img,$width-2,$height-2,$width-2,2,$lightgrey);
}
// Header info
header("Content-type: image/png");
//Sends the image
imagepng($img);
imagedestroy($img);
?>
Hope this helps someone!
-Patrick-
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60183
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60183&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60183&report=yes
Search -- http://master.php.net/manage/user-notes.php