downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Imagick::queryFonts> <Imagick::quantizeImages
[edit] Last updated: Fri, 25 May 2012

view this page in

Imagick::queryFontMetrics

(PECL imagick 2.0.0)

Imagick::queryFontMetricsフォントメトリクスを表す配列を返す

説明

array Imagick::queryFontMetrics ( ImagickDraw $properties , string $text [, bool $multiline ] )

フォントメトリクスを表す、多次元配列を返します。

パラメータ

properties

フォントのプロパティを含む ImagickDraw オブジェクト。

text

テキスト。

multiline

マルチラインパラメータ。空にすると自動検出します。

返り値

フォントメトリクスを表す、多次元配列を返します。

エラー / 例外

エラー時に ImagickException をスローします。

例1 Imagick::queryFontMetrics() の使用法

テキストのメトリクスを取得し、画面上に結果を表示します。

<?php
/* 新しい Imagick オブジェクトを作成します */
$im = new Imagick();

/* ImagickDraw オブジェクトを作成します */
$draw = new ImagickDraw();

/* フォントを設定します */
$draw->setFont('/path/to/font.ttf');

/* フォントメトリクスを出力します。複数行を自動検出します */
var_dump($im->queryFontMetrics($draw"Hello World!"));
?>



Imagick::queryFonts> <Imagick::quantizeImages
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes Imagick::queryFontMetrics
abcrdw at gmail dot com 18-Sep-2011 07:49
This script will print a watermark on the middle of an image. The width of the watermark will depend on the width of the image.

<?php
// Create a new Imagick object
$objImage = new Imagick( 'path/to/image.jpg' );

// Get the width and height of the image
$imgSize   = $objImage->getImageGeometry();
$imgWidth  = $imgSize['width'];
$imgHeight = $imgSize['height'];

// Create a new text object
$objText = new ImagickDraw();

// Set te text color
$objText->setFillColor( new ImagickPixel('grey') );

// Set the text transparency: 0 = transparent, 1 = opaque
$objText->setFillAlpha( 0.2 );

// Top left will be point of reference
$objText->setGravity( Imagick::GRAVITY_NORTHWEST );

// Create an array for the textwidth and textheight
$textProperties = array( 'textWidth' => 0 );

// Set the desired width of the watermark to 90% of the image width
$textDesiredWidth = intval( $imgWidth * 0.9 );

// Set an initial value for the fontsize, will be increased in the loop below
$fontSize = 0;

// We use the domain name of the server for the watermark text
$text = $_SERVER['SERVER_NAME'];

// Increase the fontsize until we have reached our desired width
while ( $textProperties['textWidth'] <= $textDesiredWidth ) {
   
$objText->setFontSize( $fontSize );
   
$textProperties = $objImage->queryFontMetrics( $objText, $text );
   
$fontSize++;
}

// Calculate the horizontal starting position
$watermarkPosX = intval( ($imgWidth - $textProperties['textWidth']) / 2 );

// Calculate the vertical starting position
$watermarkPosY = floor( ($imgHeight - $textProperties['textHeight']) / 2 );

// Composite the text on the image
$objImage->annotateImage( $objText, $watermarkPosX, $watermarkPosY, 0, $text );

// The browser must know this is JPEG image or else it will display garbled text
header( 'Content-Type: image/jpeg' );

// Display the image
echo $objImage;
?>
thok nojunk at spammail thok dot ca 03-Jan-2011 10:49
The following fields constitute my current PHP 5.2.1/Imagick 6.5.1 queryFontMetrics() return array (once setFontSize() and setFont() have been set):

characterWidth: maximum character ("em") width
characterHeight: maximum character height
ascender: the height of character ascensions (i.e. the straight bit on a 'b')
descender: the height of character descensions (i.e. the straight bit on a 'p')
textWidth: width of drawn text in pixels
textHeight: height of drawn text in pixels
maxHorizontalAdvance: maximum pixels from start of one character to start of the next
boundingBox: array of x1, y1, x2, y2 bounding borders
originX, originY: ?

originX seems to shadow the textWidth field, and originY has been zero thus far in my exploration.

If you're looking to center text or locate it at an edge, though, use ImagickDraw::setGravity(int $gravity), where $gravity is one of the following constants:

Imagick::GRAVITY_NORTHWEST
Imagick::GRAVITY_NORTH
Imagick::GRAVITY_NORTHEAST
Imagick::GRAVITY_WEST
Imagick::GRAVITY_CENTER
Imagick::GRAVITY_EAST
Imagick::GRAVITY_SOUTHWEST
Imagick::GRAVITY_SOUTH
Imagick::GRAVITY_SOUTHEAST

Using setGravity() is far less finicky for the simpler tasks.
george at sogetthis dot com 22-Nov-2010 12:44
Up to and including imagick 3.0.1, queryfontmetrics would output size data based on an image resolution of 72x72. (see bug: http://pecl.php.net/bugs/bug.php?id=19907)

Should you have set a resolution other than this and be using 3.0.1 or below, you need to scale measurements up by a factor of YOUR_RESOLUTION / 72

e.g. assuming you've set a resolution of 300
<?php
$im
= new Imagick();
$im->setResolution(300,300);

$draw = new ImagickDraw();
$draw->setFont('/path/to/arial.ttf');
$draw->setFontSize(72 * (300 / 72));
$data = $im->queryfontmetrics($draw, 'hello world');
var_dump($data);
?>

In 3.0.2 and above, the solution is to set the resolution on the draw object to that of the image resolution
<?php
$im
= new Imagick();
$im->setResolution(300,300);

$draw = new ImagickDraw();
$draw->setResolution(300,300);
$draw->setFont('/path/to/arial.ttf');
$draw->setFontSize(72);
$draw->setFillColor('#ff0000');
$data = $im->queryFontMetrics($draw, $string);

var_dump($data);
?>
Pete &#39;the Pete&#39; de Pijd 29-Jun-2009 03:18
You can also use $imagickDraw()->setTextAlignmnent(Imagick::ALIGN_CENTER) and $imagickDraw->annotation(..) instead.
waage 30-Jan-2008 04:57
You can use this to center a text within a box

<?php

$data
= "Hello world";

$text = new ImagickDraw();
$text->setFontSize(12);
$text->setFont("Arial");

$boxWidth = 210;

$im = new Imagick();

$fm = $im->queryFontMetrics($text, $data, false);

$textXLoc = ($boxWidth / 2) - ($fm["textWidth"] / 2);

?>

$textXLoc is now the starting location you need to use to feed to your annotateImage() function.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites