PHP:GD函数库--绘制图像

绘制图像:

1.填充画布

imagefill();

2.画一个像素

imagesetpixel();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画点
for($i=0;$i<10000;$i++){
    imagesetpixel($img,mt_rand(0,500),mt_rand(0,300),$black);
}

// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

3.画线

imageline();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画点
for($i=0;$i<10;$i++){
    imageline($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
}

// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

4.画一个矩形

imagerectangle();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画点
for($i=0;$i<100;$i++){
    imagerectangle($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
}

// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

5.画一矩形并填充

imagefilledrectangle();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画点
// for($i=0;$i<100;$i++){
//     imagefilledrectangle($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
// }
imagefilledrectangle($img,mt_rand(0,500),mt_rand(0,300),mt_rand(0,500),mt_rand(0,300),$black);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

6.画一个多边形

imagepolygon();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
$arr=array(
    250,50,
    50,250,
    450,150,
);
imagepolygon($img,$arr,3,$black);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

7.画一个多边形并填充

imagefilledpolygon();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
$arr=array(
    250,50,
    50,250,
    450,150,
    450,50,
);
imagefilledpolygon($img,$arr,4,$green);
// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述***
多边形干扰素

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
for($i=0;$i<100;$i++){
    $arr=array(
        mt_rand(0,500),mt_rand(0,300),
        50,250,
        450,150,
        450,50,
    );
    imagepolygon($img,$arr,4,$green);
}


// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

8.画一个椭圆

imageellipse();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
for($i=0;$i<100;$i++){
    
    imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
}


// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

9.画一个椭圆并填充

imagefilledellipse();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
// for($i=0;$i<100;$i++){
    
//     imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// }

     imagefilledellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);

// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

10.画一个椭圆弧

imagearc();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
// for($i=0;$i<100;$i++){
    
//     imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// }

imagearc($img,mt_rand(0,500),mt_rand(0,500),500,300,0,120,$black);

// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

11.画一个椭圆弧并填充

imagefilledarc();


**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
// for($i=0;$i<100;$i++){
    
//     imageellipse($img,mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),mt_rand(0,500),$black);
// }

imagefilledarc($img,250,150,200,200,0,340,$blue,IMG_ARC_PIE);

imagefilledellipse($img,272,98,20,20,$yellow);

// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述***
**

<?php
// 1、创建画布资源
$img=imagecreatetruecolor(500,300);

// 2、准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3、在画布上画图像或文字
imagefill($img,0,0,$white);

// 4、画
imagefilledarc($img,250,150,200,200,0,70,$blue,IMG_ARC_PIE);
imagefilledarc($img,250,150,200,200,70,160,$red,IMG_ARC_PIE);
imagefilledarc($img,250,150,200,200,160,230,$yellow,IMG_ARC_PIE);
imagefilledarc($img,250,150,200,200,230,360,$green,IMG_ARC_PIE);


// 5、输出最终图像或保存最终图像
header('content-type:image/png');
imagejpeg($img);

// 6、释放画布资源
imagedestroy($img);
?>

在这里插入图片描述

12.画字符串

imagettftext();


**

<?php 
// 1.创建画布资源
$img=imagecreatetruecolor(500,300);

// 2.准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3.在画布上画图像或文字
imagefill($img,0,0,$white);

// 随时生产5位字符串
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$randStr=join('',array_slice($arr,0,5));
// 4.画点
$str='aXdwJ';
imagettftext($img,90,0,50,200,$blue,'D:/xampp/htdocs/php2/msyhbd.ttf',$randStr);

// 5.输出最终图像或保存最终图像
header('content-type: image/png');
imagepng($img);

// 6.释放画布资源
imagedestroy($img);
 ?>

在这里插入图片描述


生成5位随时数图片

<?php 
// 1.创建画布资源
$img=imagecreatetruecolor(500,300);

// 2.准备颜色
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);
$yellow=imagecolorallocate($img,255,255,0);

// 3.在画布上画图像或文字
imagefill($img,0,0,$white);

// 随时生产5位字符串
// mt_rand — 生成更好的随机数
// range — 根据范围创建数组,包含指定的元素 
// array_merge — 合并一个或多个数组
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
// shuffle-打乱数组
shuffle($arr);
// array_slice-从数组中取出一段
// join-将一个一维数组的值转化为字符串
$randStr=join('',array_slice($arr,0,5));
// 4.画点
$str='aXdwJ';
imagettftext($img,80,mt_rand(-30,30),50,200,$blue,'D:/xampp/htdocs/php2/msyhbd.ttf',$randStr);

// 5.输出最终图像或保存最终图像
header('content-type: image/png');
imagepng($img);

// 6.释放画布资源
imagedestroy($img);
 ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数库实例 PHP函数库,PHP函数大全,PHP函数实例,PHP函数手册,PHP5函数库实例 curl获取远程文件内容 GD显示中文 PHP GIF动生成类 PHP HTML转UBB函数 PHP XML转数组函数 PHP 缓存函数 PHP 设置COOKIE,并且加密COOKIE函数 PHP不缓存数据头 PHP伪造IP PHP全角半角转换函数 PHP农历函数 PHP分页函数 PHP判断字符串是否UTF8格式 php判断爬虫函数 PHP判断远程文件是否存在 PHP图片处理类:缩略,裁剪,圆角,倾斜 PHP多功能图片处理类 PHP多重判断删除文件函数 PHP实现英文标题的正确大写 PHP常用图片处理类 PHP常用测试函数 PHP得到当周每天日期 PHP文件下载类 PHP无限分类[左右值]算法 PHP显示日期、周几、农历初几、什么节日函数 PHP格式化数据,防止注入函数 PHP模拟登陆 PHP生成唯一标识符函数 PHP生成曲线图函数 PHP生成条形码 PHP统计字符串里单词出现次数 PHP缩略图类,可生成BMP格式 PHP自定义大小验证码函数 PHP获取.NET发出的WEBSERVICE数据 PHP获取FLV文件播放时间函数 PHP获取一年内所有周的开始和结束日期 php获取指定日期所在周的开始和结束日期 PHP读取文件前几个字节 判断文件类型函数 PHP连接ACCESS PHP采集程序中常用的函数 PHP随机产生指定长度中文字符串 SMTP类 url地址参数加密 一些常用验证函数 下拉-单选框选择 创建多级目录 删除数组中相同元素,只保留一个 判断路径是绝对目录还是相对目录 利用PHP搜索指定目录下指定的文件 加密解密 去掉指定的html标签 发送 trackback 数据包 图像处理类 图片验证码生成 字符集转换类 对要输入的字符串进行转换 对要输出的字符串进行反转换 对输入JS进行转换 寻找两个函数所有不同的元素 寻找两数组所有不同元素 得到文件类型 截取字符串中两个特定唯一字符之间的内容 截取指定长度字符串 折线图 按照比例改变图片大小(非生成缩略图) 收藏主页 数据验证类 数组转换成XML格式 日期计算 是否为电子邮件格式 柱形统计图 检查是否为一个合法的时间格式 检测URL地址有效性 检测文件是否图片 检测是否可以以网页形式显示 检测是否序列化后的字符串 模仿JAVASCRIPT的ESCAPE和UNESCAPE函数的功能 用curl函数读取远程文件 用file_getcontents提交数据 用php生成扭曲,有角度的验证图片(支持中文) 用正则加亮关键字 程序运行过程中直接输出 缩略图带版权信息函数 缩略图类 获得用户操作系统的换行符 获得用户的真实IP地址 计算字符串的长度(汉字按照两个字符计算) 设为主页 转换附件大小单位 转静态函数 遍历文件夹文件 采集网络数据 随机字符串 验证码 验证码类 验证输入的邮件地址是否合法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值