首先需要在php中安装gd库 当然kaili自带的有gd库,所以不用安装了
注意事项,php处理图像时不能加入html标签 且需要在开头加上Header("Content-type: image/png")
<?php
Header("Content-type: image/png");
$im=imagecreate(800,60);//创建一个宽为200,高为60的画布
$white=imagecolorallocate($im,255,66,159);//设置画布的背景色为粉色
imagegif($im);//输出画布
?>
在图像中添加文字
<?php
Header("Content-type: image/png");//定义输出的图像类型
$im=imagecreatefromjpeg("123.png");//载入照片
$textcolor=imagecolorallocate($im,56,73,136);//设置字体颜色为蓝色
$motto=iconv("gb2312","utf-8","王梦涵老婆");//定义输出字符串
imageTTFText($im,220,0,480,340,$textcolor,$motto);//把文字写入图中
imagejpeg($im);//建立图形
imagedestory($im);//结束图形
?>
用php写生成验证码
<?php
session_start();
header("content-type:image/png");
$image_width=70;
$image_heigth=18;
srand(microtime()*100000);
for($i=0;$i<4;$i++){
$new_number.=dechex(rand(0,15));
}
$_SESSION[check_checks]=$new_number;
$num_image=imagecreate($image_width,$image_height);
imagecolorallocate($num_image,255,255,255);
for($i=0;$i<strlen($_SESSION[check_checks]);$i++){
$font=mt_rand(3,5);
$x=mt_rand(1,8)+$image_width*$i/4;
$y=mt_rand(1,$image_height/4);
$color=imagecolorallocate($num_image,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imagestring($num_image,$font,$x,$y,$_SESSION[check_checks][$i],$color);
}
imagepng($num_image);
echo $_SESSION[check_checks];
imagedestroy($num_image);
?>