doulu5109 2015-10-22 18:07
浏览 72
已采纳

如何在PHP中使用Imagick更改特定单词的颜色?

I'm currently using annotateImage to add some text to an image.

/* Black text */
$draw->setFillColor('black');

/* Font properties */
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );

$image->annotateImage($draw, 10, 45, 0, 'The fox jumped over the lazy dog');

This currently displays all the text in black. I'm looking for a way to change the color of the word fox to red, but I'm out of ideas.

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • drrkgbm6851 2015-10-27 17:31
    关注

    As suggested by Danack's comment, here's my solution if anyone needs it. The parseText function looks for <span> tags with the attributes, color, font, size and returns an array.

    The <span color="red">fox</span> jumped over the lazy <span color="green">dog</span>.
    

    In the above text, the word fox will have a color=red attribute, and dog will have color=green.

    Using the array, we can draw the regular text, track the positioning and draw the stylized texts as well.

    function parseText($txt) {
      if ( strstr($txt, '<span') ) {
        $txt = preg_split('@(<span(?:\\\?.)*?>(?:\\\?.)*?</span>)@', $txt, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        $arr = array();
        foreach ( $txt as $t ) {
          if ( strstr($t, '<span') ) {
            $tag = array();
            preg_match('@<span\s*((?:\\\?.)*?)\s*>((?:\\\?.)*?)</span>@', $t, $m);
            $attrs = $m[1];
            $inner = $m[2];
            if ( $attrs ) {
              $attrs = preg_split('@((?:\\\?.)*?=["\'](?:\\\?.)*?["\'])\s*@', $attrs, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
              foreach ( $attrs as $a ) {
                $a = explode('=', $a);
                $tag[ $a[0] ] = trim($a[1], '"\'');
              }
            }
            $tag['text'] = $inner;
            $arr[] = $tag;
          } else {
            $arr[] = array( 'text' => $t );
          }
          $txt = $arr;
        }
      }
      return $txt;
    }
    
    function newStyle($image, $draw, $t, &$width) {
      if ( $t['color'] )  $draw->setFillColor($t['color']);
      if ( $t['size']  )  $draw->setFontSize($t['size']);
      if ( $t['font']  )  $draw->setFont($t['font']);
      $metrics = $image->queryFontMetrics($draw, $t['text'], FALSE);
      $width = $metrics['textWidth'];
    }
    
    // To use it
    
    function defaultStyle($draw) {
      $draw->setFillColor('black');
      $draw->setFont('Bookman-DemiItalic');
      $draw->setFontSize(20);
    }
    
    defaultStyle($draw);
    
    $txt = 'The <span color="red">fox</span> jumped over the lazy <span color="green">dog</span>.';
    $x = 45;
    $y = 45;
    $txt = parseText($txt);
    if ( is_array($txt) ) {
      foreach ( $txt as $t ) {
        defaultStyle($draw);
        newStyle($image, $draw, $t, $width);
        $image->annotateImage($draw, $x, $y, 0, $t['text']);
        $x += $width;
      }
    } else {
      $image->annotateImage($draw, $x, $y, 0, $txt);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程