Implement gradient font output using PHP

Source: Internet
Author: User

First, let's demonstrate:

The Code is as follows:

<? Php
Header ("Content-type: image/png ");

$ Txt = $ _ GET [txt]. "(I will change the color when refreshing )";
$ I = strlen ($ txt );
$ Width = 16 * strlen ($ txt );
$ Height = 42 * (int) ($ width/500 + 1 );
$ Im = imagecreate ($ width, $ height );

$ Background_color = imageColorAllocate ($ instant, 223,223,223 );
$ White = imageColorAllocate ($ im, 255,255,255 );
$ Color1 = imageColorAllocate ($ im, 255,128,128 );
$ Red = imageColorAllocate ($ im, 255, 0, 0 );
$ Color3 = imageColorAllocate ($ im, 128,64, 64 );
$ Yellow = imageColorAllocate ($ im, 255,255, 0 );
$ Color4 = imageColorAllocate ($ im, 255,128, 0 );
$ Color5 = imageColorAllocate ($ im, 128,128, 0 );
$ Green = imageColorAllocate ($ im, 0,255, 0 );
$ Color6 = imageColorAllocate ($ im, 0,128, 0 );
$ Blue = imageColorAllocate ($ im, 255 );
$ Pink = imageColorAllocate ($ im, 128 );
$ Black = imageColorAllocate ($ im, 0, 0 );

$ Array = array ($ color1, $ red, $ color3, $ color4, $ color5, $ green, $ color6, $ blue, $ pink, $ black, $ white );

$ X = 1;
$ Y = 40;
$ Count = 1;

For ($ t = 0; $ t <$ I; $ t ++)
{
$ Temp = rand (0, 10 );
$ Color = $ array [$ temp];
If (ord ($ txt [$ t])> = 128)
{
If ($ x + 32> 500)
{
$ X = 1;
$ Y = 40 + $ count * 41;
$ Count ++;
}
$ Temp = $ txt [$ t ++]. $ txt [$ t];
$ Temp = mb_convert_encoding ($ temp, "UTF-8", "gb2312 ");
Imagettftext ($ im, 24, 0, $ x, $ y, $ color, "tupian/finish/simsun. ttc", $ temp );
$ X + = 32;

}
Else
{
If ($ x + 8> 500)
{
$ X = 1;
$ Y = 40 + $ count * 41;
$ Count ++;
}
Imagettftext ($ im, 24, 0, $ x, $ y, $ color, "tupian/finish/simsun. ttc", $ txt [$ t]);
$ X + = 16;
}
}
ImagePNG ($ im );
ImageDestroy ($ im );

?>


Usage instructions: Save the above Code as 1.php!

1. Enter http: // your url/1.php? in the browser? Txt = text can be viewed directly
2. Post in the Forum in the form of {img} http: // your url/1.php? Txt = text {/img} (braces are replaced with brackets in actual use)

PS: If your space does not support the mb_convert_encoding function, you need to convert gb2312 to UTF-8 to support Chinese characters!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.