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

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

2.绘画步骤

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

3.示例

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

  输出图像:   

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

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

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

  创建一个画布:

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

  显示效果:

 

 //新建一个真彩色图像
$img=imagecreatetruecolor(400,400);
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 ] ) 取得图像大小
?>

综合代码示例:

  

 <?php
// 创建画布
$img=imagecreatetruecolor(1000,1000);
// 创建颜色
$red=imagecolorallocate($img,255,0,0);
$blue=imagecolorallocate($img, 0, 0, 255);
$green=imagecolorallocate($img, 0, 255, 0);
$gray=imagecolorallocate($img, 200, 200, 200); //填充颜色
imagefill($img,100,0,$gray);
//创建一个点
imagesetpixel($img, 20, 20, $red);
// 随机创建1000个点
for($i=0;$i<1000;$i++){
imagesetpixel($img, rand(0,1000), rand(0,1000), $red);
}
//输出线段
imageline($img, 0, 0, 200, 200, $blue);
//输出矩形
imagerectangle($img, 200, 200, 400, 400, $blue);//不填充
imagefilledrectangle($img, 200, 400, 400, 600, $blue);//填充
//绘制多边形 imagefilledpolygon----填充
imagepolygon($img, array(0,0,100,200,300,200), 3, $blue);
//绘制椭圆(正圆)
imageellipse($img, 600, 600, 200, 200, $blue);
imagefilledellipse($img, 800, 600, 200, 200, $blue);
// 绘制字符串
imagestring($img, 15, 600, 200, "string", $blue);//水平
imagestringup($img, 15, 600, 200, "string", $blue);//垂直
// 绘制字符
imagechar($img, 5, 800, 200, 'H', $blue);
imagecharup($img, 5, 800, 200, 'H', $blue);
//绘制文本
imagettftext($img, 20, 0, 500, 500, $blue, 'bb.ttc', 'this is string!');
/*当上面的字体出现乱码时,使用下面的函数转换编码
string iconv ( string in_charset, string out_charset, string str )*/
//imagecopyresampled($img, "dd.jpg", 200, 200, 200, 200, 200, 200, 200, 200);
//输出图像
header('content-type:image/png');
imagepng($img);
//销毁图像
imagedestroy($img);
?>

难重点:

PHP->利用GD库新建图像的更多相关文章

  1. php学习笔记:利用gd库生成图片,并实现随机验证码

    说明:一些基本的代码我都进行了注释,这里实现的验证码位数.需要用的字符串都可以再设置.有我的注释,大家应该很容易能看得懂. 基本思路: 1.用mt_rand()随机生成数字确定需要获取的字符串,对字符 ...

  2. PHP 使用GD 库绘制图像,无法显示的问题

    根据官方GD 库绘制图像文档样式 原基本样式 $width = 120; $height = 50; $img = @imagecreatetruecolor($width, $height) or ...

  3. 【PHP】使用GD库实现 图像生成、缩放、logo水印和简单验证码

    gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展, windows系统下需要在php.ini中将ex ...

  4. GD库处理图像

    在PHP5中,动态图象的处理要比以前容易得多.PHP5在php.ini文件中包含了GD扩展包,只需去掉GD扩展包的相应注释就可以正常使用了.PHP5包含的GD库正是升级的GD2库,其中包含支持真彩图像 ...

  5. PHP利用GD库绘图和生成验证码图片

    首先得确定php.ini设置有没有打开GD扩展功能,測试例如以下 print_r(gd_info()); 假设有打印出内容例如以下,则说明GD功能有打开: Array ( [GD Version] = ...

  6. 利用pil库处理图像

    1关于PIL PIL(Python Image Library)是python的第三方图像处理库,但是由于其强大的功能与众多的使用人数,几乎已经被认为是python官方图像处理库了. 2PIL的主要功 ...

  7. PHP学习笔记:利用gd库给图片打图片水印

    <?php $dst_path = '1.jpg';//目标图片 $src_path = 'logo1.png';//水印图片 //创建图片的实例 $dst = imagecreatefroms ...

  8. PHP利用GD库画曲线

    效果: PHP代码 <?php Header('Content-type: image/png;Charset:utf-8'); //声明图片 $im = imagecreate(400,200 ...

  9. php 利用Gd库添加文字水印乱码的问题及解决方案

    最近一个项目进行了服务器迁移,部署后发现 ,其中一个为图片添加水印文字的功能出现了乱码问题,确认功能代码不存在问题,同时项目代码都是使用UTF-8编码,不存在编码问题,也检查排除了字体文件出现问题的可 ...

随机推荐

  1. http://blog.csdn.net/a491057947/article/details/46724707

    http://blog.csdn.net/a491057947/article/details/46724707

  2. Redis 安装为Window服务

    参考: http://www.cnblogs.com/zhoub/p/3841222.html 关键命令 redis-server.exe –service-install conf/redis.co ...

  3. node.js文件系统

    FS文件系统 第一次用这个的时候非常爽,可以在代码层编写“增删改查”文件的功能.分解里面提供的方法,接下来我们从头创建开始,最后删除文件结束,一套流程接下fs文件系统的神奇. 要使用fs文件系统就要引 ...

  4. ural 1255. Graveyard of the Cosa Nostra

    1255. Graveyard of the Cosa Nostra Time limit: 1.0 secondMemory limit: 64 MB There is a custom among ...

  5. iOS学习05C语言函数

    本次主要是学习和理解函数,函数树状图如下: 1.函数的声明和定义 函数定义的四要素分别为: 返回值类型 :函数的结果值类型,函数不能返回数组. 指定返回类型是void类型说明函数没有返回值. 函数名 ...

  6. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

  7. Linux进程间通信:IPC对象——信号灯集详解

    作者:倪老师,华清远见嵌入式学院讲师. 一.信号灯概述 信号灯与其他进程间通信方式不大相同,它主要提供对进程间共享资源访问控制机制.相当于内存中的标志,进程可以根据它判定是否能够访问某些共享资源,同时 ...

  8. Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误

    解决方案如下:将tomcat下的server.xml文件中的端口有问题,修改规则按以下标准显示“http的端口修改为6000 to 6800之间,shutdown的端口修改为3000 to 3300之 ...

  9. CentOS上安装RabbitMQ3.6.X

    RabbitMQ3.6.1的安装方法跟以前的版本有点不一样,我在网上找了很多资料,基本都是3.1左右的版本,而且安装过程很繁琐,所以我花了一下午 的时间研究如何实现最简安装.为了让大家少走弯路,就把安 ...

  10. win7 64位DCOM配置(关于导出excel 配置计算机组件服务)(转)

    http://blog.sina.com.cn/s/blog_9323b3a50101qrxm.html [解决方案1] 1:在服务器上安装office的Excel软件. 2:在"开始&qu ...