简体   繁体   中英

Php Colored Words, Centered and Multiline Text

Multiline text, centered, each word must be of different color. I've tried too many methods. But none of them could do what I wanted. How can this be an easy and trouble-free way?

How can the text be divided into words of 3-4, with the same color background, each with a different color?

$metin="Music Industry Sharks How to Uncover the Unscrupulous Many in the Music Business";

Example with still gd library:

use GDText\Box;
use GDText\Color;

    $files = glob('./fontlar/*.ttf');
$fontik = implode("||", $files);
$fontik2=$fontik;




$metin="Music Industry Sharks How to Uncover the Unscrupulous Many in the Music Business";
$metin=preg_replace("/[^A-Za-z0-9öÖüÜéèêôÄäçÇŞşĞğİı ]/", '', $metin);
$yenimetin=str_replace('-','',$metin);
$yenimetin=str_replace("\n",'',$yenimetin);
$yenimetinp = wordwrap($yenimetin, 1, "<br>", false);

echo $yenimetinp;
$prc=explode('<br>',$yenimetinp);

$i=0;

while ($i<=substr_count($yenimetinp,'<br>')) {


if (substr_count($fontik2,'||')<=0); $fontik2=$fontik;
$nafont=explode('||',$fontik2);
$nafont=$nafont[rand(0,substr_count($fontik2,'||'))];
$fontik2=str_replace($nafont.'||','',$fontik2);
$fontik2=str_replace('||'.$nafont,'',$fontik2);



$im = imagecreatetruecolor(2000, 1000);
$backgroundColor = imagecolorallocate($im, 0, 18, 64);
imagefill($im, 0, 0, $backgroundColor);

$renkler=array("#FFFFFF","#99FF00","#FFCC00","#ACF17E","#92E4E4","#FFFF33","#00FF66");

$rgb = hex2rgb($renkler[array_rand($renkler)]);

$r=$rgb[0];
$g=$rgb[1];
$b=$rgb[2];

$box = new Box($im);
$box->setFontFace(__DIR__.$nafont);
$box->setFontColor(new Color($r, $g, $b));
$box->setTextShadow(new Color(0, 0, 0, 50), 2, 2);
$box->setFontSize(150);
$box->setBox(strlen($prc[$i]), 20, 1800, 1000);
$box->setTextAlign('left', 'center');

$box->draw($prc[$i]);

    $renkler=array("#FFFFFF","#99FF00","#FFCC00","#ACF17E","#92E4E4","#FFFF33","#00FF66");

$rgb = hex2rgb($renkler[array_rand($renkler)]);

$r=$rgb[0];
$g=$rgb[1];
$b=$rgb[2];
$box->setFontFace(__DIR__.$nafont);
$box->setFontColor(new Color($r, $g, $b));
$box->setTextShadow(new Color(0, 0, 0, 50), 2, 2);
$box->setFontSize(150);
$box->setBox(strlen($prc[$i])*100, 20, 1800, 1000);
$box->setTextAlign('left', 'center');

$box->draw($prc[$i+1]);

    $renkler=array("#FFFFFF","#99FF00","#FFCC00","#ACF17E","#92E4E4","#FFFF33","#00FF66");

$rgb = hex2rgb($renkler[array_rand($renkler)]);

$r=$rgb[0];
$g=$rgb[1];
$b=$rgb[2];
$box->setFontFace(__DIR__.$nafont);
$box->setFontColor(new Color($r, $g, $b));
$box->setTextShadow(new Color(0, 0, 0, 50), 2, 2);
$box->setFontSize(150);
$box->setBox(strlen($prc[$i+1])*150, 20, 1800, 1000);
$box->setTextAlign('left', 'center');

$box->draw($prc[$i+2]);

imagepng($im,'./tmp/test'.$i.'.png');
/*

$cropped = imagecropauto($im, IMG_CROP_SIDES);      
imagepng($cropped,'./tmp/test'.$i.'.png');
imagedestroy( $cropped );   
*/
    $i++;
}

测试0

Failed .

sign valid css colours in var colors bellow

var colors = ["#ED401B", "blue", "red", "green", "yellow"];

$('#your-text').html(function (i, text) {
    return $.map(text.split(' '), function (word) {
         return '<div style="color:'
             + colors[Math.floor(Math.random() * colors.length)]
             + '">' + word + '</div>';
    }).join(' ');
});

Here is jsfiddle: https://jsfiddle.net/4fhvsw1j/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM