web开发N例-案例2:PHP绘制图片、绘制中文

来源:互联网 发布:海牛大数据 编辑:程序博客网 时间:2024/06/03 05:06
web开发N例
案例2:PHP绘制图片、绘制中文
case2.php:
<span style="font-family:Arial;"><!DOCTYPE html><html><head>    <!-- 请将文档保存为UTF-8编码,否则中文会乱码 -->    <meta charset="UTF-8">    <title>案例2</title><!-- PHP绘制图片、绘制中文 --></head><body><?php$im  = @ imagecreate ( 300 ,  200 ) or die( "无法初始化GD2引擎,请打开php.ini中gd2插件" );$background_color  =  imagecolorallocate ( $im ,  0 ,  0 ,  255 );//初次调用imagecolorallocate函数会填充该颜色的背景$text_color  =  imagecolorallocate ( $im ,  233 ,  14 ,  91 );//绘制普通英文//注意:第三、四个参数x、y指绘制文字的左上角坐标imagestring ( $im ,  5 ,  5 ,  5 ,   "img in same page" ,  $text_color );imagepng ( $im ,"test.png");//将文件存到本地imagedestroy ( $im );//销毁图片资源?><img alt="" src="test.png"><!-- 显示来自同一页面中的图片 --><img alt="" src="case2-img.php"><!-- 显示来自另一个php文件的图片 --></body></span><span style="font-family:Times New Roman;"></html></span>


case2-img.php:
<span style="font-family:Arial;"><?phpheader ( "Content-type: image/png" );//发送一个http头,告诉浏览器这是一个png的图片资源$im  = @ imagecreate ( 300 ,  200 )or die( "无法初始化GD2引擎,请打开php.ini中gd2插件" );//初次调用imagecolorallocate函数会填充该颜色的背景$background_color  =  imagecolorallocate ( $im ,  0 ,  0 ,  255 );$text_color  =  imagecolorallocate ( $im ,  233 ,  14 ,  91 );//绘制普通英文//注意:第三、四个参数x、y指绘制文字的左上角坐标imagestring ( $im ,  5 ,  0 ,  0 ,   "img in another page" ,  $text_color );/*绘制中文:需要注意以下两点 *Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。 *Note: 此函数仅在 PHP 编译时加入 freetype 支持时有效(--with-freetype-dir=DIR )*///引入方正大黑简体字体文件$font_file="./FZDHTJW.TTF";//注意:第三、四个参数x、y指绘制文字的左下角坐标$str='中文';// 如果创建网页时,使用的是utf-8字符集,则不需要使用iconv()函数,否则需要转换为UTF-8//$str=iconv("gb2312","utf-8",$str);imagefttext($im,13,0,200,100,$text_color,$font_file ,$str);imagepng ( $im );//输出图片至浏览器imagedestroy ( $im );//销毁图片资源</span>

注释够详细了,不用再说什么了吧。

0 0
原创粉丝点击