PHP:在创建图像之前为超链接添加颜色

PHP:在创建图像之前为超链接添加颜色,php,text,colors,hyperlink,Php,Text,Colors,Hyperlink,我正在获取一个xml提要并从文本中创建一个图像。我想做的是将链接文本的颜色设置为与常规文本不同的颜色。我在文本中循环查找链接,但不知道如何给文本上色 imagetttftext()只能以单色绘制。您不能更改它,也不能嵌入html颜色代码来动态更改文本颜色。您必须将文本拆分为多个块,每个块使用单一颜色绘制 这意味着您必须计算每个字符串块开始/停止使用的位置,并相应地调整imagetttftext()中的坐标 意见跟进: 好的,所以单独的标签内容,带有不同颜色的链接。第一步是对字符串进行预处理,并沿

我正在获取一个xml提要并从文本中创建一个图像。我想做的是将链接文本的颜色设置为与常规文本不同的颜色。我在文本中循环查找链接,但不知道如何给文本上色

imagetttftext()
只能以单色绘制。您不能更改它,也不能嵌入html颜色代码来动态更改文本颜色。您必须将文本拆分为多个块,每个块使用单一颜色绘制

这意味着您必须计算每个字符串块开始/停止使用的位置,并相应地调整
imagetttftext()
中的坐标

意见跟进:

好的,所以单独的标签内容,带有不同颜色的链接。第一步是对字符串进行预处理,并沿链接边界将其拆分,从而得到一系列“text/link/text/link/text”块。之后,它只是一个循环:

$start_x = 5;
$start_y = 20; // initial x/y coords of text
$fontsize = 14;
$font = 'font.ttf';
$angle = 0;

$black = imagecolorallocate($im, 0, 0, 0);
$linkcolor = imagecolorallocate($im, ?, ? ,?);

foreach ($string_chunks as $chunk) {
   // get coordinates of bounding box containing text
   $coords = imagegettfbbox($fontsize, $angle, $font, $chunk);

   $end_x = $coords[4]; // as per imagetttfbbox() doc page
   $end_y = $coords[5]; // x,y coords of top right corner of bounding box

   // figure out which color to draw in
   $color_to_draw = is_link($chunk) ? $linkcolor : $black; 

   // draw the text chunk
   imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);

   // adjust starting coordinates to the END of the just-drawn text
   $start_x += $end_x;
   $start_y += $end_y;
}
如果两者之间没有足够的空间,则可能需要在移动时调整坐标 每个文本块,或在获取其边界框之前在字符串中添加空格。

imagetttftext()
只能以单一颜色绘制。您不能更改它,也不能嵌入html颜色代码来动态更改文本颜色。您必须将文本拆分为多个块,每个块使用单一颜色绘制

这意味着您必须计算每个字符串块开始/停止使用的位置,并相应地调整
imagetttftext()
中的坐标

意见跟进:

好的,所以单独的标签内容,带有不同颜色的链接。第一步是对字符串进行预处理,并沿链接边界将其拆分,从而得到一系列“text/link/text/link/text”块。之后,它只是一个循环:

$start_x = 5;
$start_y = 20; // initial x/y coords of text
$fontsize = 14;
$font = 'font.ttf';
$angle = 0;

$black = imagecolorallocate($im, 0, 0, 0);
$linkcolor = imagecolorallocate($im, ?, ? ,?);

foreach ($string_chunks as $chunk) {
   // get coordinates of bounding box containing text
   $coords = imagegettfbbox($fontsize, $angle, $font, $chunk);

   $end_x = $coords[4]; // as per imagetttfbbox() doc page
   $end_y = $coords[5]; // x,y coords of top right corner of bounding box

   // figure out which color to draw in
   $color_to_draw = is_link($chunk) ? $linkcolor : $black; 

   // draw the text chunk
   imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);

   // adjust starting coordinates to the END of the just-drawn text
   $start_x += $end_x;
   $start_y += $end_y;
}
如果两者之间没有足够的空间,则可能需要在移动时调整坐标 每个文本块,或者在获取其边界框之前在字符串中加一个空格。

我知道有和来获取字体字符的高度和宽度。但是,这只适用于内置字体或加载的字体,我认为它们不支持TTF文件。但是如果你使用这个,计算每个块的坐标就相当容易了

例如:

<?php
// Image:
$image = imagecreatetruecolor(200, 200);

// First built-in font:
$font = 1;

// X and Y coordinates of the 5th character in the 7th row:
$x = imagefontwidth($font) * 4;
$y = imagefontheight($font) * 6;

// Put character onto image, with white color:
imagestring($image, $font, $x, $y, 'H', 0xffffff);
?>

(请注意,尽管我在代码中使用了
0xffffff
作为颜色,但建议尽可能改用
imagecoloralallocate()

我知道有和来获取字体字符的高度和宽度。但是,这只适用于内置字体或加载的字体,我认为它们不支持TTF文件。但是如果你使用这个,计算每个块的坐标就相当容易了

例如:

<?php
// Image:
$image = imagecreatetruecolor(200, 200);

// First built-in font:
$font = 1;

// X and Y coordinates of the 5th character in the 7th row:
$x = imagefontwidth($font) * 4;
$y = imagefontheight($font) * 6;

// Put character onto image, with white color:
imagestring($image, $font, $x, $y, 'H', 0xffffff);
?>


(请注意,尽管我在代码中使用了
0xffffff
作为颜色,但建议尽可能使用
imagecolorallocate()

xml的内容会发生变化,因此如何计算动态位置。或者有没有比imagettftext更好的方法来实现这一点?您是否正在绘制文本XML源代码?或者只是一些单个标记的内容?单个标记的内容。在$coords=imagettfbbox($im、$fontsize、$angle、$font、$chunk)上;第1行出现错误“imagettfbbox()的参数计数错误”。哎呀,该函数不接受
$im
参数(因为它不处理图像,只计算一些文本内容)。如果您删除它,应该可以工作。xml的内容会发生变化,因此我将如何动态计算位置。或者有没有比imagettftext更好的方法来实现这一点?您是否正在绘制文本XML源代码?或者只是一些单个标记的内容?单个标记的内容。在$coords=imagettfbbox($im、$fontsize、$angle、$font、$chunk)上;第1行出现错误“imagettfbbox()的参数计数错误”。哎呀,该函数不接受
$im
参数(因为它不处理图像,只计算一些文本内容)。如果您删除它,应该可以工作。imageloadfont()的问题是它不支持ttf字体,我需要使用ttf字体进行品牌推广。imageloadfont()的问题是它不支持ttf字体,我需要使用ttf字体进行品牌推广。