ImagickDraw::annotation

(PECL imagick 2, PECL imagick 3)

ImagickDraw::annotation β€” РисуСт тСкст Π½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅

ОписаниС

public function ImagickDraw::annotation(float $x, float $y, string $text): bool
Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

Π€ΡƒΠ½ΠΊΡ†ΠΈΡŽ ΠΏΠΎΠΊΠ° Π½Π΅ Π·Π°Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π»ΠΈ; для знакомства доступСн Ρ‚ΠΎΠ»ΡŒΠΊΠΎ список Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ΠΎΠ².

РисуСт тСкст Π½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

x

ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° X, Π³Π΄Π΅ рисуСтся тСкст

y

ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° Y, Π³Π΄Π΅ рисуСтся тСкст

text

ВСкст для рисования Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π½Π΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ значСния послС выполнСния.

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
15
use_contact_form at Jonas-Kress dot de ΒΆ
16 years ago
may help someone...

<?php
    /**
     * Split the given text into rows fitting the given maxWidth
     *
     * @param unknown_type $draw
     * @param unknown_type $text
     * @param unknown_type $maxWidth
     * @return array
     */
    private function getTextRows($draw, $text, $maxWidth)
    {        
        $words = explode(" ", $text);
        
        $lines = array();
        $i=0;
        while ($i < count($words)) 
        {//as long as there are words

            $line = "";
            do
            {//append words to line until the fit in size
                if($line != ""){
                    $line .= " ";
                }
                $line .= $words[$i];
                
                
                $i++;
                if(($i) == count($words)){
                    break;//last word -> break
                }
                
                //messure size of line + next word
                $linePreview = $line." ".$words[$i];
                $metrics = $this->canvas->queryFontMetrics($draw, $linePreview);
                //echo $line."($i)".$metrics["textWidth"].":".$maxWidth."<br>";
                
            }while($metrics["textWidth"] <= $maxWidth);
            
            //echo "<hr>".$line."<br>";
            $lines[] = $line;
        }
        
        //var_export($lines);
        return $lines;
    }
?>
up
9
Anonymous ΒΆ
16 years ago
Here's how to create a header image and write it to file.  This took me a while to figure out.  I hope this helps.

<?php

/* Text to write */
$text = "Hello World!";

/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // Transparent

/* Font properties */
$draw->setFont('Arial');
$draw->setFontSize(50);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $text);

/* Create text */
$draw->annotation(0, $metrics['ascender'], $text);

/* Create image */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);

/* Save image */
file_put_contents('/path/to/file.png', $image);
?>
up
0
daniel at justathought dot net ΒΆ
2 years ago
[As mentioned in another comment] To correctly position text in the vertical plane when using `annotation()`, you can use the value of the `"ascender"` element returned by `Imagick::queryFontMetrics()`.

So, to effectively make your text touch [0, 0], for example, you could do something like the following.

<?php
$textDraw = new ImagickDraw();
$textDraw->setFont($fontPathname);
$textDraw->setFontSize($fontSize);
$textDraw->setFillColor(new ImagickPixel($color));

$imagick= new Imagick();
$textMetrics = $imagick->queryFontMetrics($textDraw, 'Hello, World!');

$textDraw->annotation(
    0,
    $textMetrics['ascender'],  // <-- This is the important bit :-)
    'Hello, World!'
);
?>

Hope that helps clarify.  The other comment was a blessing for me.
up
-1
Anonymous ΒΆ
16 years ago
You can use this method to break your text so that it'll fit a certain $maxWidth.

<?php
/**
 * @param string $text
 * @param int $maxWidth
 */
protected function _fitText($text, $maxWidth)
{
    $im = new Imagick();
    $im->newImage($this->_width, $this->_height, "none");

    $lines = explode(PHP_EOL, trim($text));
    $DEBUG_LOOP = 0;

    for ($k = 0; $k < count($lines); ++$k) {
        do {
            $drawText = new ImagickDraw();
            // set your font settings like size, family, .. here
            $metrics  = $im->queryFontMetrics($drawText, $lines[$k]);
            $fits     = $metrics["textWidth"] <= $maxWidth;

            if ($fits) {
                break;
            }

            $pos = mb_strrpos($lines[$k], " ");
            if ($pos === false) {
                throw new RuntimeException("can not make it fit");
            }
            if (!isset($lines[$k + 1])) {
                $lines[$k + 1] = null;
            }
            $lines[$k + 1] = trim(mb_substr($lines[$k], $pos + 1) . " " . $lines[$k + 1]);
            $lines[$k]     = trim(mb_substr($lines[$k], 0, $pos));

            if (++$DEBUG_LOOP >= 200) {
                throw new RuntimeException("infinite loop");
            }
        } while (!$fits);
    }
    $text     = implode(PHP_EOL, $lines);
    $drawText = new ImagickDraw();
    // set your font settings like size, family, .. here again!
    $metrics  = $im->queryFontMetrics($drawText, $text);
    $metrics["text"] = $text;
    assert('$metrics["textWidth"] <= $maxWidth');
    return $metrics;
}
?>
up
-1
web at synaptech dot fr ΒΆ
12 years ago
In some cases the (custom) font can be truncated on the side parts, especially the handwritten ones.
To improve the above note from Anonymous on "how to create a header image", I've changed this part of the code:

<?php
/* Create text */
$draw->annotation( $metrics['boundingBox']['y2'], $metrics['ascender'], $text );

/* Create image */
$image->newImage( $metrics['textWidth'] + $metrics['boundingBox']['y2'], $metrics['textHeight'], $background );
?>