PHP Generate text Image effect

Source: Internet
Author: User
Tags transparent color

PHP Generate text Image effect
Recently see the GD function of PHP, try to make a basic text image effect of the code:

Display text Picture 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 the specific color to be a transparent color, if the annotation

$black = Imagecolorallocate ($im, 0x00,0x00,0x00);
Imagefilledrectangle ($im, 50,50,150,150, $black);
Imagestring ($im, 5,50,160, "Happy every Day", $black);

Imagettftext ($im, 15,0,50,40, $black, "D:\windows\Fonts\simhei.ttf", $str); Font Settings section Linux and Windows may have different paths
Header ("Content-type:image/png");
Imagepng ($im);
?>

Call Picture 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: Create a link-like, underlined blue text image

Get the text and convert it into a picture
$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";//font setting part of the Linux path
$text = $str; The string to display
$font _size = 14; Font size
$arr = Imagettfbbox ($font _size,0, $font _file, $text); Determine the position of the string that will change
$text _width = $arr [2]-$arr [0]; String text box length
$text _height = $arr [3]-$arr [5]; String text Box height

$im = Imagecreate ($text _width, $text _height);
$white = Imagecolorallocate ($im, 0xff,0xff,0xff);
Imagecolortransparent ($im, $white); Imagecolortransparent () sets the specific color to be a transparent color, if the annotation
$blue = Imagecolorallocate ($im, 0,0,255);

$arr = Imagettftext ($im, $font _size,0,0, $text _height-5, $blue, $font _file, $text);
Imageline ($im, $arr [0], $arr [1], $arr [2], $arr [3], $blue);
Imagettftext ($im, 12,0,0,20, $black, "C:\windows\Fonts\simhei.ttf", $url);//font Set the path to Windows

Header ("Content-type:image/png");
Imagepng ($im);
Imagedestroy ($im);

Text watermark:
<?php
$pic =imagecreate (250,30);
$black =imagecolorallocate ($pic, 0,0,0);
$white =imagecolorallocate ($pic, 255,255,255);
$font = "C://windows//fonts//simhei.ttf";
$str = ' php '. Iconv (' gb2312 ', ' utf-8 ', ' face object '). "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, 0,0,0,0,250,30,50);
Imagejpeg ($im);
?>

PHP Generate text Image effect

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.