<?php
#创建一个真彩画布
$img=imagecreatetruecolor(100,100) or die('图片创建真彩画布失败');
#从已有jpeg图片中打开资源
//$img=imagecreatefromjpeg('../phtotshop/logo.jpg');
//从已有gif图片中打开资源
//$gif_image=imagecreatefromgif('../phtotshop/cat.gif');
//ImageCreatefromPng(图片文件路径):打开png格式图片资源
//$png_image=imagecreatefrompng('../phtotshop/giao.png');
//var_dump($png_image);
#操作画布资源:所有的画布资源操作都是需要指定画布资源,而且都是第一个参数。
/*
* 分配颜色:imageColorAllocate(画布资源,红色,绿色,蓝色),
* 根据RGB三色组给指定画布资源分配一组颜色,
* 会返回一个颜色句柄(一组整数),
* 每一个色组都是从0到255。
*
*/
$bg_color=imagecolorallocate($img,255,0,0);
/*
* 注释:在真彩图片资源中,所有分配的颜色都不会自动给图片资源上色,
* 是用来后续操作图片资源的时候,指定着色的;
* 但是如果当前使用的imagecreate创建的图片资源,
* 那么第一个分配的颜色,会自动被着色为图片背景色。
*
* 注意:凡是给图片上增加内容,基本都需要分配颜色(每一个操作图片的函数之前,都需要先调用分配颜色的函数得到一个颜色)
*/
//填充区域:imageFill(画布资源,起始X坐标,起始Y坐标,颜色句柄):指定位置开始填充指定颜色
imagefill($img,0,0,$bg_color);
//制作直线 imageLine(图片资源,起始点X,起始点Y,终点X,终点Y,颜色),制作一条直线
$line_colir=imagecolorallocate($img,0,255,0);
imageline($img,10,10,90,90,$line_colir);
//制作矩形 imageRectangle(图片资源,左上角X,左上角Y,右下角X,右下角Y,颜色):制作一个矩形
$rec_color=imagecolorallocate($img,0,0,255);
imagerectangle($img,10,10,90,90,$rec_color);
//画圆弧 imageArc(图像资源,轴点X,轴点Y,宽度,高度,弧度起点,弧度终点,颜色)
$arc_color=imagecolorallocate($img,0,0,0);
imagearc($img,50,50,80,80,180,360,$arc_color);
//在画布上写字 :imageString(),imageTtfText()
//Imagestring (图片资源,文字大小,起始X,起始Y,内容,颜色):用来书写ASCII对应的字符(英文)
$str_color=imagecolorallocate($img,100,100,100);
imagestring($img,5,20,20,'giao',$str_color);
/*
* Imagettftext(图片资源,文字大小,旋转角度,起始X,起始Y,颜色,字体,内容):
* 可以书写任意文字(中文):需要指定字体路径(ttf文件:默认是windows/fonts/)
* (字体路径不可有中文,GD库版本低于2.0.18不可有空格)
*/
//写入中文汉字
$ch_color=imagecolorallocate($img,50,50,50);
imagettftext($img,30,45,50,50,$ch_color,'E:\LearningSoftware\phpStudy\phpstudy_pro\WWW\PHP\TTF\simfang.ttf','中');
//输出为图片文件:以图片文件形式保存在本地
//保存输出
imagepng($img,'../phtotshop/hb.png');
//输出为网页图片:将图片展示给HTML(用户):服务器告知当前内容是图片(修改响应头)
//header('Content-type:image/png');
//imagepng($img);
//销毁资源
imagedestroy($img);
?>
获取图片信息
<?php
//定义图片
$image='../phtotshop/logo.jpg';
//打开图片资源
$img=imagecreatefromjpeg($image);
//获取图片信息
echo imagesx($img);
echo imagesy($img);
echo '<pre>';
#获取图片的全部信息
$info=getimagesize($image);
print_r($info);
?>
变换验证码
<?php
$img=imagecreatetruecolor(200,50);
$bg_color=imagecolorallocate($img,220,220,220);
imagefill($img,0,0,$bg_color);
$font='E:\LearningSoftware\phpStudy\phpstudy_pro\WWW\PHP\TTF\simfang.ttf';
$str_color=imagecolorallocate($img,100,100,100);
$str='今天晚上吃什么';
$len=strlen($str);
$c_len=$len/3;
$char1=substr($str,mt_rand(0,$c_len-1)*3,3);
$char2=substr($str,mt_rand(0,$c_len-1)*3,3);
imagettftext($img,25,15,60,35,$str_color,$font,$char1);
imagettftext($img,25,-15,130,35,$str_color,$font,$char2);
header('Content-type:image/png');
imagepng($img);
imagedestroy($img);
?>
//添加噪点和干扰线
<?php
$img=imagecreatetruecolor(200,50);
$bg_color=imagecolorallocate($img,220,220,220);
imagefill($img,0,0,$bg_color);
$font='E:\LearningSoftware\phpStudy\phpstudy_pro\WWW\PHP\TTF\simfang.ttf';
//实现颜色的不同变换
$str_color=imagecolorallocate($img,mt_rand(0,130),mt_rand(0,130),mt_rand(0,130));
$str_color2=imagecolorallocate($img,mt_rand(0,13),mt_rand(0,130),mt_rand(0,130));
$str='今天晚上吃什么吃米饭吃萝卜吃大葱吃奥里给';
$len=strlen($str);
$c_len=$len/3;
$char1=substr($str,mt_rand(0,$c_len-1)*3,3);
$char2=substr($str,mt_rand(0,$c_len-1)*3,3);
imagettftext($img,25,15,60,35,$str_color,$font,$char1);
imagettftext($img,25,-15,130,35,$str_color2,$font,$char2);
//添加噪点
for($i=0;$i<80;$i++){
$dots_color=imagecolorallocate($img,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
imagestring($img,mt_rand(1,7),mt_rand(0,200),mt_rand(0,50),'.',$dots_color);
}
//添加干扰线
for ($i=0;$i<20;$i++){
$line_color=imagecolorallocate($img,mt_rand(150,250),mt_rand(150,250),mt_rand(150,250));
imageline($img,mt_rand(0,200),mt_rand(0,50),mt_rand(0,200),mt_rand(0,50),$line_color);
}
header('Content-type:image/png');
imagepng($img);
imagedestroy($img);
?>