使用 PHP 图像处理函数,需要加载 GD 支持库。
<?php
//1. 创建画布;
$image = imagecreatetruecolor(600,600);
//2. 创建颜色;
$red = imagecolorallocate($image,255,0,0);
$green = imagecolorallocate($image,0,255,0);
$blue = imagecolorallocate($image,0,0,255);
//3. 用GD库的函数画画;
imageline($image,0,0,600,600,$blue); //画一条线段
imageellipse($image,50,50,100,100,$red); //画一个椭圆
//4. 告诉浏览器你的mime类型;
header("Content-type:image/png");
//5. 输出到浏览器或者可以存放到你的本地;
imagepng($image);
//6. 销毁资源。
imagedestroy($image);
?>
<?php
verify();
//验证码函数
//宽、高、字母、数字、字母数字混合、干扰线、干扰点、背景色、字体颜色
function verify($width=150,$height=40,$num=5,$type=3)
{
//准备画布
$image = imagecreatetruecolor($width,$height);
//配置颜色
//$red = imagecolorallocate($image,mt_rand(200,255),0,0);
//$green = imagecolorallocate($image,0,mt_rand(200,255),0);
//$blue = imagecolorallocate($image,0,0,mt_rand(200,255));
//设置需要的字符
$string = '';
switch($type)
{
case 1:
$str = '0123456789';
$string = substr(str_shuffle($str),0,$num);
break;
case 2:
$arr = range('a','z');
shuffle($arr);
$tmp = array_slice($arr,0,$num);
$string = join('',$tmp);
break;
case 3:
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = substr(str_shuffle($str),0,$num);
break;
}
//给图片填充背景色
imagefilledrectangle($image,0,0,$width,$height,lightcolor($image));
//开始写字
for($i=0;$i<$num;$i++)
{
$x = floor($width / $num * $i + 10);
$y = mt_rand(10,$height - 20);
imagechar($image,6,$x,$y,$string[$i],deepcolor($image));
}
//干扰线
for($i=0;$i<$num;$i++)
{
imagearc($image,mt_rand(10,$width),mt_rand(10,$height),mt_rand(10,$width),mt_rand(10,$height),mt_rand(0,10),mt_rand(0,270),deepcolor($image));
}
//干扰点
for($i=0;$i<100;$i++)
{
imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),deepcolor($image));
}
//指定输出类型
header('Content-type:image/png');
//准备输出的图片
imagepng($image);
//销毁资源
imagedestroy($image);
return $string;
}
//浅色
function lightcolor($image)
{
return imagecolorallocate($image,mt_rand(125,255),mt_rand(125,255),mt_rand(125,255));
}
//深色
function deepcolor($image)
{
return imagecolorallocate($image,mt_rand(0,124),mt_rand(0,124),mt_rand(0,124));
}
?>
<?php
addwater($source='picture.jpg');
function addwater($source,$water = 'water.gif',$area = 9,$alpha = 100,$type = 'jpg',$path = 'test',$isRandName = True)
{
//打开图片资源
$sourceRes = open($source);
$waterRes = open($water);
//获取图片大小
$sourcesize = getimagesize($source);
$watersize = getimagesize($water);
//计算水印位置
switch($area)
{
case 1:
$x = 0;
$y = 0;
break;
case 2:
$x = ($sourcesize[0] - $watersize[0])/2;
$y = 0;
break;
case 3:
$x = $sourcesize[0] - $watersize[0];
$y = 0;
break;
case 4:
$x = 0;
$y = ($sourcesize[1] - $watersize[1])/2;
break;
case 5:
$x = ($sourcesize[0] - $watersize[0])/2;
$y = ($sourcesize[1] - $watersize[1])/2;
break;
case 6:
$x = $sourcesize[0] - $watersize[0];
$y = ($sourcesize[1] - $watersize[1])/2;
break;
case 7:
$x = 0;
$y = ($sourcesize[1] - $watersize[1]);
break;
case 8:
$x = ($sourcesize[0] - $watersize[0])/2;
$y = ($sourcesize[1] - $watersize[1]);
break;
case 9:
$x = $sourcesize[0] - $watersize[0];
$y = ($sourcesize[1] - $watersize[1]);
break;
default:
$x = mt_rand(0,$sourcesize[0] - $watersize[0]);
$y = mt_rand(0,$sourcesize[1] - $watersize[1]);
break;
}
imagecopymerge($sourceRes,$waterRes,$x,$y,0,0,$watersize[0],$watersize[1],$alpha);
//文件格式
if($type = 'jpg')
{
$type = 'jpeg';
}
$func = 'image'.$type; /*imagepng(),imagejpeg(),imagegif()*/
//处理path路径,是否启用随机文件名
if($isRandName)
{
$name = uniqid().'.'.$type;
}
else
{
$pathinfo = pathinfo($source);
$name = $pathinfo['filename'].'.'.$type;
}
//输出资源到指定路径
$path = './'.rtrim($path,'/').'/'.$name;
$func($sourceRes,$path);
//释放资源
imagedestroy($sourceRes);
imagedestroy($waterRes);
}
//打开图片函数定义
function open($path)
{
//判断文件是否存在
if(!file_exists($path))
{
exit('抱歉,没有找到该文件!');
}
$info = getimagesize($path);
switch ($info['mime'])
{
case 'image/png':
$res = imagecreatefrompng($path);
break;
case 'image/jpg':
case 'image/pjpeg':
case 'image/jpeg':
$res = imagecreatefromjpeg($path);
break;
case 'image/gif':
$res = imagecreatefromgif($path);
break;
case 'imag/wbmp':
case 'image/bmp':
$res = imagecreatefromwbmp($path);
break;
}
return $res;
}
?>
imagecreatetruecolor() //创建一个画布
imagecolorallocate() //为一副图像分配颜色
imagefill() //填充
imageline() //画一个线段
imagerotate() //旋转
输出
imagepng(资源,路径) //输出(资源)
imagejpeg
imagegif
imagewbmp
形状
imagerectangle //矩形
imagefilledrectangle //画一个矩形并且填充
imagesetpixel //画像素
imagepolygon //多边形
imagefilledpolygon //画一个多边形并填充
imageellipse //椭圆
imagefilledarc //饼状图
打开图片生成一个新的图片
imagecreatefromjpeg
imagecreatefromgif
imagecreatefromwbmp
imagecreatefrompng
文字
imagestring //水平地画一行字符串(不支持中文)
imagettftext //可以写汉字(资源,字体大小,角度,x,y,颜色,样式,字符串)
原文:https://www.cnblogs.com/k1ng/p/12382471.html