imagepsbbox
(PHP 4, PHP 5)
imagepsbbox — Give the bounding box of a text rectangle using PostScript Type1 fonts
Description
$text
, resource $font
, int $size
)$text
, resource $font
, int $size
, int $space
, int $tightness
, float $angle
)Gives the bounding box of a text rectangle using PostScript Type1 fonts.
The bounding box is calculated using information available from character metrics, and unfortunately tends to differ slightly from the results achieved by actually rasterizing the text. If the angle is 0 degrees, you can expect the text to need 1 pixel more to every direction.
Parameters
-
text
-
The text to be written.
-
font_index
-
A font resource, returned by imagepsloadfont().
-
size
-
size
is expressed in pixels. -
space
-
Allows you to change the default value of a space in a font. This amount is added to the normal value and can also be negative. Expressed in character space units, where 1 unit is 1/1000th of an em-square.
-
tightness
-
tightness
allows you to control the amount of white space between characters. This amount is added to the normal character width and can also be negative. Expressed in character space units, where 1 unit is 1/1000th of an em-square. -
angle
-
angle
is in degrees.
Return Values
Returns an array containing the following elements:
0 | left x-coordinate |
1 | upper y-coordinate |
2 | right x-coordinate |
3 | lower y-coordinate |
Examples
Example #1 imagepsbbox() usage
<?php
// Create image handle
$im = imagecreatetruecolor(200, 200);
// Allocate colors
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Load the PostScript Font
$font = imagepsloadfont('font.pfm');
// Make a bounding box for the font
$bbox = imagepsbbox('Sample text is simple', $font, 12);
// Define our X and Y cordinates
$x = ($bbox[2] / 2) - 10;
$y = ($bbox[3] / 2) - 10;
// Write the font to the image
imagepstext($im, 'Sample text is simple', $font, 12, $black, $white, $x, $y);
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Notes
Note: This function is only available if PHP is compiled using --with-t1lib[=DIR] .
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Обработка и генерация изображений
- Обработка изображений и GD
- gd_info
- getimagesize
- getimagesizefromstring
- image_type_to_extension
- image_type_to_mime_type
- image2wbmp
- imageaffine
- imageaffinematrixconcat
- imageaffinematrixget
- imagealphablending
- imageantialias
- imagearc
- imagechar
- imagecharup
- imagecolorallocate
- imagecolorallocatealpha
- imagecolorat
- imagecolorclosest
- imagecolorclosestalpha
- imagecolorclosesthwb
- imagecolordeallocate
- imagecolorexact
- imagecolorexactalpha
- imagecolormatch
- imagecolorresolve
- imagecolorresolvealpha
- imagecolorset
- imagecolorsforindex
- imagecolorstotal
- imagecolortransparent
- imageconvolution
- imagecopy
- imagecopymerge
- imagecopymergegray
- imagecopyresampled
- imagecopyresized
- imagecreate
- imagecreatefromgd2
- imagecreatefromgd2part
- imagecreatefromgd
- imagecreatefromgif
- imagecreatefromjpeg
- imagecreatefrompng
- imagecreatefromstring
- imagecreatefromwbmp
- imagecreatefromwebp
- imagecreatefromxbm
- imagecreatefromxpm
- imagecreatetruecolor
- imagecrop
- imagecropauto
- imagedashedline
- imagedestroy
- imageellipse
- imagefill
- imagefilledarc
- imagefilledellipse
- imagefilledpolygon
- imagefilledrectangle
- imagefilltoborder
- imagefilter
- imageflip
- imagefontheight
- imagefontwidth
- imageftbbox
- imagefttext
- imagegammacorrect
- imagegd2
- imagegd
- imagegif
- imagegrabscreen
- imagegrabwindow
- imageinterlace
- imageistruecolor
- imagejpeg
- imagelayereffect
- imageline
- imageloadfont
- imagepalettecopy
- imagepalettetotruecolor
- imagepng
- imagepolygon
- imagepsbbox
- imagepsencodefont
- imagepsextendfont
- imagepsfreefont
- imagepsloadfont
- imagepsslantfont
- imagepstext
- imagerectangle
- imagerotate
- imagesavealpha
- imagescale
- imagesetbrush
- imagesetinterpolation
- imagesetpixel
- imagesetstyle
- imagesetthickness
- imagesettile
- imagestring
- imagestringup
- imagesx
- imagesy
- imagetruecolortopalette
- imagettfbbox
- imagettftext
- imagetypes
- imagewbmp
- imagewebp
- imagexbm
- iptcembed
- iptcparse
- jpeg2wbmp
- png2wbmp
Коментарии
When using imagepsbbox, you are probably trying to do something like creating a button with text, so that the button is large enough for the text...
Below is a very simple example of making a black button just big enough to display white text on it.
<?php
//if text is no variable set sample text
if (!$text)
$text = "This is a sample text";
// set the font size
$fontsize=14;
// load the font to use
$font=ImagePsLoadFont("/fonts/ariam___.pfb");
//get the left lower corner and the right upper
list($lx,$ly,$rx,$ry) = imagepsbbox($text,$font,$fontsize,0,0,0);
// calculate the size of the text
$textwidth = $rx - $lx;
$textheight = $ry - $ly;
// make an image 40 pixels wider and 20 pixels higher than the text
$imh = $textheight + 20;
$imw = $textwidth + 40;
$im = imageCreate( $imw, $imh );
//define colors, first color is used as background color!
$black = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);
//create the text (with the same parameters as imagepsbbox!)
ImagePSText ($im, "$text", $font, $fontsize, $white, $white, 20, 20,'','','',4);
//send the header
header("Content-type: image/jpeg");
// create the image
ImageJPEG ($im,"",100);
//destroy the image & font to free memory
Imagepsfreefont ( $font );
ImageDestroy ( $im );
?>
When using imagepsbbox, keep in mind, that meaning of y-coordinates is slightly different here. Y-coordinates returned by this function are related to the baseline of the text starting at point [0,0]. Positive values represent points ABOVE the baseline, negative values represent points BELOW the baseline. That is why the lower left y-coordinate is always smaller here than the upper right y-coordinate (these two coordinates are actualy values of metrics.descent and metrics.ascent - see T1Lib docs).
So when you want to place some text using coordinates of the top left corner (for example [100,100]), use this:
<?php
$x = 100;
$y = 100;
$text = "Dodge this";
$fontsize=18;
$font=imagepsloadfont("somefont.pfb");
list($lx,$ly,$rx,$ry) = imagepsbbox($text,$font,$fontsize);
imagepstext ($someimage, $text, $font, $fontsize, $somecolor, $somecolor, $x, $y + $ry);
?>
Hope it helps someone, I got stuck with this for a while.
I have a given image width and need to render a long string on that image. By using the following function I'm, able to get an array of strings which each will fit into the images width. It might need a lot of CPU time, but it's cool:
// Function to return an Array of Strings
// Arguments: String to be wrapped, maximum width in pixels of each line, font and fontsize
function imgwordwrap($s, $maxWidth, $font, $size) {
// Make an empty ArrayList
$a = array();
$w = 0; // Accumulate width of chars
$i = 0; // Count through chars
$rememberSpace = 0; // Remember where the last space was
// As long as we are not at the end of the String
while ($i < strlen($s)) {
// Current char
$c = substr($s, $i, 1);
$bb = imagepsbbox($c, $font, $size); // calculate width
$w += abs($bb[3])-abs($bb[1]); // accumulate width
if ($c == ' ') $rememberSpace = $i; // Are we a blank space?
if ($w > $maxWidth) { // Have we reached the end of a line?
$sub = substr($s,0,$rememberSpace); // Make a substring
// Chop off space at beginning
if (substr($sub,0,1)==' ') $sub = substr($sub,1);
// Add substring to the array
$a[] = $sub;
// Reset everything
$s = substr($s,$rememberSpace);
$i = 0;
$w = 0;
}
else {
$i++; // Keep going!
}
}
// Take care of the last remaining line
trim($s);
if (substr($s,0,1)==' ') $s = substr($s,1);
$a[] = $s;
return $a;
}
in my code below, there is an error!
replace
$w += abs($bb[3])-abs($bb[1]); // accumulate width
with
$w += abs($bb[2])-abs($bb[0]); // accumulate width
also after
$bb = imagepsbbox($c, $font, $size); // calculate width
you can add this line because spaces make odd values...
if ($c == ' ' ) $bb = imagepsbbox('i', $font, $size);