Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: eight plus zero?
(Example: nine)

The Note You're Voting On

SkepticaLee
11 years ago
The values do not match the array descriptors at all. If you have to know exactly how much space is being taken up by an annotation, use the following:

<?php
$text
= "Algbqdj";
$im = new Imagick ();
$draw = new ImagickDraw ();
$draw->setStrokeColor ("none");
$draw->setFont ("Arial");
$draw->setFontSize (96);
$draw->setTextAlignment (Imagick::ALIGN_LEFT);
$metrics = $im->queryFontMetrics ($draw, $text);
//these are the values which accurately described the extent of the text and where it is to be drawn:
$baseline = $metrics['boundingBox']['y2'];
$textwidth = $metrics['textWidth'] + 2 * $metrics['boundingBox']['x1'];
$textheight = $metrics['textHeight'] + $metrics['descender'];
$draw->annotation (0, $baseline, $text);
$im->newImage ($textwidth, $textheight, "white");
$im->drawImage ($draw);
$draw->clear ();
$im->writeImage ("test.png");
$im->clear ();
?>

The file test.png should now contain pretty much the complete text with no borders. This may not work for very unusual fonts, but will work OK for Times New Roman and Arial.

<< Back to user notes page

To Top