【问题标题】:Make imagettftext recognice HEX colors使 imagettftext 识别 HEX 颜色
【发布时间】:2012-07-10 18:54:02
【问题描述】:

简单的问题,我在那里搜索并没有找到方法。

我有一个字符串(变量),可以是这样的:

#FF9900Hel#FFFFFFlo

我需要一种方法让 imagettftext 函数识别这些颜色并使用这些颜色绘制文本。例如,在我之前提到的示例中,文本应该是:红色:Hel 和白色:lo。我希望我能很好地解释我的意思。

提前致谢

【问题讨论】:

  • imagettftext 中绝对没有任何东西可以做到这一点。您必须自己执行此操作,将其作为具有两种不同颜色的两个单独的文本部分放入图像中,并通过一些数学对齐它们。
  • 我明白这一点。我是新来的。是否有任何函数可以将这些 HEX 颜色转换为可用于 imagettftext 的实际变量,或者我也应该为此创建自己的函数?
  • 如果您理解这一点,那么您的问题的措辞就会大不相同。最好描述一下您在实施上述流程时遇到的具体问题。
  • 好吧,如果我问这个问题是因为也许有比制作我自己的函数更简单的方法,因为我怀疑我是否能够以我的知识水平做到这一点。现在我知道没有更简单的方法我会尝试为此创建一个函数。

标签: php hex gd


【解决方案1】:

您不仅需要识别颜色,还需要对齐字符串。这不是自动的,因为它在 HTML 中。

我会首先将字符串拆分为其组成部分:

// Split the text using hex colors as strings:
$hex     = '[0-9a-fA-F]'; // Capital hex should be supported

$colorex = "(#{$hex}{$hex}{$hex}{$hex}{$hex}{$hex})";
// or also $colorex = "(#{$hex}{6})";

$parts = preg_split ("/{$colorex}/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);

// Then you would iterate through the parts:

$color = imageColorAllocate($gd, 0, 0, 0); // Default is black
foreach($parts as $part)
{
    // Scan the hex value
    if (preg_match("/^{$colorex}\$/", $part, $gregs))
    {
        sscanf($gregs[1], "#%02x%02x%02x", &$r, &$g, &$b);
        $color = imageColorAllocate($gd, $r, $g, $b);
        continue;
    }
    // IMPROVEMENT: if count(explode("\n", $part)) > 1, $y += $height, $x = 0
    // to indicate "go to next line". So this would appear on two lines:
    //     #012345Hel#ff7700lo,
    //     #443212World 
    // Next section will be repeated for each item
    // in the exploded string.
    //! $subparts = explode("\n", $part);
    //! foreach($subparts as $part)
    //! { // We have overwritten $part

    // Here $part is printed as string at position $x, $y
    // Ask GD to calculate string width
    // with http://php.net/manual/en/function.imagettfbbox.php
    // Calculations with $angle != 0 is a bit more difficult, entails trigonometric
    // evaluation of $w and $h.
    $bbox = imagettfbbox ($size, $angle, $fontfile, $part);
    $w    = $bbox[4] - $bbox[0];  // 0,1 is lower left corner
    $h    = $bbox[5] - $bbox[1];  // 4,5 is upper right
    imagettftext ($gd, $size, $angle, $x, $y, $color, $fontfile, $part);
    // Increment $x position by $w
    $x += $w;

    //!     if (count($subparts) > 1)
    //!     {
    //!         $x = 0; $y += $h;
    //!     }
    //! }
}

【讨论】:

    【解决方案2】:

    您将不得不解析颜色和相应的字符串,为每种唯一颜色分配 GD 颜色资源,并根据需要单独调用 imagettftext 调整您的 xy 坐标。

    imagettxtext 函数不能也不会为您执行此操作。

    查看imageftbbox,因为您将需要此函数来计算每个文本片段的边界框,这是正确放置下一个不同颜色的文本块所必需的。

    这是一个将 HTML 颜色转换为十六进制三元组的函数,您可以将其传递给 imagecolorallocate

    function htmlColorToHex($color) {
        if (substr($color, 0, 1) == '#') {
            $color = substr($color, 1);
        }
    
        if (strlen($color) == 3) {
            $red   = str_repeat(substr($color, 0, 1), 2);
            $green = str_repeat(substr($color, 1, 1), 2);
            $blue  = str_repeat(substr($color, 2, 1), 2);
        } else {
            $red   = substr($color, 0, 2);
            $green = substr($color, 2, 2);
            $blue  = substr($color, 4, 2);
        }
    
        $hex = array('r' => hexdec($red),
                     'g' => hexdec($green),
                     'b' => hexdec($blue));
    
        return $hex;
    }
    

    您要做的最复杂的部分是正确计算文本每个部分的坐标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 2019-02-08
      • 2021-04-28
      • 2013-01-25
      • 1970-01-01
      • 2020-11-21
      • 2015-08-10
      相关资源
      最近更新 更多