景初

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

·函数resource imagecreatefromgif(string $filename),用来从给定的GIF文件或URL取出一个图像,参数$filename是文件名或URL。该函数返回值是图像标识符,代表了从给定的文件名取得的图像。失败时,返回一个空字符串,并且输出一条错误信息。

·函数bool imagegif(resource $image [ , string $filename]),该函数从参数$image所代表的图像以参数$filename为文件名创建一个GIF图像。image参数是imagecreate()或imagecreatefromgif等函数的返回值。

·函数resource imagecreatefrompng(string $filename),该函数从PNG文件或URL文件取出一个图像,参数$filename是文件名或URL。该函数返回值是图像标识符,如果执行失败,函数返回一个空字符串,并且输出一条错误信息。

·函数bool imagepng(resource $image [, $filename]),该函数类似imagegif(),将GD图像流(参数$image代表)以PNG格式输出到标准输出(通常为浏览器),或者如果用参数$filename给出了文件名,则将其输出到该文件。

·函数resource imagecreate(int $x_size,int $y_size),新建一个基于调色板的图像,参数$x_size和$y_size代表了创建图像的宽和高,该函数返回所创建图像的标识符。

·函数resource imagecreatetruecolor($int $x_size,$int $y_size),该函数返回一个图像标识符,它代表了一副大小为$x_size和$y_size的黑色图像。

·函数int imagecolorallocate(resource $image,int $red ,int $green,int $blue),参数$image是图片标识符,参数$red、$green 、$blue分别代表色系中的红色,绿色和蓝色(RGB),这些参数的取值范围是0到255,或者十六进制的0x00到0xFF,该函数的返回值代表了由给定的参数组成的颜色。

·函数bool imagefill(resource $image,int $x,int $y,int $color),该函数在参数$image所指定图像的坐标$x和$y(图像左上角为0,0处用$color颜色执行区域填充,即与x,y点颜色相同且相邻的点都会被填充。

·函数bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,$color),该函数用参数$color所指定的颜色在参数$image所标识的图像中从坐标$x1,$y1到$x2,$y2画一条线段,

·函数bool imagestring(resource $image,int $font,int $x,int $y,string $s,int $col)水平地显示一行字符串,该函数用参数$col所指定的颜色将字符串$s显示到参数$image所标识图像的$x,$y坐标处。

1.图像的建立

(1)建立画布

(2)在画布上绘制形状或书写文本

(3)输出最终的图片

(4)清空绘图资源

2.用PHP生成图像

<?php
header("Content-type: image/gif");
$width = 200 ;
$height=200;
$img =imagecreatetruecolor($width,$height) or die ("不支持GD图像处理");
imagepng($img);
imagedestroy($img);
?>

3.设定图像颜色

<?php
header("Content-type: image/gif");
$width = 200 ;
$height=200;
$img =imagecreatetruecolor($width,$height) or die ("不支持GD图像处理");
$bg_color=imagecolorallocate($img,255,0,0);
imagefill($img,0,0,$bg_color);
imagepng($img);
imagedestroy($img);
?>
4.在图像上绘制直线

<?php
header("Content-type: image/gif");
$width = 200 ;
$height=300;
$img =imagecreatetruecolor($width,$height) or die ("不支持GD图像处理");
$line_color=imagecolorallocate($img,255,255,255);
imageline($img,0,40,200,40,$line_color);
imageline($img,0,260,200,260,$line_color);
imagepng($img);
imagedestroy($img);
?>

5.在图像上输出文字

<?php
header("Content-type: image/gif");
$width = 200 ;
$height=300;
$img =imagecreatetruecolor($width,$height) or die ("不支持GD图像处理");
$line_color=imagecolorallocate($img,255,255,255);
imageline($img,0,40,200,40,$line_color);
imageline($img,0,260,200,260,$line_color);
imagestring($img,5,0,60,"It's time to learn PHP!",$line_color);
imagepng($img);
imagedestroy($img);
?>

6.在图像上输出中文

如果在图像上显示中文,需要将中文字符做转换,转换成16进制的字符。

首先在WINDOWS记事本中输入中文字符,保存时按UTF-8编码保存。之后通过UltraEdit打开该文件,在工具栏中选择“切换Hex模式”按钮,即按16进制格式查看文件内容,就可以看到中文字符的16进制编码。

在程序中,还需要使用函数chr()返回这些16进制编码所表示的单个字符。在图像上显示中文使用的函数是imagettftext(),该函数输出中文时需要用TrueType字体向图像写入文本。

<?php
header("Content-type: image/gif");
$width = 200 ;
$height=300;
$img =imagecreatetruecolor($width,$height) or die ("不支持GD图像处理");
$line_color=imagecolorallocate($img,255,255,255);

$font_type="c://WINDOWS//Fonts//SIMYOU.TTF";

$cn_char1=chr(0xE8).chr(0xA5).chr(0xBF);
$cn_char2=chr(0xE6).chr(0xB8).chr(0xB8);
$cn_char3=chr(0xE8).chr(0xAE).chr(0xB0);

$cn_str=chr(0xE5).chr(0x90).chr(0xB4).chr(0xE6).chr(0x89).chr(0xBF).chr(0xE6).chr(0x81).chr(0xA9);
$cn_str.=" ".chr(0xE8).chr(0x91).chr(0x97);


imageline($img,0,40,200,40,$line_color);
imageline($img,0,260,200,260,$line_color);

imagettftext($img,30,0,10,80,$line_color,$font_type,$cn_char1);
imagettftext($img,30,0,10,120,$line_color,$font_type,$cn_char2);
imagettftext($img,30,0,10,160,$line_color,$font_type,$cn_char3);

imagettftext($img,15,0,90,254,$line_color,$font_type,$cn_str);

imagepng($img);
imagedestroy($img);
?>


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2014-05-06 07:56  景初  阅读(138)  评论(0编辑  收藏  举报