1.确认php中GD库是否开启

  在PHP配置文件php.ini中查找extension=php_gd2.dll,去掉前边的(分号) ';' 即可,一般php是默认开启的

2.绘画步骤

  • 创建一个画布(画板)、画笔、色彩。
  • *开始绘画(重点,难点)
  • 输出图像(复制型)
  • 销毁图像资源(释放内存)

3.示例

  为了方便演示,先了解后两步

  输出图像:   

1 header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
2 imagejpeg($im);//输出一个jpeg图片
3
4 header("Content-Type: image/png");//设置响应头信息为一个png的图片
5 imagepng($im);//输出一个png图片
6
7 //输出到指定文件中(另存为)
8 imagepng($im,"**.png");

  销毁图像(解除占用的资源):

1 bool imagedestroy  ( resource $image  )//销毁一图像 

  创建一个画布:

1 //创建一个基于256色的画布
2 $img=imagecreate(400,400);
3 echo $img;

  显示效果:

 

1 //新建一个真彩色图像
2 $img=imagecreatetruecolor(400,400);
3 echo $img;

  显示效果:

  其他的方式:

//还有基于某个图片的画布
imagecreatefromgif( string filename )
imagecreatefrompng( string filename )
imagecreatefromjpeg( string filename )

真彩和256色画布的显示区别-----真彩的会默认填充黑色的画布

<?php  //创建一个基于256色的画布$img1=imagecreate(400,400);$img2=imagecreatetruecolor(400,400);// 输出图像header('content-type:image/png');// imagepng($img1);imagepng($img2);// 销毁图像,解除占用的资源// imagedestroy($img1);imagedestroy($img2);
?>

显示效果:

  

*开始绘画

  

<?php//分配定义颜色$red = imagecolorallocate($im,255,0,0); //分配一个颜色//填充背景bool imagefill(resource image,int x,int y, int color ); //填充背景//画点bool imagesetpixel(resource image,int x,int y,int color );//画一个像素点//画一个线段的函数bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )//画矩形框(不填充)bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int color )//画矩形框(填充)bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )//绘制多边形bool imagepolygon ( resource image, array points, int num_points, int color )bool imagefilledpolygon ( resource image, array points, int num_points, int color )//绘制椭圆(正圆)imageellipse ( resource image, int cx, int cy, int w, int h, int color )imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )//绘制圆弧(可填充)imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )//绘制字串bool imagestring ( resource image, int font, int x, int y, string s, int col )bool imagestringup ( resource image, int font, int x, int y, string s, int col )//绘制字符
        imagechar//绘制文本:*array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )//当上面的字体出现乱码时,使用下面的函数转换编码string iconv ( string in_charset, string out_charset, string str )$name="张三";$str = iconv("ISO8859-1","UTF-8",$name);$str = iconv("GBK","UTF-8",$name);$str = iconv("GB2312","UTF-8",$name);//图片的裁剪、合成、缩放**bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )* imagesx — 取得图像宽度* imagesy — 取得图像高度* array getimagesize ( string $filename [, array &$imageinfo ] )  取得图像大小
?>

综合代码示例:

  

 1 <?php
 2     // 创建画布
 3     $img=imagecreatetruecolor(1000,1000);
 4     // 创建颜色
 5     $red=imagecolorallocate($img,255,0,0);
 6     $blue=imagecolorallocate($img, 0, 0, 255);
 7     $green=imagecolorallocate($img, 0, 255, 0);
 8     $gray=imagecolorallocate($img, 200, 200, 200);
 9
10     //填充颜色
11     imagefill($img,100,0,$gray);
12     //创建一个点
13     imagesetpixel($img, 20, 20, $red);
14     // 随机创建1000个点
15     for($i=0;$i<1000;$i++){
16         imagesetpixel($img, rand(0,1000), rand(0,1000), $red);
17     }
18     //输出线段
19     imageline($img, 0, 0, 200, 200, $blue);
20     //输出矩形
21     imagerectangle($img, 200, 200, 400, 400, $blue);//不填充
22     imagefilledrectangle($img, 200, 400, 400, 600, $blue);//填充
23     //绘制多边形   imagefilledpolygon----填充
24     imagepolygon($img, array(0,0,100,200,300,200), 3, $blue);
25     //绘制椭圆(正圆)
26     imageellipse($img, 600, 600, 200, 200, $blue);
27     imagefilledellipse($img, 800, 600, 200, 200, $blue);
28     // 绘制字符串
29     imagestring($img, 15, 600, 200, "string", $blue);//水平
30     imagestringup($img, 15, 600, 200, "string", $blue);//垂直
31     // 绘制字符
32     imagechar($img, 5, 800, 200, 'H', $blue);
33     imagecharup($img, 5, 800, 200, 'H', $blue);
34     //绘制文本
35     imagettftext($img, 20, 0, 500, 500, $blue, 'bb.ttc', 'this is string!');
36     /*当上面的字体出现乱码时,使用下面的函数转换编码
37     string iconv ( string in_charset, string out_charset, string str )*/
38     //imagecopyresampled($img, "dd.jpg", 200, 200, 200, 200, 200, 200, 200, 200);
39     //输出图像
40     header('content-type:image/png');
41     imagepng($img);
42     //销毁图像
43     imagedestroy($img);
44 ?>

难重点:

转载于:https://www.cnblogs.com/zhengfengyun/p/5905734.html

PHP-利用GD库新建图像相关推荐

  1. php 利用GD库在制定图片上添加文字

    我们可以利用php的gd库扩展来对我们的图片进行处理,例如是生成缩略图,对图片进行裁切,和本章将说的在图片添加文字. 1.首先我们需要接受到我们需要的文字:$key = $_GET['key'];具体 ...

  2. php gd库 函数 建立gif,PHP_PHP GD库生成图像的几个函数总结,使用GD库中提供的函数动态绘 - phpStudy...

    PHP GD库生成图像的几个函数总结 使用GD库中提供的函数动态绘制完成图像以后,就需要输出到浏览器或者将图像保存起来.在PHP中,可以将动态绘制完成的画布,直接生成GIF.JPEG.PNG和WBMP ...

  3. PHP GD库生成图像的几个函数总结

    使用GD库中提供的函数动态绘制完成图像以后,就需要输出到浏览器或者将图像保存起来.在PHP中,可以将动态绘制完成的画布,直接生成GIF.JPEG.PNG和WBMP四种图像格式.可以通过调用下面四个函数 ...

  4. php 利用GD库将正方形图片变成圆形

    当我们获取到微信头像时,我们抓取下来会发现是正方形,但是现实中我们一般都是用圆形的. 以下我将介绍用php 原生GD库,将图片抓取下来后切成圆角,其实代码很简单,所以就直接是上代码了 <?php ...

  5. php的验证码要gd库,PHP利用GD库实现一个简单的验证码

    搜索热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. $img=imagecreatetruecolor(100,40); $red= ...

  6. 宝塔php gd库,宝塔面板安装 EasyImag – 一款最简单图床的安装体验

    近日闲逛,发现了一款图床,一款开箱即食的简单图床程序.因为没有数据库所以安装起来也是异常简单,我们看看功能:支持设置图片质量 支持仅登录后上传 支持QQ截图,剪切板上传 支持在线管理(增删改查) 支持 ...

  7. PHP用gd库给图片添加水印,php用GD库给图片添加水印

    php用GD库给图片添加文字水印,整个代码比较简单,DEMO如下: /*打开图片*/ //1.配置图片路径 $src = "aeroplane.jpg"; //2.获取图片信息 $ ...

  8. php 图片上加文字,php使用GD库实现图片上添加文字的方法(代码)

    本篇文章给大家带来的内容是关于php使用GD库实现图片上添加文字的方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 我们可以利用php的gd库扩展来对我们的图片进行处理,例 ...

  9. php gd图片验证,php笔记之GD库图片创建/简单验证码

    燕十八 公益PHP培训 课堂地址:YY频道88354001 学习社区:www.zixue.it php画图:比如说验证码,缩略图,加水印都要用到GD库,所以要开启gd2库,才能用 首先找到php.in ...

  10. PHP GD库及jpgraph的安装与配置

    一.了解 GD 库 在PHP中可以使用GD库对图像进行操作.DG库是一个开放的动态创建图像.源代码公开的函数库,可以从官主网站下载.目前GD库支持gif .png .jpeg. wbmp和xbm等多种 ...

最新文章

  1. 拆卸台式电脑主机,cpu,硬盘,内存条等
  2. win10桌面和手机的扩展API,判断是否有实体后退键API
  3. 【深度学习】基于Pytorch的线性模型概念辨析和实现(一)
  4. mysql三范式和反三范式_数据库三范式和反三范式
  5. python opencv cv.applyColorMap()函数(颜色映射)ColormapTypes【将Intel Realsense D435深度图的黑白图映射为彩色图】
  6. 致Go学习者, 该跟大佬学习做项目了
  7. Android给TextView和EditText等控件设置透明背景、圆角边框
  8. android实现箭头流程列表_Android开发关于ExpandableListView上下箭头左右显示的笔记...
  9. python logging模块 默认_python logging模块
  10. 西安科技大学计算机学院保研,独臂姑娘,好样的!
  11. java 中怎么比较两个时间相差的秒数
  12. js 编译emoji表情
  13. 微店关键词取商品列表API接口(item_search-根据关键词取商品列表API接口),微店API接口
  14. flush-hosts
  15. 在使用ambari进行安装部署过程中遇到的glibc问题
  16. KAPPA领衔实施服装ERP软件树立行业榜样
  17. Qt FFmpeg视频播放器开发(二):FFmepg基本使用与视频播放
  18. Container is running beyond physical memory limits
  19. python抓取财务数据_Python与财务「上」——数据采集篇
  20. 【仙人掌直径】P4244 [SHOI2008]仙人掌图 II

热门文章

  1. linux网络 (二):无线网络操作
  2. iOS-CoreText的那些事【电子书的那些事】
  3. JMeter基础教程1:若隐若现的参数化
  4. python学习笔记第三节
  5. Java Synchronized的用法
  6. OpenGL ES API with no current context
  7. Star UML指导手册
  8. 使用kitti2bag和RVIZ播放KITTI数据集
  9. python_ 学习笔记(基础语法)
  10. css3-伪元素与伪类