文档详情

php图形处理教程.php生成图片.doc

发布:2017-03-22约5.28千字共5页下载文档
文本预览下载声明
在web应用中经常会用到生成图片这一功能,在php教程?中创建图片需要gd库的支持才能创建图形,有了这个图形功能,我们就可以方便的生成缩图,验证码,给图片加水印等。 在php中要安装gd库才能正常运行创建图片功能,方法如下,在win系统,找到php.ini把 ;extension=php_gd2.dll前面的;去了,重起apache就OK了。 下面我们一看实例 php的gd库可以生成多种图像文件,如gif,png,jpg,wbmp,xpm等,下面来看一个生成正方形的文件。 ?php $height = 300; $width = 300; //创建背景图 $im = ImageCreateTrueColor($width, $height); //分配颜色 $white = ImageColorAllocate ($im, 255, 255, 255); $blue = ImageColorAllocate ($im, 0, 0, 64); //绘制颜色至图像中 ImageFill($im, 0, 0, $blue); //绘制字符串:Hello,PHP ImageString($im, 10, 100, 120, Hello,PHP, $white); //输出图像,定义头 Header (Content-type: image/png); //将图像发送至浏览器 ImagePng($im); //清除资源 ImageDestroy($im); ? 查看结果只要浏览php文件就可以了,如果要图像调用img src=a.php / 实例二,使用基本函数创建图片imagecreate() resource imagescreate(int x,inty) imagedestroy 是放图片所占内存空间 int ingaedestroy( image) imagecopy() int imagecopy( dst_im,sr_im,int x,int y,int x,int y,) 下面来看实例 ?php header(Content-type: image/jpeg); // 载入图像 $imagen1 = imagecreatefromjpeg(imagen1.jpg); $imagen2 = imagecreatefromjpeg(imagen2.jpg); // 复制图像 imagecopy($imagen1,$imagen2,0,0,0,0,200,150); // 输出jpeg图像? imagejpeg($imagen1); //释放内存 imagedestroy($imagen2); imagedestroy($imagen1); ? 实例三在图片上图片文字 在这个功能上我们会用到imageCreateFromJpeg这个函数是,把来自文件或者form的图片重新创建一次, resource imageCrrateFromJpge(string,imageName); imageCrateFromPng(); resource imageCrrateFromJpge(string,imageName); 看实例 ?php //PNG格式图像处理函数 function Loadpng ($imgname) { ??? $im = @ImageCreateFromPNG ($imgname);? ??? if (!$im) {??? //载入图像失败?????????????????????? ??????? $im = ImageCreate (400, 30);?????? ??????? $bgc = ImageColorAllocate ($im, 255, 255, 255); ??????? $tc? = ImageColorAllocate ($im, 0, 0, 0); ?????? ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc); ?????? ImageString($im, 4, 5, 5, Error loading: $imgname, $tc); ??? } ??? return $im; ?} ?$imgPng=Loadpng(./karte.png); ?? /* 输出图像到浏览器 */ ?header(Content-type: image/png); ?imagePng($imgPng);? ?? ?php $img = imageCreate(100, 100); $black = imageColorAllocate($img, 0, 0, 0); $white = imageColorAllocate($img, 255, 255,
显示全部
相似文档