【问题标题】:PHP to ouput html code for text gradient or "rainbow text" of sortsPHP 为文本渐变或“彩虹文本”输出 html 代码
【发布时间】:2015-07-02 14:26:34
【问题描述】:

首先让我说我整天在互联网上寻找解决方案,但我只是难住了。我设法找到足够的代码 sn-ps 来组合我需要的“几乎”工作版本 - 但老实说,我只是迷失了如何让它工作。

这是我想要做的:

我正在尝试制作一个 php 函数,该函数将采用 2 种或 3 种颜色,并将它们作为平滑渐变应用于文本字符串。 我需要该函数来输出渐变的实际 HTML 代码。我设想它的工作方式是:它将获取消息字符串并将其拆分为单个字符,然后以这样一种方式为每个字符着色,当它与 html 输出一起显示时,它看起来就像从一种颜色平滑淡入到下一个。现在我正在使用我刚刚在其中定义的 2 种颜色(FF0000 和 0000FF)测试该函数。我似乎无法让它为整个字符串着色。它似乎抓住了第一个字母,并进行了部分转换,然后就停止了。

这是我试图让它看起来像的屏幕截图:

这是我的屏幕截图(包括用于解释的 html 输出)

这是我正在使用的代码:

<?php 
function html2rgb($color)
{
    if ($color[0] == '#')
        $color = substr($color, 1);

    if (strlen($color) == 6)
        list($r, $g, $b) = array($color[0].$color[1],
                                 $color[2].$color[3],
                                 $color[4].$color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return false;

    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

    return array($r, $g, $b);
}
function rgb2html($r, $g=-1, $b=-1)
{
    if (is_array($r) && sizeof($r) == 3)
        list($r, $g, $b) = $r;

    $r = intval($r); $g = intval($g);
    $b = intval($b);

    $r = dechex($r<0?0:($r>255?255:$r));
    $g = dechex($g<0?0:($g>255?255:$g));
    $b = dechex($b<0?0:($b>255?255:$b));

    $color = (strlen($r) < 2?'0':'').$r;
    $color .= (strlen($g) < 2?'0':'').$g;
    $color .= (strlen($b) < 2?'0':'').$b;
    return '#'.$color;
}
    echo "<h1>Result:</h1>";
    $src_color = html2rgb('FF0000');
    $dst_color = html2rgb('0000FF');
    print_r($dst_color);

    for($i=0; $i<3; $i++)
        $step_color[$i] = ( $dst_color[$i] - $src_color[$i] ) / 30.30;
    // step_color array contains difference between adjacent color stripes

    $html_out = ''; // html code container

    for($j=0; $j<60; $j++)
    {
    // generate color stripe code

        $message = 'I am trying to make this text string fade from one color to another';
        $counter = strlen($message);
        $array = str_split($message);
        $mycount = 0;


        if($mycount < $counter){
            $line = '<b><font color=" '.rgb2html($src_color).';">'.$array[$mycount].'</font></b>';
        $html_out .= "{$line}"; // save color stripe to display HTML code later
        $mycount = $mycount + 1;
        }
        echo $line; // output color stripe to browser

        for($i=0; $i<1; $i++) // incrementing each color component
            $src_color[$i] += $step_color[$i];
    }

?>
<h1>HTML Code:</h1>
<pre><?php
// output HTML code replacing < and > with &lt; and &gt;
$stuff = strtr($html_out, array('&' => '&amp;',
                            '<' => '&lt;',
                            '>'=> '&gt;'));
echo $stuff;

我对这种事情还很陌生,所以如果我的代码“落后”或很糟糕,请不要对我太残忍。谁能指出我正确的方向?我只是不知道如何让它做我想做的事。

非常感谢您抽出宝贵时间阅读本文以及您可以提供的任何建议!

编辑:底部屏幕截图的图片链接,使其更易于查看 http://i.stack.imgur.com/vsfdQ.jpg

更新——好的,我已经重写了大部分函数,​​并且几乎可以正常工作了。我现在遇到的问题是它一遍又一遍地重复整个字符串。它正在应用淡入淡出,但不是它应该的方式。我需要它在字符串中从一种颜色褪色到另一种颜色。这是它现在正在做什么的新屏幕截图: 这是该链接,因此您可以更轻松地查看它: http://i.stack.imgur.com/X0Pmq.jpg

这是我正在使用的新代码:

<?php
function rgb($rgb) {
    $ret = '';
    foreach ($rgb as $x) {
        // Make sure the RGB values are 0-255...
        $x = max(0, min(255, $x));
        // create a 2 digit hex value for this color component...
        $ret .= ($x < 16 ? '0'.dechex($x) : dechex($x));
    }
    return '#'.$ret;
}

// Returns a fade of $from into $to by $amount ($from and $to are RGB arrays)...
function fade($from, $to, $amount) {
    $rgb = array();
    // Go through the RGB values and adjust the values by $amount...
    for ($i = 0; $i <= 2; $i++) {
        $rgb[$i] = (($to[$i] - $from[$i]) * $amount) + $from[$i];
    }
    return $rgb;
}
$string = 'testing out the fade function here';
//$string1 = strlen($string);
for ($j = 0; $j < 1; $j += .01) {
    $color = rgb(fade(array(255,0,0), array(0,255,0), $j));

    for($i=0;$i<strlen($string);$i++){
echo "<font style='color:$color'>$string[$i]</font>";
}

}

有谁能告诉我如何让它只打印一次字符串,并将淡入淡出正确应用于字符串?

非常感谢你们所有的时间和专业知识!

【问题讨论】:

标签: php html hex rgb fade


【解决方案1】:

检查第二个示例,因为它更多的是您要查找的内容。

只需使用 php 添加 html 元素及其 id 或类,然后使用 css 给出渐变。

例子:

#id_of_element {      /*or class of element*/
    background: -webkit-linear-gradient(left, red , blue);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;

    /* the following would cover other browsers...not sure about IE */
    background: -webkit-linear-gradient(left, red , blue); /* For Safari 5.1 to 6.0 */
    background: -o-linear-gradient(right, red, blue); /* For Opera 11.1 to 12.0 */
    background: -moz-linear-gradient(right, red, blue); /* For Firefox 3.6 to 15 */
    background: linear-gradient(to right, red , blue); /* Standard syntax */
    /* then just add the -o- or -moz- etc. */
}

取决于您想要渐变的角度或方向 -> 只需使用 php(和/或 javascript)来更改 background: -webkit-linear-gradient(direction, color1 , color2); 的值

以下是代码示例

试试下面的代码作为例子:

然后,在网络浏览器中打开该页面。它应该有从黑到白的文本。

将其附加到网址后:

?color1=FFFFFF&color2=000000

所以完整的网址应该是这样的:

http://yoursite.com/pageName.php?color1=FFFFFF&color2=000000

现在渐变反转了。因为 color1 最初是从 #000000 开始的,但是 php 由于它从 GET 请求中获得的值而切换了它。

下面是代码示例:

<?php 
    $textOutput = '';
?>
<?php if(isset($_GET['color1']) && isset($_GET['color2'])):
    $textOutput = '';
    $userFirstInput  = $_GET['color1'];  // these are the posts or gets you send from your form
    $userSecondInput = $_GET['color2']; // these are the posts or gets you send from your form

    $firstColor = $userFirstInput;    // #FFFFFF for example
    $secondColor = $userSecondInput;  // #000000 for example

    $textOutput .= '.spans{';
    $textOutput .= 'background: -webkit-linear-gradient(left, #'. $firstColor . ', #'.$secondColor .');';
    $textOutput .= '-webkit-background-clip: text;';
    $textOutput .= '-webkit-text-fill-color: transparent;';
?>
<?php else:
    $textOutput = '';
    $textOutput .= '.spans{';
    $textOutput .= 'background: -webkit-linear-gradient(left, #000000 , #FFFFFF);';
    $textOutput .= '-webkit-background-clip: text;';
    $textOutput .= '-webkit-text-fill-color: transparent;';
?>
<?php endif ?>
<!DOCTYPE html>
<html>
<head>
<style>
    <?php echo $textOutput; ?>
</style>
<span class="spans">IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII</span>
</body>
</html>

如果您在获取用户输入方面需要帮助,我也可以提供帮助。我使用 ajax 发送帖子或使用 PHP 并检查/清理输入。

【讨论】:

  • 感谢您抽出宝贵时间回答!我确实在其他网站上看到过这种方法,但我遇到的问题是我需要它来输出实际的 HTML 代码(如上面的屏幕截图所示)。我认为我朝着正确的方向前进,我的代码中出现了一些错误,这些错误阻止它在整个字符串中拉伸渐变 ** EDITED** 以使其更加透明——我正在使用该函数一个聊天网站。我们正在尝试允许聊天者使用 /fade 命令在主屏幕上创建褪色文本消息——这就是为什么我需要它来输出实际标记
  • @MikeRoberts 任何函数都可以输出实际的标记。只需将 html 设置为某个变量,并将带有样式的元素附加到它(变量),然后再次将结束的 html 标记附加到它。然后将变量回显。出来。
  • @user3473227 -- 好的,这在逻辑上是有道理的,感谢您的信息!你能给我举个例子,让我知道从哪里开始吗?再次感谢您抽出这么多时间
  • @MikeRoberts 顺便说一句,您可以将颜色更改为红色和蓝色。我没有特殊原因使用黑白。
  • @user3473227 感谢您抽出宝贵时间制作该示例!我能够让它按照我想要的方式为 chrome 工作,但渐变没有出现在 IE 中。我猜这与webkit的东西有关?有没有办法让它们跨浏览器显示?
【解决方案2】:

为什么不这样做

$string = 'My text'
$count = 0;
for($i=0,$i<=255,$i++){
   for($i_i=0,$i_i<=255,$i_i++){
      for($i_i_i=0,$i_i_i<=255,$i_i_i++){
            echo '<span style="color:rgb('.$i.','.$i_i.','.$i_i_i.')">'.$string[$count].'</span>';
            if($count == strlen($string))break;
            $count++;
      }
      if($count == strlen($string))break;
   }
   if($count == strlen($string))break;
}

或者设置$i...的一些其他值来获得另一种颜色。

【讨论】:

  • 您也可以在echo函数中更改$i, $i_i, $i_i_i
  • 感谢您抽出宝贵时间撰写答案。我试图弄清楚如何将您编写的代码合并到我的示例中以使其工作。我会一直在这里。这整个功能让我感到困惑。大声笑
  • 我修改了我的代码并更新了原始问题。这和你说的更接近吗?
  • 尝试构建$i (for($i,$i&lt;255,$i=$i + your iterator)) 取决于你的字符串长度。
猜你喜欢
  • 2016-09-14
  • 1970-01-01
  • 2013-09-13
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
相关资源
最近更新 更多