本文整理匯總了PHP中Imagick::queryFontMetrics方法的典型用法代碼示例。如果您正苦於以下問題:PHP Imagick::queryFontMetrics方法的具體用法?PHP Imagick::queryFontMetrics怎麽用?PHP Imagick::queryFontMetrics使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Imagick
的用法示例。
在下文中一共展示了Imagick::queryFontMetrics方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: createEmailPic
function createEmailPic($jid, $email)
{
$cachefile = DOCUMENT_ROOT . '/cache/' . $jid . '_email.png';
if (file_exists(DOCUMENT_ROOT . '/cache/' . $jid . '_email.png')) {
unlink(DOCUMENT_ROOT . '/cache/' . $jid . '_email.png');
}
$draw = new ImagickDraw();
$draw->setFontSize(13);
$draw->setGravity(Imagick::GRAVITY_CENTER);
$canvas = new Imagick();
$metrics = $canvas->queryFontMetrics($draw, $email);
$canvas->newImage($metrics['textWidth'], $metrics['textHeight'], "transparent", "png");
$canvas->annotateImage($draw, 0, 0, 0, $email);
$canvas->setImageFormat('PNG');
$canvas->writeImage($cachefile);
}
示例2: wordWrapAnnotation
function wordWrapAnnotation(\Imagick $imagick, $draw, $text, $maxWidth)
{
$words = explode(" ", $text);
$lines = array();
$i = 0;
$lineHeight = 0;
while ($i < count($words)) {
$currentLine = $words[$i];
if ($i + 1 >= count($words)) {
$lines[] = $currentLine;
break;
}
//Check to see if we can add another word to this line
$metrics = $imagick->queryFontMetrics($draw, $currentLine . ' ' . $words[$i + 1]);
while ($metrics['textWidth'] <= $maxWidth) {
//If so, do it and keep doing it!
$currentLine .= ' ' . $words[++$i];
if ($i + 1 >= count($words)) {
break;
}
$metrics = $imagick->queryFontMetrics($draw, $currentLine . ' ' . $words[$i + 1]);
}
//We can't add the next word to this line, so loop to the next line
$lines[] = $currentLine;
$i++;
//Finally, update line height
if ($metrics['textHeight'] > $lineHeight) {
$lineHeight = $metrics['textHeight'];
}
}
return array($lines, $lineHeight);
}
示例3: box
/**
* (non-PHPdoc)
* @see Imagine\Image\FontInterface::box()
*/
public function box($string, $angle = 0)
{
$text = new \ImagickDraw();
$text->setFont($this->file);
$text->setFontSize($this->size);
$info = $this->imagick->queryFontMetrics($text, $string);
$box = new Box($info['textWidth'], $info['textHeight']);
return $box;
}
示例4: text
/**
* (non-PHPdoc)
* @see Imagine\Draw\DrawerInterface::text()
*/
public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
{
try {
$pixel = $this->getColor($font->getColor());
$text = new \ImagickDraw();
$text->setFont($font->getFile());
$text->setFontSize($font->getSize());
$text->setFillColor($pixel);
$text->setTextAntialias(true);
$info = $this->imagick->queryFontMetrics($text, $string);
$rad = deg2rad($angle);
$cos = cos($rad);
$sin = sin($rad);
$x1 = round(0 * $cos - 0 * $sin);
$x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin);
$y1 = round(0 * $sin + 0 * $cos);
$y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos);
$xdiff = 0 - min($x1, $x2);
$ydiff = 0 - min($y1, $y2);
$this->imagick->annotateImage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
$pixel->clear();
$pixel->destroy();
$text->clear();
$text->destroy();
} catch (\ImagickException $e) {
throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
}
}
示例5: calculateTextSize
/**
* Calculates size for bounding box of string written in given font.
*
* @param string $string
* @param FontInterface $font
*
* @return bool|Box Instance of Box object, false on error
*/
public static function calculateTextSize($string, FontInterface $font)
{
$imagine = Tygh::$app['image'];
if ($imagine instanceof \Imagine\Imagick\Imagine && class_exists('ImagickDraw')) {
$text = new \ImagickDraw();
$text->setFont($font->getFile());
if (version_compare(phpversion("imagick"), "3.0.2", ">=")) {
$text->setResolution(96, 96);
$text->setFontSize($font->getSize());
} else {
$text->setFontSize((int) ($font->getSize() * (96 / 72)));
}
$imagick = new \Imagick();
$info = $imagick->queryFontMetrics($text, $string);
$text->clear();
$text->destroy();
$imagick->clear();
$imagick->destroy();
return new Box($info['textWidth'], $info['textHeight']);
}
if ($imagine instanceof \Imagine\Gd\Imagine && function_exists('imagettfbbox')) {
$ttfbbox = imagettfbbox($font->getSize(), 0, $font->getFile(), $string);
return new Box(abs($ttfbbox[2]), abs($ttfbbox[7]));
}
return false;
}
示例6: writeText
function writeText($text)
{
if ($this->printer == null) {
throw new LogicException("Not attached to a printer.");
}
if ($text == null) {
return;
}
$text = trim($text, "\n");
/* Create Imagick objects */
$image = new \Imagick();
$draw = new \ImagickDraw();
$color = new \ImagickPixel('#000000');
$background = new \ImagickPixel('white');
/* Create annotation */
//$draw -> setFont('Arial');// (not necessary?)
$draw->setFontSize(24);
// Size 21 looks good for FONT B
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$metrics = $image->queryFontMetrics($draw, $text);
$draw->annotation(0, $metrics['ascender'], $text);
/* Create image & draw annotation on it */
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
//$image -> writeImage("test.png");
/* Save image */
$escposImage = new EscposImage();
$escposImage->readImageFromImagick($image);
$size = Printer::IMG_DEFAULT;
$this->printer->bitImage($escposImage, $size);
}
示例7: makeImageOfCertification
public function makeImageOfCertification()
{
date_default_timezone_set('UTC');
$timeStamp = date('jS F Y');
$text = "CERTIFIED COPY" . "\n" . $this->serial . "\n" . $timeStamp;
$image = new \Imagick();
$draw = new \ImagickDraw();
$color = new \ImagickPixel('#000000');
$background = new \ImagickPixel("rgb(85, 196, 241)");
$draw->setFont($this->container->getParameter('assetic.write_to') . $this->container->get('templating.helper.assets')->getUrl('fonts/futura.ttf'));
$draw->setFontSize(24);
$draw->setFillColor($color);
$draw->setTextAntialias(true);
$draw->setStrokeAntialias(true);
//Align text to the center of the background
$draw->setTextAlignment(\Imagick::ALIGN_CENTER);
//Get information of annotation image
$metrics = $image->queryFontMetrics($draw, $text);
//Calc the distance(pixels) to move the sentences
$move = $metrics['textWidth'] / 2;
$draw->annotation($move, $metrics['ascender'], $text);
//Create an image of certification
$image->newImage($metrics['textWidth'], $metrics['textHeight'], $background);
$image->setImageFormat('png');
$image->drawImage($draw);
//Save an image temporary
$image->writeImage("cert_" . $this->serial . "_.png");
}
示例8: text_metrics
public function text_metrics($text, $size, $font_file)
{
$draw = new $this->draw_class();
$draw->setFont($font_file);
$draw->setFontSize($size);
$metrics = $this->image->queryFontMetrics($draw, $text, true);
return array('ascender' => floor($metrics['boundingBox']['y2']), 'descender' => floor(-$metrics['boundingBox']['y1']), 'width' => floor($metrics['textWidth']), 'height' => floor($metrics['boundingBox']['y2'] - $metrics['boundingBox']['y1']));
}
示例9: textFitsImage
/**
* @see \wcf\system\image\adapter\IImageAdapter::textFitsImage()
*/
public function textFitsImage($text, $margin, $font, $size)
{
$draw = new \ImagickDraw();
$draw->setFont($font);
$draw->setFontSize($size);
$metrics = $this->imagick->queryFontMetrics($draw, $text);
return $metrics['textWidth'] + 2 * $margin <= $this->getWidth() && $metrics['textHeight'] + 2 * $margin <= $this->getHeight();
}
示例10: textBoundingBox
public function textBoundingBox($size, $font, $text)
{
$im = new Imagick();
$draw = new ImagickDraw();
$draw->setFontSize($size * $this->fontSizeScale);
$draw->setFont($font);
$fontMetrics = $im->queryFontMetrics($draw, $text);
return array($fontMetrics['textWidth'], $fontMetrics['textHeight'] * $this->lineHeightScale, $fontMetrics['ascender'] * $this->lineHeightScale);
}
示例11: box
/**
* {@inheritdoc}
*/
public function box($string, $angle = 0)
{
$text = new \ImagickDraw();
$text->setFont($this->file);
/**
* @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027
*
* ensure font resolution is the same as GD's hard-coded 96
*/
if (version_compare(phpversion("imagick"), "3.0.2", ">=")) {
$text->setResolution(96, 96);
$text->setFontSize($this->size);
} else {
$text->setFontSize((int) ($this->size * (96 / 72)));
}
$info = $this->imagick->queryFontMetrics($text, $string);
$box = new Box($info['textWidth'], $info['textHeight']);
return $box;
}
示例12: render
function render()
{
$text = "Lorem ipsum";
$im = new \Imagick();
$draw = new \ImagickDraw();
$draw->setStrokeColor("none");
$draw->setFont("../fonts/Arial.ttf");
$draw->setFontSize(96);
$draw->setTextAlignment(\Imagick::ALIGN_LEFT);
$metrics = $im->queryFontMetrics($draw, $text);
return print_table($metrics);
}
示例13: generate
/**
* Generates and dies with an image containing the heading and the text of the error.
*
* @param string $headingText The heading of the error.
* @param string $errorText The text of the error.
*/
public function generate($headingText, $errorText)
{
$draw = new ImagickDraw();
$draw->setFillColor('#777777');
$draw->setFontSize(15);
$draw->setFont('fonts/exo2bold.ttf');
$headingMetrics = $this->canvas->queryFontMetrics($draw, $headingText);
$draw->setFont('fonts/exo2regular.ttf');
$textMetrics = $this->canvas->queryFontMetrics($draw, $errorText);
$this->canvas->newImage(max($textMetrics['textWidth'], $headingMetrics['textWidth']) + 6, $textMetrics['textHeight'] + $headingMetrics['textHeight'] + 6, new ImagickPixel('white'));
$this->canvas->annotateImage($draw, 3, $headingMetrics['textHeight'] * 2, 0, $errorText);
$draw->setFont('fonts/exo2bold.ttf');
$draw->setFillColor('#333333');
$draw->setGravity(Imagick::GRAVITY_NORTH);
$this->canvas->annotateImage($draw, 3, 3, 0, $headingText);
$this->canvas->setImageFormat('png');
header('Content-Type: image/' . $this->canvas->getImageFormat());
header("Cache-Control: max-age=60");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 60) . " GMT");
die($this->canvas);
}
示例14: wrapText
/**
* Internal
*
* Fits a string into box with given width
*/
private function wrapText($string, $text, $angle, $width)
{
$result = '';
$words = explode(' ', $string);
foreach ($words as $word) {
$teststring = $result . ' ' . $word;
$testbox = $this->imagick->queryFontMetrics($text, $teststring, true);
if ($testbox['textWidth'] > $width) {
$result .= ($result == '' ? '' : "\n") . $word;
} else {
$result .= ($result == '' ? '' : ' ') . $word;
}
}
return $result;
}
示例15: copyrightImage
/**
* Generate and save the image with a copyright text at the bottom right corner
*
* @param string $text The copyright text
* @param int $fontSize OPTIONAL the copyright font size DEFAULT 22
* @param string $font OPTIONAL the copyright font DEFAULT "Verdana"
* @param string $position OPTIONAL the position to put copyright text DEFAULT "bottom-right" Possibles
* Values ("bottom-right", "bottom-left", "top-right", "top-left")
*/
public function copyrightImage(string $text, int $fontSize = 22, string $font = 'Verdana', string $position = 'bottom-right')
{
$draw = new \ImagickDraw();
$draw->setFontSize($fontSize);
$draw->setFont($font);
$draw->setFillColor('#ffffff');
$draw->setTextUnderColor('#00000088');
$textMetrics = $this->image->queryFontMetrics($draw, $text);
$textWidth = $textMetrics['textWidth'] + 2 * $textMetrics['boundingBox']['x1'];
$extraTextHeight = $textMetrics['descender'];
$textHeight = $textMetrics['textHeight'] + $extraTextHeight;
switch ($position) {
case 'bottom-right':
$width = $this->image->getImageWidth() - $textWidth;
$height = $this->image->getImageHeight() + $extraTextHeight;
$width -= static::$EXTRA_TEXT_PADDING;
$height -= static::$EXTRA_TEXT_PADDING;
break;
case 'bottom-left':
$width = 0;
$height = $this->image->getImageHeight() + $extraTextHeight;
$width += static::$EXTRA_TEXT_PADDING;
$height -= static::$EXTRA_TEXT_PADDING;
break;
case 'top-right':
$width = $this->image->getImageWidth() - $textWidth;
$height = $textHeight;
$width -= static::$EXTRA_TEXT_PADDING;
$height += static::$EXTRA_TEXT_PADDING;
break;
case 'top-left':
$width = 0;
$height = $textHeight;
$width += static::$EXTRA_TEXT_PADDING;
$height += static::$EXTRA_TEXT_PADDING;
break;
default:
$width = $this->image->getImageWidth() - $textWidth;
$height = $this->image->getImageHeight() + $extraTextHeight;
$width -= static::$EXTRA_TEXT_PADDING;
$height -= static::$EXTRA_TEXT_PADDING;
break;
}
$this->image->annotateImage($draw, $width, $height, 0, $text);
$this->image->writeImage($this->imageSavePath . DIRECTORY_SEPARATOR . $this->imageName . '_copyright' . '.' . $this->imageExtension);
}