2016年2月25日 星期四

php image



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 $colorstring $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 and y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where x and y 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(170'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($im170$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
Warning
This function was REMOVED in PHP 7.0.0.

Description  

resource imagepsloadfont ( string $filename )
Load a PostScript Type 1 font from the given filename.
<?php// Create a new image instance$im imagecreatetruecolor(35045);// Allocate colors and fill the background$black imagecolorallocate($im000);$white imagecolorallocate($im255255255);imagefilledrectangle($im0034944$white);// Load a font, write to the image and free the font from memory$font imagepsloadfont("bchbi.pfb");imagepstext($im"Testing... It worked!"$font32$white$black3232);imagepsfreefont($font);// Output the imageheader('Content-type: image/png');imagepng($im);imagedestroy($im);?>


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 and y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where x and y 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';?>
<?php// Create a 300x150 image$im imagecreatetruecolor(300150);$black imagecolorallocate($im000);$white imagecolorallocate($im255255255);// Set the background to be whiteimagefilledrectangle($im00299299$white);// Path to our font file$font './arial.ttf';// First we create our bounding box for the first text$bbox imagettfbbox(1045$font'Powered by PHP ' phpversion());// This is our cordinates for X and Y$x $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 25;$y $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;// Write itimagettftext($im1045$x$y$black$font'Powered by PHP ' phpversion());// Create the next bounding box for the second text$bbox imagettfbbox(1045$font'and Zend Engine ' zend_version());// Set the cordinates so its next to the first text$x $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10;$y $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;// Write itimagettftext($im1045$x$y$black$font'and Zend Engine ' zend_version());// Output to browserheader('Content-Type: image/png');imagepng($im);imagedestroy($im);?>


Reference : http://php.net/manual/en/function.imagettftext.php

沒有留言:

張貼留言