2024 年 PHP 日本研討會

ImagickDraw::annotation

(PECL imagick 2, PECL imagick 3)

ImagickDraw::annotation在影像上繪製文字

描述

公開 ImagickDraw::annotation(浮點數 $x, 浮點數 $y, 字串 $text): 布林值
警告

此函式目前沒有說明文件;僅提供其參數列表。

在影像上繪製文字。

參數

x

繪製文字的 x 座標

y

繪製文字的 y 座標

text

要在影像上繪製的文字

回傳值

無回傳值。

新增註解

使用者貢獻的註解 5 則註解

15
use_contact_form at Jonas-Kress dot de
15 年前
可能對某些人有幫助...

<?php
/**
* 將給定的文字分割成符合指定最大寬度的多行文字
*
* @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))
{
//只要還有詞彙

$line = "";
do
{
//將詞彙附加到行,直到符合大小
if($line != ""){
$line .= " ";
}
$line .= $words[$i];


$i++;
if((
$i) == count($words)){
break;
//最後一個詞彙 -> 跳出迴圈
}

//測量此行加上下一個詞彙的尺寸
$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;
}
?>
9
匿名
15 年前
以下是建立標題圖片並將其寫入檔案的方法。這花了我一些時間才弄清楚。希望這個方法對您有所幫助。

<?php

/* 要寫入的文字 */
$text = "Hello World!";

/* 建立 Imagick 物件 */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('#000000');
$background = new ImagickPixel('none'); // 透明

/* 字型屬性 */
$draw->setFont('Arial');
$draw->setFontSize(50);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

/* 取得字型度量 */
$metrics = $image->queryFontMetrics($draw, $text);

/* 建立文字 */
$draw->annotation(0, $metrics['ascender'], $text);

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

/* 儲存圖片 */
file_put_contents('/path/to/file.png', $image);
?>
0
daniel at justathought dot net
5 個月前
[如同其他評論所述] 要在使用 `annotation()` 時正確地在垂直平面上放置文字,您可以使用 `Imagick::queryFontMetrics()` 返回的 `"ascender"` 元素的值。

因此,若要有效地讓您的文字觸及 [0, 0],例如,您可以執行以下操作。

<?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'], // <-- 這是關鍵 :-)
'Hello, World!'
);
?>

希望這樣能釐清問題。另一則評論對我來說非常有幫助。
-1
匿名
15 年前
您可以使用這個方法來斷行,讓您的文字符合特定的 $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;
}
?>
-1
web at synaptech dot fr
11 年前
在某些情況下,(自訂)字體的側邊部分可能會被截斷,尤其是手寫字體。
為了改進上面匿名關於「如何建立標題圖片」的說明,我修改了這部分的程式碼

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

/* 建立圖片 */
$image->newImage( $metrics['textWidth'] + $metrics['boundingBox']['y2'], $metrics['textHeight'], $background );
?>
To Top