标签:gd 库 验证码 php
画图简单介绍:
<?php
header("Content-type: text/html; charset=utf-8");
//print_r(gd_info()); gd开启与否库验证
/**********创建资源(画布大小)**********/
$width=200; //往右
$height=200;//往下
$file=‘./image/boy.jpeg‘;
//创建空画布
$img = imagecreatetruecolor($width,$height);
//引入图片作为画布imagecreatefromjpeg imagecreatefrompng imagecreatefromgif
//$img = imagecreatefromjpeg($file);
//print_r($img);die;
/********设置画布颜色*******/
//imagecolorallocate(资源,红,绿,蓝); rgb
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$blue=imagecolorallocate($img,0,0,255);
$green=imagecolorallocate($img,0,255,0);
/****填充画布 imagefill(资源,填充起始点x值,填充起始点y值,$red)****/
imagefill($img,0,0,$red);
/*画点*/
//imagesetpixel(资源,起始x点值,起始y点值,结尾x点值,结尾y点值,线段颜色)
imagesetpixel($img,30,30,60,60,$blue);
/*画矩形*/
//imagerectangle(资源,起始x点值,起始y点值,结尾x点值,结尾y点值,线段颜色)
imagerectangle($img,30,30,60,60,$blue);
//填充的矩形
imagefilledrectangle($img,10,10,30,30,$blue);
/*椭圆 圆*/
//imageellipse(资源,图像左上角x,y,宽度,高度,颜色)
//填充 imagefilledellipse
imageellipse($img,70,70,20,20,$blue);
/*椭圆圆弧*/
//imagearc(资源,图像左上角x,y,宽度,高度,起始点,结束点,颜色,样式)
imagearc($img,0,0,40,40,10 ,80,$green);
/*写字*/
//imagestring(资源,字体大小1-5,,字符最左上角x坐标,y坐标,要写的字符串,颜色)
//imagestringup 垂直
imagestring($img,5,50,50,‘hello‘,$green);
/*中文*/
//imagettftext(资源,尺寸,角度,x,y,颜色,字体文件,中文) 需要字体支持
$str=‘你好‘;
//中文字符在范围 0x4E00-0x9FA0
//imagettftext($img,20,0,10,25,$blue,‘./MSYH.TTF‘,$str);
/*画线*/
//imageline(资源,起始x点值,起始y点值,结尾x点值,结尾y点值,线段颜色)
imageline($img,0,0,100,100,$red);
imageline($img,0,100,100,0,$red);
/******输出给浏览器*******/
header("Content-type: image/jpeg;");
imagejpeg($img);
/******保存*******/
//imagepng imagejpeg imagegif
echo imagejpeg($img,‘image/a.jpeg‘)? ‘图片生成成功‘: ‘生成失败‘;
//释放资源
imagedestroy($img);验证码:
<?php
header("Content-type: text/html; charset=utf-8");
class verifyCode{
private $width;//验证码宽度
private $height;//验证码高度
private $num;//验证码个数
private $code;//验证码字符串
private $img;// 图像资源
function __construct ($width=80,$height=30,$num=4)
{
$this->width=$width;
$this->height=$height;
$this->num=$num;
$this->code=$this->_setCode();
}
function getCode()
{
return $this->code;
}
function entry()
{
$this->_setBack();
$this->_setString();
$this->_setPoint();
$this->_setLine();
$this->_outImg();
}
/*设置画布 及背景*/
private function _setBack()
{
$this->img=imagecreatetruecolor($this->width,$this->height);
//背景色
$bgcolor=$this->_setColor(‘light‘);
imagefill($this->img,0,0,$bgcolor);
//边框颜色
$bordercolor = $this->_setColor(‘dark‘);
imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$bordercolor);
}
/*设置验证码数据*/
private function _setCode()
{
return substr(str_shuffle(‘qwertyuipasdfghjkzxcvbnmQWERTYUIPASDFGHJKZXCVBNM23456789‘),0,$this->num);
}
/*将验证码数据画到图上*/
private function _setString()
{
for($i=0;$i<$this->num;$i++){
$fontsize=mt_rand(2,5);
$x=3+($this->width/$this->num)*$i;
imagestring($this->img,$fontsize,$x,mt_rand(5,$this->height*0.5), $this->code{$i},$this->_setColor(‘dark‘));
}
}
/*输出图像*/
private function _outImg()
{
if (imagetypes() & IMG_PNG)
header("Content-type: image/png;");
elseif (imagetypes() & IMG_JPG)
header("Content-type: image/jpg;");
elseif (imagetypes() & IMG_GIF)
header("Content-type: image/gif;");
else
die(‘no image supper‘);
imagepng($this->img);
}
/*设置干扰点*/
private function _setPoint($num=50)
{
for($i=0;$i<$num;$i++)
{
imagesetpixel($this->img,mt_rand(1,$this->width-1),mt_rand(1,$this->height-1),$this->_setColor());
}
}
/*设置干扰线*/
private function _setLine($num=4)
{
for($i=0;$i<$num;$i++)
{
imagearc($this->img,mt_rand(-10,$this->width),mt_rand(-10,$this->height),mt_rand(20,100),mt_rand(30,300),55,44,$this->_setColor());
}
}
private function _setColor($type=‘‘)
{
$color=‘‘;
switch($type){
case ‘red‘:
$color=imagecolorallocate($this->img,255,0,0);
break;
case ‘green‘:
$color=imagecolorallocate($this->img,0,255,0);
break;
case ‘blue‘:
$color=imagecolorallocate($this->img,0,0,255);
break;
case ‘black‘:
$color=imagecolorallocate($this->img,0,0,0);
break;
case ‘white‘:
$color=imagecolorallocate($this->img,255,255,255);
break;
case ‘light‘:
$color=imagecolorallocate($this->img,mt_rand(150,255),mt_rand(150,255),mt_rand(150,255));
break;
case ‘dark‘:
$color=imagecolorallocate($this->img,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
break;
default:
$color= imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
break;
}
return $color;
}
/*资源销毁*/
function __destruct ()
{
imagedestroy($this->img);
}
}
$v=new verifyCode(100,30,4);
$v->entry();缩放:
<?php
header("Content-type: text/html; charset=utf-8");
function thumb ($img, $width, $height)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
$imgdst = imagecreatetruecolor($width,$height);
//等比例缩放
if($width && ($widthsrc < $heightsrc))
$width=($height/$heightsrc)*$widthsrc;
else
$height=($width/$widthsrc)*$heightsrc;
//imagecopyresampled 目标图片 原图片 目标开始x 目标开始y 目标结束x 目标结束y 目标缩放x 目标缩放y 原结束x 原结束y
imagecopyresampled($imgdst,$imgsrc,0,0,0,0,$width,$height,$widthsrc,$heightsrc);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img); $dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($imgdst, $newname);
imagedestroy($imgsrc);
imagedestroy($imgdst);
}
thumb(‘image/boy.jpeg‘, 100,100);剪切:
<?php
header("Content-type: text/html; charset=utf-8");
function cut ($img, $x,$y,$width, $height)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
$imgdst = imagecreatetruecolor($width,$height);
//imagecopyresampled 目标图片 原图片 目标开始x 目标开始y 目标结束x 目标结束y 目标缩放x 目标缩放y 原结束x 原结束y
imagecopyresampled($imgdst,$imgsrc,0,0,$x,$y,$width,$height,$widthsrc,$heightsrc);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img); $dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($imgdst, $newname);
imagedestroy($imgsrc);
imagedestroy($imgdst);
}
cut(‘image/boy.jpeg‘, 50,50,100,100);水印:
<?php
header("Content-type: text/html; charset=utf-8");
function watermark ($img, $waterpic)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
list($widthwater, $heightwater, $typewater) = getimagesize($waterpic);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
//原图片
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
//水印图片
$createwaterimage = ‘imagecreatefrom‘ . $types[$typewater];
$imgwater = $createwaterimage($waterpic);
$x = mt_rand(3,$widthsrc-$widthwater);
$y = mt_rand(3,$heightsrc-$heightwater);
//imagecopy 目标图/原图 水印图 目标x 目标y 水印x 水印y 水印x结束 水印y结束
imagecopy($imgsrc,$imgwater,$x,$y,0,0,$widthwater,$heightwater);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img); $dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($imgsrc, $newname);
imagedestroy($imgsrc);
imagedestroy($imgwater);
}
watermark(‘image/kuli.png‘, ‘image/a.png‘);旋转:
<?php
header("Content-type: text/html; charset=utf-8");
function rotate ($img, $degree)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
//原图片
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
//imagerotate 原图 角度 旋转后出来部分的颜色
$newpic = imagerotate($imgsrc, $degree,0);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img);
$dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($newpic, $newname);
imagedestroy($imgsrc);
imagedestroy($newpic);
}
rotate(‘image/kuli.png‘, ‘30‘);翻转:
<?php
header("Content-type: text/html; charset=utf-8");
/*按照y轴反转*/
function reversal_y ($img, $degree)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
//原图片
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
$newpic = imagecreatetruecolor($widthsrc,$heightsrc);
for($i=0;$i<$widthsrc;$i++)
imagecopy($newpic,$imgsrc,$widthsrc-$i-1,0,$i,0,1,$heightsrc);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img);
$dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($newpic, $newname);
imagedestroy($imgsrc);
imagedestroy($newpic);
}
reversal_y(‘image/boy.jpeg‘, ‘30‘);
/*按照x轴反转*/
function reversal_x ($img, $degree)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
//原图片
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
$newpic = imagecreatetruecolor($widthsrc,$heightsrc);
for($i=0;$i<$heightsrc;$i++)
imagecopy($newpic,$imgsrc,0,$heightsrc-$i-1,0,$i,$widthsrc,1);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img);
$dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($newpic, $newname);
imagedestroy($imgsrc);
imagedestroy($newpic);
}
reversal_x(‘image/boy.jpeg‘, ‘30‘);图片添加文字:
<?php
header("Content-type: text/html; charset=utf-8");
function watermark ($img, $string)
{
list($widthsrc, $heightsrc, $type) = getimagesize($img);
$types = array(1 => ‘gif‘, 2 => ‘jpeg‘, 3 => ‘png‘);
$createimage = ‘imagecreatefrom‘ . $types[$type];
$imgsrc = $createimage($img);
$white=imagecolorallocate($imgsrc,255,255,255);
$green=imagecolorallocate($imgsrc,0,255,0);
$x = mt_rand(3,$widthsrc-strlen($string)*imagefontwidth(5));
$y = mt_rand(3,$heightsrc-imagefontheight(5)-2);
imagestring($imgsrc,5,$x,$y,$string,$white);
imagestring($imgsrc,5,$x+1,$y+1,$string,$green);
//保存文件名
$save = "image" . $types[$type];
$basename = basename($img); $dirname = dirname($img);
if ($dirname == ‘.‘)
$newname = $basename;
else
$newname = $dirname . ‘/new_‘ . $basename;
$save($imgsrc, $newname);
imagedestroy($imgsrc);
}
watermark(‘image/boy.jpeg‘, ‘hello‘);标签:gd 库 验证码 php
原文地址:http://marvin89.blog.51cto.com/9163436/1789291