当前位置: 首页>>代码示例>>PHP>>正文


PHP resource::queryFontMetrics方法代码示例

本文整理汇总了PHP中resource::queryFontMetrics方法的典型用法代码示例。如果您正苦于以下问题:PHP resource::queryFontMetrics方法的具体用法?PHP resource::queryFontMetrics怎么用?PHP resource::queryFontMetrics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在resource的用法示例。


在下文中一共展示了resource::queryFontMetrics方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: text

 /**
  * 图像添加文字
  * @param  string  $text   添加的文字
  * @param  string  $font   字体路径
  * @param  integer $size   字号
  * @param  string  $color  文字颜色
  * @param  integer $locate 文字写入位置
  * @param  integer $offset 文字相对当前位置的偏移量
  * @param  integer $angle  文字倾斜角度
  */
 public function text($text, $font, $size, $color = '#00000000', $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0)
 {
     //资源检测
     if (empty($this->img)) {
         throw new Exception('没有可以被写入文字的图像资源');
     }
     if (!is_file($font)) {
         throw new Exception("不存在的字体文件:{$font}");
     }
     //获取颜色和透明度
     if (is_array($color)) {
         $color = array_map('dechex', $color);
         foreach ($color as &$value) {
             $value = str_pad($value, 2, '0', STR_PAD_LEFT);
         }
         $color = '#' . implode('', $color);
     } elseif (!is_string($color) || 0 !== strpos($color, '#')) {
         throw new Exception('错误的颜色值');
     }
     $col = substr($color, 0, 7);
     $alp = strlen($color) == 9 ? substr($color, -2) : 0;
     //获取文字信息
     $draw = new ImagickDraw();
     $draw->setFont(realpath($font));
     $draw->setFontSize($size);
     $draw->setFillColor($col);
     $draw->setFillAlpha(1 - hexdec($alp) / 127);
     $draw->setTextAntialias(true);
     $draw->setStrokeAntialias(true);
     $metrics = $this->img->queryFontMetrics($draw, $text);
     /* 计算文字初始坐标和尺寸 */
     $x = 0;
     $y = $metrics['ascender'];
     $w = $metrics['textWidth'];
     $h = $metrics['textHeight'];
     /* 设定文字位置 */
     switch ($locate) {
         /* 右下角文字 */
         case Image::IMAGE_WATER_SOUTHEAST:
             $x += $this->info['width'] - $w;
             $y += $this->info['height'] - $h;
             break;
             /* 左下角文字 */
         /* 左下角文字 */
         case Image::IMAGE_WATER_SOUTHWEST:
             $y += $this->info['height'] - $h;
             break;
             /* 左上角文字 */
         /* 左上角文字 */
         case Image::IMAGE_WATER_NORTHWEST:
             // 起始坐标即为左上角坐标,无需调整
             break;
             /* 右上角文字 */
         /* 右上角文字 */
         case Image::IMAGE_WATER_NORTHEAST:
             $x += $this->info['width'] - $w;
             break;
             /* 居中文字 */
         /* 居中文字 */
         case Image::IMAGE_WATER_CENTER:
             $x += ($this->info['width'] - $w) / 2;
             $y += ($this->info['height'] - $h) / 2;
             break;
             /* 下居中文字 */
         /* 下居中文字 */
         case Image::IMAGE_WATER_SOUTH:
             $x += ($this->info['width'] - $w) / 2;
             $y += $this->info['height'] - $h;
             break;
             /* 右居中文字 */
         /* 右居中文字 */
         case Image::IMAGE_WATER_EAST:
             $x += $this->info['width'] - $w;
             $y += ($this->info['height'] - $h) / 2;
             break;
             /* 上居中文字 */
         /* 上居中文字 */
         case Image::IMAGE_WATER_NORTH:
             $x += ($this->info['width'] - $w) / 2;
             break;
             /* 左居中文字 */
         /* 左居中文字 */
         case Image::IMAGE_WATER_WEST:
             $y += ($this->info['height'] - $h) / 2;
             break;
         default:
             /* 自定义文字坐标 */
             if (is_array($locate)) {
                 list($posx, $posy) = $locate;
                 $x += $posx;
//.........这里部分代码省略.........
开发者ID:a3147972,项目名称:wswl,代码行数:101,代码来源:Imagick.class.php

示例2: imagickDrawText

 /**
  * 在图片上添加验证文字
  * @param  resource $image         图片对象
  * @param  string   $text          要添加的字符
  * @return resource 图片对象
  */
 protected function imagickDrawText($image, $text)
 {
     $draw = new \ImagickDraw();
     $draw->setFont($this->font);
     $draw->setFontSize($this->height * 0.8);
     $draw->setFillColor(new \ImagickPixel('#333333'));
     $draw->setStrokeAntialias(true);
     $draw->setTextAntialias(true);
     $metrics = $image->queryFontMetrics($draw, $text);
     $draw->annotation(0, $metrics['ascender'], $text);
     $image->drawImage($draw);
     $draw->destroy();
     return $image;
 }
开发者ID:feixingbeibao,项目名称:vitexframework,代码行数:20,代码来源:Captcha.php

示例3: getTextGeometry

 /**
  * {@inheritdoc}
  */
 public function getTextGeometry($text, Drawer $drawer)
 {
     $metrics = $this->resource->queryFontMetrics($this->getImagickDraw($drawer), $text);
     return array('width' => $metrics['textWidth'], 'height' => $metrics['textHeight']);
 }
开发者ID:basekit,项目名称:imanee,代码行数:8,代码来源:ImagickResource.php

示例4: imageAddText

 /**
  * Add watermark text to image
  *
  * @param resource $image
  * @param array $opt
  * @return resource
  */
 private function imageAddText($image, $color, $text)
 {
     if (osc_use_imagick()) {
         $draw = new ImagickDraw();
         $draw->setFillColor('black');
         //$color);
         $draw->setFont($this->font);
         $draw->setFontSize(30);
         $metrics = $image->queryFontMetrics($draw, $text);
         $geometry = $image->getImageGeometry();
         switch (osc_watermark_place()) {
             case 'tl':
                 $offset['x'] = 1;
                 $offset['y'] = $metrics['ascender'] + 1;
                 break;
             case 'tr':
                 $offset['x'] = $geometry['width'] - $metrics['textWidth'] - 1;
                 $offset['y'] = $metrics['ascender'] + 1;
                 break;
             case 'bl':
                 $offset['x'] = 1;
                 $offset['y'] = $geometry['height'] - 1;
                 break;
             case 'br':
                 $offset['x'] = $geometry['width'] - $metrics['textWidth'] - 1;
                 $offset['y'] = $geometry['height'] - 1;
                 break;
             default:
                 $offset['x'] = $geometry['width'] / 2 - $metrics['textWidth'] / 2;
                 $offset['y'] = $geometry['height'] / 2 - $metrics['ascender'] / 2;
                 break;
         }
         $image->annotateImage($draw, $offset['x'], $offset['y'], 0, $text);
         $image->setImageFormat('jpg');
     } else {
         // allocate text color
         $color = $this->imageColorAllocateHex($image, $color);
         // calculate watermark position and get full path to font file
         $offset = $this->calculateOffset($image, $text);
         // Add the text to image
         imagettftext($image, 20, 0, $offset['x'], $offset['y'], $color, $this->font, html_entity_decode($text, null, "UTF-8"));
     }
     return $image;
 }
开发者ID:nsswaga,项目名称:OSClass,代码行数:51,代码来源:Watermark.php

示例5: text

 public function text($text, $font, $size, $color = "#00000000", $locate = THINKIMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0)
 {
     if (empty($this->img)) {
         throw new Exception("没有可以被写入文字的图像资源");
     }
     if (!is_file($font)) {
         throw new Exception("不存在的字体文件:{$font}");
     }
     if (is_array($color)) {
         $color = array_map("dechex", $color);
         foreach ($color as &$value) {
             $value = str_pad($value, 2, "0", STR_PAD_LEFT);
         }
         $color = "#" . implode("", $color);
     } else {
         if (!is_string($color) || 0 !== strpos($color, "#")) {
             throw new Exception("错误的颜色值");
         }
     }
     $col = substr($color, 0, 7);
     $alp = strlen($color) == 9 ? substr($color, -2) : 0;
     $draw = new ImagickDraw();
     $draw->setFont(realpath($font));
     $draw->setFontSize($size);
     $draw->setFillColor($col);
     $draw->setFillAlpha(1 - hexdec($alp) / 127);
     $draw->setTextAntialias(true);
     $draw->setStrokeAntialias(true);
     $metrics = $this->img->queryFontMetrics($draw, $text);
     $x = 0;
     $y = $metrics["ascender"];
     $w = $metrics["textWidth"];
     $h = $metrics["textHeight"];
     switch ($locate) {
         case THINKIMAGE_WATER_SOUTHEAST:
             $x += $this->info["width"] - $w;
             $y += $this->info["height"] - $h;
             break;
         case THINKIMAGE_WATER_SOUTHWEST:
             $y += $this->info["height"] - $h;
             break;
         case THINKIMAGE_WATER_NORTHWEST:
             break;
         case THINKIMAGE_WATER_NORTHEAST:
             $x += $this->info["width"] - $w;
             break;
         case THINKIMAGE_WATER_CENTER:
             $x += ($this->info["width"] - $w) / 2;
             $y += ($this->info["height"] - $h) / 2;
             break;
         case THINKIMAGE_WATER_SOUTH:
             $x += ($this->info["width"] - $w) / 2;
             $y += $this->info["height"] - $h;
             break;
         case THINKIMAGE_WATER_EAST:
             $x += $this->info["width"] - $w;
             $y += ($this->info["height"] - $h) / 2;
             break;
         case THINKIMAGE_WATER_NORTH:
             $x += ($this->info["width"] - $w) / 2;
             break;
         case THINKIMAGE_WATER_WEST:
             $y += ($this->info["height"] - $h) / 2;
             break;
         default:
             if (is_array($locate)) {
                 $posy = $locate[1];
                 $posx = $locate[0];
                 $x += $posx;
                 $y += $posy;
             } else {
                 throw new Exception("不支持的文字位置类型");
             }
     }
     if (is_array($offset)) {
         $offset = array_map("intval", $offset);
         $oy = $offset[1];
         $ox = $offset[0];
     } else {
         $offset = intval($offset);
         $ox = $oy = $offset;
     }
     if ("gif" == $this->info["type"]) {
         $img = $this->img->coalesceImages();
         $this->img->destroy();
         do {
             $img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
         } while ($img->nextImage());
         $this->img = $img->deconstructImages();
         $img->destroy();
     } else {
         $this->img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
     }
     $draw->destroy();
 }
开发者ID:AxelPanda,项目名称:ibos,代码行数:95,代码来源:ImageImagick.class.php


注:本文中的resource::queryFontMetrics方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。