PHP——图像处理

PHP不仅局限于处理文本数据,还可以创建不同格式的动态图像

*还可以通过GD库处理图像,都是在先以内存处理,处理完后再以文件流的形式输出到浏览器或保存在务器的磁盘中

1.创建图像
imagecreatetruecolor( $width , $height ); //创建一个真彩色图像

2.绘制图像
imagecolorallocate( resource image, R,G,B) //分配颜色
imgefill(resource img, x ,y ,$color); //区域填充,x,y为坐标,图像左上角为(0,0)

3.输出图像
imagejpeg( ); //输出到浏览器上
header (); //告诉浏览器返回的是一张图片, 注意 :在header函数之前,不能输出任何内容

4.释放资源
imagedestroy(); //销毁一个图像

<?php
header('content-type:image/jpeg');
$img = imagecreateturecolor( 200,200); //创建一个长和高都为200的真彩色图像
$color1 = imagecolorallocate( $img ,50,50,50);   // 分配颜色 
imagefill( $img ,0,0, $color1);   //从(0,0)坐标开始填充
imagejpeg($img);
imagedestroy($img);

图像处理——制作验证码

<?php
header('cont-type:image/jpeg');
$width = 120;
$height = 40;
$element= arry('a','b','c','d','f','g','h','i','j','k','l');
//设置一个数组,包括从A到Z的字符

$string=' ';   //初始化string
for($i=0;$i<4;$i++)
{
	$string=$element[rand(0,count($element-1)];
}
//用for循环,执行随机随机字母,共生成4个字符

$img = imagecreatetruecolor($width,$height);

$color_bg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
//rand() ,设置一个随机数,我们这里设置的是RGB的取值范围,让图片呈现一个动态颜色变化

$color_border=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));

imagefill($img,0,0,$color_bg );
imagerectangle($img,0,0,$width-1,$height-1,$color_border);
//imagerectangle是设置矩形边框,前面两位数据是矩形左上角的坐标,后两位数据是右下角的坐标

for($i=0;$i,100;$i++){
		imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
}
//画一个点,前两个数据是点的位置,后面的数据是分配给点的颜色。用了for循环,执行了随机生成了100个点

for(i=0;i<3;i++){
	imageline($image,rand(0,$width/2),rand(0,$height/2,rand($width/2,$width),rand($height/2,$height),imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
}
//imageline,画一条线,前面四是个数据同上,是坐标信息。最后一个是颜色,for循环执行了3次

$color_string=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));

//imagestring($img,5,0,0,'abcdef',$color_string);
//imagestring( 作用对象,字的大小,x,y[字符串的左上角坐标],字符串,颜色) , 是用来创建字符

imagettftext($img,14,0,rand(5,15),rand(20,35),$color_string, 'font/SketchyComic.ttf',$string);
//imagettftext ,有很多字体
//格式为  ( 作用对象,字体的大小,字体倾斜的角度,x,y[字符串的左 下角坐标],字符串,颜色,字体路径,内容)

imagejpeg($img);
imagedestroy($img);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值