重力常量非常有用,因为它们可以避免计算可变文本字符串和字体大小的放置位置。
例如
$draw->setGravity (Imagick::GRAVITY_CENTER);
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox jumps over the lazy dog');
将水平和垂直居中文本字符串。
注意,有几个重力常量可用。参见常量 — Imagick 类常量
(PECL imagick 2, PECL imagick 3)
ImagickDraw::setGravity — 设置文本放置重力
不返回值。
示例 #1 ImagickDraw::setGravity() 示例
<?php
function setGravity($fillColor, $strokeColor, $backgroundColor) {
$draw = new \ImagickDraw();
$draw->setStrokeColor($strokeColor);
$draw->setFillColor($fillColor);
$draw->setStrokeWidth(1);
$draw->setFontSize(24);
$gravitySettings = array(
\Imagick::GRAVITY_NORTHWEST => '西北',
\Imagick::GRAVITY_NORTH => '北',
\Imagick::GRAVITY_NORTHEAST => '东北',
\Imagick::GRAVITY_WEST => '西',
\Imagick::GRAVITY_CENTER => '中心',
\Imagick::GRAVITY_SOUTHWEST => '西南',
\Imagick::GRAVITY_SOUTH => '南',
\Imagick::GRAVITY_SOUTHEAST => '东南',
\Imagick::GRAVITY_EAST => '东'
);
$draw->setFont("../fonts/Arial.ttf");
foreach ($gravitySettings as $type => $description) {
$draw->setGravity($type);
$draw->annotation(50, 50, '"' . $description . '"');
}
$imagick = new \Imagick();
$imagick->newImage(500, 500, $backgroundColor);
$imagick->setImageFormat("png");
$imagick->drawImage($draw);
header("Content-Type: image/png");
echo $imagick->getImageBlob();
}
?>
重力常量非常有用,因为它们可以避免计算可变文本字符串和字体大小的放置位置。
例如
$draw->setGravity (Imagick::GRAVITY_CENTER);
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox jumps over the lazy dog');
将水平和垂直居中文本字符串。
注意,有几个重力常量可用。参见常量 — Imagick 类常量
GRAVITY 常量似乎有整数等效项
NORTHWEST = 1
NORTH = 2
NORTHEAST = 3
WEST = 4
CENTER = 5
EAST = 6
SOUTHWEST = 7
SOUTH = 8
SOUTHEAST = 9