Recently I saw the GD function of PHP and tried to make a code that basically generates text images:
Test environment: Windows + Apache + PHP
Show text image page: Demo. php
<? PHP
$ STR = $ _ request ['str']? $ _ Request ['str']: "No input ";
// $ STR = "People's Republic of China ";
$ Im = imagecreate (200,200 );
$ White = imagecolorallocate ($ im, 0xff, 0xff, 0xff );
Imagecolortransparent ($ im, $ white); // imagecolortransparent () sets a specific color to transparent.
$ Black = imagecolorallocate ($ im, 0x00,0x00,0x00 );
Imagefilledrectangle ($ im, 50, 50, 150,150, $ black );
Imagestring ($ im, 160, "happy every day", $ black );
Imagettftext ($ im, 15, 0, 50, 40, $ black, "d: \ windows \ fonts \ simhei. TTF ", $ Str); // the path of some Linux and Windows fonts may be different.
Header ("Content-Type: image/PNG ");
Imagepng ($ IM );
?>
Call the image page: demo2.php
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<? PHP $ Var = "People's Republic of China";?>
">
Text image advanced:
Environment: Red Hat as4.0 + gd2.0.28 + FreeType 2.1.9
Purpose: To generate an underlined blue text image with similar links
// Obtain the text and convert it into an image
// $ STR = urldecode ($ _ request ['str'])? Urldecode ($ _ request ['str']): "No input ";
// $ STR = "People's Republic of China ";
$ Font_file = "/usr/share/fonts/zh_cn/TrueType/gbsn00lp. TTF"; // set the path of some Linux
$ Text = $ STR; // string to be displayed
$ Font_size = 14; // font size
$ Arr = imagettfbbox ($ font_size, 0, $ font_file, $ text); // determine the position of the variable string
$ Text_width = $ arr [2]-$ arr [0]; // string text box Length
$ Text_height = $ arr [3]-$ arr [5]; // The height of the string text box
$ Im = imagecreate ($ text_width, $ text_height );
$ White = imagecolorallocate ($ im, 0xff, 0xff, 0xff );
Imagecolortransparent ($ im, $ white); // imagecolortransparent () sets a specific color to transparent.
$ Blue = imagecolorallocate ($ im, 255 );
$ Arr = imagettftext ($ im, $ font_size, $ text_height-5, $ blue, $ font_file, $ text );
Imageline ($ im, $ arr [0], $ arr [1], $ arr [2], $ arr [3], $ blue );
// Imagettftext ($ im, $ black, "C: \ WINDOWS \ fonts \ simhei. TTF", $ URL); // set the Windows path in the font.
Header ("Content-Type: image/PNG ");
Imagepng ($ IM );
Imagedestroy ($ IM );
Text watermark:
<? PHP
$ PIC = imagecreate (250,30 );
$ Black = imagecolorallocate ($ pic, 0, 0 );
$ White = imagecolorallocate ($ pic, 255,255,255 );
$ Font = "C: // windows // fonts // simhei. TTF ";
$ STR = 'php'. iconv ('gb2312', 'utf-8', 'object facing '). "ovliverlin.cnblogs.com ";
Imagettftext ($ pic, 10, 0, 10, 20, $ white, $ font, $ Str );
Header ("Content-Type: image/JPEG ");
$ Filename = '../src/images/photo.jpg ';
$ Im = imagecreatefromjpeg ($ filename );
Imagecopymerge ($ im, $ pic, 50 );
Imagejpeg ($ IM );
?>