imagettftext 是以 low-left 為基準點....
imagettfbbox function
Description ¶
array imagettfbbox ( float
$size
, float $angle
, string $fontfile
, string $text
)
This function calculates and returns the bounding box in pixels for a TrueType text.
以這個例子來看17 -> font size
0 -> angle
Description ¶
array imagettftext ( resource
$image
, float $size
, float $angle
, int $x
, int $y
, int $color
, string $fontfile
, string $text
)
Writes the given
text
into the image using TrueType fonts.Parameters ¶
image
- An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
size
- The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2).
angle
- The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.
x
- The coordinates given by
x
andy
will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), wherex
andy
define the upper-left corner of the first character. For example, "top left" is 0, 0. y
- The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.
color
- The color index. Using the negative of a color index has the effect of turning off antialiasing. Seeimagecolorallocate().
<?php
$tb = imagettfbbox(17, 0, 'airlock.ttf', 'Hello world!');?>
$tb would contain:
Array
(
[0] => 0 // lower left X coordinate
[1] => -1 // lower left Y coordinate
[2] => 198 // lower right X coordinate
[3] => -1 // lower right Y coordinate
[4] => 198 // upper right X coordinate
[5] => -20 // upper right Y coordinate
[6] => 0 // upper left X coordinate
[7] => -20 // upper left Y coordinate
)
應用: 如果你要計算字型的width 和 height
width = $th[2] - $tb[0]
height = $th[1] - $tb[5]
For horizontal alignment, we need to substract the "text box's" width { $tb[2] or $tb[4] } from the image's width and then substract by two.
Saying you have a 200px wide image, you could do something like this:
<?php
$x = ceil((200 - $tb[2]) / 2); // lower left X coordinate for textimagettftext($im, 17, 0, $x, $y, $tc, 'airlock.ttf', 'Hello world!'); // write text to image?>
imagepsloadfont
搭配 imagepstext() 使用
(PHP 4, PHP 5)
imagepsloadfont — Load a PostScript Type 1 font from file
Description
resource imagepsloadfont ( string
$filename
)
Load a PostScript Type 1 font from the given
filename
.imagefttext
(PHP 4 >= 4.0.7, PHP 5, PHP 7)
imagefttext — Write text to the image using fonts using FreeType 2
Description ¶
array imagefttext ( resource
$image
, float $size
, float $angle
, int $x
, int $y
, int $color
,string $fontfile
, string $text
[, array $extrainfo
] )Parameters ¶
image
- An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
size
- The font size to use in points.
angle
- The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text.
x
- The coordinates given by
x
andy
will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), wherex
andy
define the upper-left corner of the first character. For example, "top left" is 0, 0. y
- The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character.
color
- The index of the desired color for the text, see imagecolorexact().
fontfile
- The path to the TrueType font you wish to use.Depending on which version of the GD library PHP is using, when
fontfile
does not begin with a leading/ then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.<?php// Set the enviroment variable for GDputenv('GDFONTPATH=' . realpath('.'));// Name the font to be used (note the lack of the .ttf extension)$font = 'SomeFont';?>
Reference : http://php.net/manual/en/function.imagettftext.php
沒有留言:
張貼留言