function Watermark_IM($preview = 0) {
global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
$watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
switch($watermarkstatus) {
case 1:
$gravity = 'NorthWest';
break;
case 2:
$gravity = 'North';
break;
case 3:
$gravity = 'NorthEast';
break;
case 4:
$gravity = 'West';
break;
case 5:
$gravity = 'Center';
break;
case 6:
$gravity = 'East';
break;
case 7:
$gravity = 'SouthWest';
break;
case 8:
$gravity = 'South';
break;
case 9:
$gravity = 'SouthEast';
break;
}
$targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
if($watermarktype < 2) {
$watermark_file = $watermarktype == 1 ? DISCUZ_ROOT.'./images/common/watermark.png' : DISCUZ_ROOT.'./images/common/watermark.gif';
$exec_str = $imageimpath.'/composite'.
($watermarktype != 1 && $watermarktrans != '100' ? ' -watermark '.$watermarktrans.'%' : '').
' -quality '.$watermarkquality.
' -gravity '.$gravity.
' '.$watermark_file.' '.$this->targetfile.' '.$targetfile;
} else {
$watermarktextcvt = str_replace(array("n", "r", "'"), array('', '', '''), pack("H*", $watermarktext['text']));
$watermarktext['angle'] = -$watermarktext['angle'];
$translate = $watermarktext['translatex'] || $watermarktext['translatey'] ? ' translate '.$watermarktext['translatex'].','.$watermarktext['translatey'] : '';
$skewX = $watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
$skewY = $watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
$exec_str = $imageimpath.'/convert'.
' -quality '.$watermarkquality.
' -font "'.$watermarktext['fontpath'].'"'.
' -pointsize '.$watermarktext['size'].
(($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor'] ?
' -fill "rgb('.$watermarktext['shadowcolor'].')"'.
' -draw "'.
' gravity '.$gravity.$translate.$skewX.$skewY.
' rotate '.$watermarktext['angle'].
' text '.$watermarktext['shadowx'].','.$watermarktext['shadowy'].' ''.$watermarktextcvt.''"' : '').
' -fill "rgb('.$watermarktext['color'].')"'.
' -draw "'.
' gravity '.$gravity.$translate.$skewX.$skewY.
' rotate '.$watermarktext['angle'].
' text 0,0 ''.$watermarktextcvt.''"'.
' '.$this->targetfile.' '.$targetfile;
}
@exec($exec_str, $output, $return);
if(empty($return) && empty($output)) {
$this->attach['size'] = filesize($this->targetfile);
}
}
function Thumb_IM($thumbwidth, $thumbheight, $preview = 0) {
global $thumbstatus, $imageimpath, $thumbquality;
if($thumbstatus) {
list($img_w, $img_h) = $this->attachinfo;
$targetfile = !$preview ? ($thumbstatus == 1 || $thumbstatus == 3 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
if($thumbstatus != 3) {
$exec_str = $imageimpath.'/convert -quality '.intval($thumbquality).' -geometry '.$thumbwidth.'x'.$thumbheight.' '.$this->targetfile.' '.$targetfile;
@exec($exec_str, $output, $return);
if(empty($return) && empty($output)) {
$this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
}
} else {
$imgratio = $img_w / $img_h;
$thumbratio = $thumbwidth / $thumbheight;
if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
$cuty = $img_h;
$cutx = $cuty * $thumbratio;
} elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio < $thumbratio) {
$cutx = $img_w;
$cuty = $cutx / $thumbratio;
}
$exec_str = $imageimpath.'/convert -crop '.$cutx.'x'.$cuty.'+0+0 '.$this->targetfile.' '.$targetfile;
@exec($exec_str, $output, $return);
$exec_str = $imageimpath.'/convert -quality '.intval($thumbquality).' -geometry '.$thumbwidth.'x'.$thumbheight.' '.$targetfile.' '.$targetfile;
@exec($exec_str, $output, $return);
if(empty($return) && empty($output)) {
$this->attach['thumb'] = $thumbstatus == 1 || $thumbstatus == 3 ? 1 : 0;
}
}
}
}
}
<?php php 验证码图片 程序
/*
*文件名:class.safeCode.php
*类名:safeCode
*目的:生成web应用时所需的验证码图片
*当前版本:1.0.2
*作者:NoAngels
*联系方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com
*开发时间:2008年06月20日
*最后更新:2008年06月21日
*更新内容:
*版本1.0.2
*1.更新了对linux主机支持,设置GDFONTPATH环境变量,
* putenv('GDFONTPATH=' . realpath('.'));
*2.对不支持antialias函数的时候避免采用此方法,避免低版本的GD库
*版本1.0.1
*1.更新了绘制指定类型图片,支持类型有:png,jpeg,gif.默认为png(此效果最佳)
*2.优化了部分代码
*/
class safeCode{
function __construct(){
#设置默认safe安全保护码
$this->__safe = substr(md5(time()), 0 , 4);
}
function __destruct(){
#释放资源
unset($this->__img, $this->__canvasWidth, $this->__canvasHeight, $this->__codeLength, $this->__spacePerChar, $this->__code, $this->__safe, $this->__background, $this->__borderColor, $this->__colors, $this->__font, $this->__sessionName);
}
function setCanvas($width = 200, $height = 60){
#设置画布宽度以及高度.有默认值
$this->__canvasWidth = $width;
$this->__canvasHeight = $height;
$this->__img = imagecreatetruecolor($width, $height);
return;
}
function setSafe($char){
#设置安全码,不调用系统会自己设置
$this->__safe = $char;
return;
}
function setCodeLength($num = 8){
#设置验证码长度
$this->__codeLength = $num;
}
function setFont($fontName){
#设置绘图时所用的字体,字体文件必须与类文件同目录
$this->__font = $fontName;
return;
}
function setBackground($arg1 = 255, $arg2 = 255, $arg3 = 255){
#设置背景颜色
$this->__background = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
return;
}
function setBorderColor($arg1 = 128, $arg2 = 128, $arg3 = 128){
#设置边框颜色
$this->__borderColor = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
}
function setFontColor($arr){
#设置绘图时所用颜色,参数为颜色集合数组
foreach($arr as $color){
$this->__colors[] = imagecolorallocate($this->__img, $color[0], $color[1], $color[2]);
}
return;
}
function setSession($sessionName = 'safeCode'){
#设置session变量名,默认为$_SESSION['safeCode']
$this->__sessionName = $sessionName;
#$_SESSION[$sessionName] = $this->__code;
return;
}
function display($type = 'png'){
#显示图片,可指定显示类型,目前支持png(默认),jpeg,gif
$this->__setSpacePerChar();
imagefilledrectangle($this->__img, 1, 1, $this->__canvasWidth - 2, $this->__canvasHeight -2, $this->__background);
imagerectangle($this->__img, 0, 0, $this->__canvasWidth - 1, $this->__canvasHeight - 1, $this->__borderColor);
$this->__drawText($this->__colors, $this->__getCode(), $this->__font);
#修正linux以及低版本的GD库问题
if(function_exists(imageantialias)){
imageantialias($this->__img, true);
}
$_SESSION[$this->__sessionName] = $this->__code;
switch ($type){
case 'png':
header('Content-type:image/png');
imagepng($this->__img);
case 'jpeg':
header('Content-type:image/jpeg');
imagejpeg($this->__img, '' , 75);
case 'gif':
header('Content-type:image/gif');
imagegif($this->__img);
default:
header('Content-type:image/png');
imagepng($this->__img);
}
imagegif($this->__img);
}
private function __setSpacePerChar(){
#私有函数,设置字符间隔
$this->__spacePerChar = $this->__canvasWidth / $this->__codeLength;
return;
}
private function __getCode(){
#获取验证码
$this->__code = substr(md5(time().$this->__safe), rand(0, 24), $this->__codeLength);
return $this->__code;
}
private function __drawText($colors, $code, $fontName){
#开始绘图
#设置GBFONTPATH为当前目录,不然linux环境下会报错
putenv('GDFONTPATH=' . realpath('.'));
for($i = 0; $i < strlen($code); $i++){
$color = $colors[$i % count($colors)];
imagettftext(
$this->__img,
24 + rand(0, 8),
-20 + rand(0, 40),
($i + 0.3) * $this->__spacePerChar,
$this->__canvasHeight - 20 + rand(0, 20),
$color,
$fontName,
$code{$i}
);
}
#绘制随机实线
for($i = 0; $i < 400; $i++){
$x1 = rand(5, $this->__canvasWidth - 5);
$y1 = rand(5, $this->__canvasHeight - 5);
$x2 = $x1 - 4 + rand(0, 8);
$y2 = $y1 - 4 + rand(0, 8);
imageline($this->__img, $x1, $y1, $x2, $y2, $this->__colors[rand(0, count($this->__colors) - 1)]);
}
return;
}
private $__img = NULL;
private $__canvasWidth = 120;
private $__canvasHeight = 80;
private $__codeLength = 8;
private $__spacePerChar = 0;
private $__code = 12345678;
private $__safe = 2008;
private $__background = NULL;
private $__borderColor = NULL;
private $__colors = array();
private $__font = 'arial.ttf';
private $__sessionName = '';
}
?>
使用方法.
<?php
#测试文件
include_once('class.safeCode.php');
session_start();
$safeCode = new safeCode;
$safeCode->setCanvas(200, 50);
$safeCode->setCodeLength(8);
$safeCode->setSafe('2008');#设置安全码,不设置有默认值,动态生成
$safeCode->setBackground();
$safeCode->setBorderColor();
$safeCode->setFont('arial.ttf');#设置字体.必须同目录,字体文件
$colors = array(array(128, 64, 192), array(192, 64, 128), array(108, 192, 64));
$safeCode->setFontColor($colors);
$safeCode->setSession();
$safeCode->display('jpeg');
?>
php 生成验证码程序
<?php
$consts = 'bcdfghjkmnpqrstwxyz';
$vowels = 'aei23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1);
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1);
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //only display 4 str
/ t up image, the first number is the width and the second is the height
$imageX = strlen($radomstring)*5.5; //the image width
$imageY = 20; //the image height
$im = imagecreatetruecolor($imageX,$imageY);
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250));
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255)));
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80);
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160));
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80);
//fill image with bgcolor
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//writes string
imagestring($im, 5, 3, floor(rand(0,5))-1, $string[0], $foregroundArr[rand(0,3)]);
imagestring($im, 5, 13, floor(rand(0,5))-1, $string[1], $foregroundArr[rand(0,3)]);
imagestring($im, 5, 23, floor(rand(0,5))-1, $string[2], $foregroundArr[rand(0,3)]);
imagestring($im, 5, 33, floor(rand(0,5))-1, $string[3], $foregroundArr[rand(0,3)]);
for($i=0; $i<strlen($string); $i++)
{
if(mt_rand(0,ord(substr($string,$i,1)))%2 == 0)
{
$string{$i} = ' ';
}
}
imagestring($im, 2, 2, 0, $string, $foreground2);
//strikethrough
$border = imagecolorallocate($im, 133, 153, 193);
//imagefilledrectangle($aimg, 0, 0, $x_size - 1, $y_size - 1, $back);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<20;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//rotate
$middleground = imagecolorallocatealpha($im, rand(160, 200), rand(160, 200), rand(160, 200), 80);
//random shapes
for ($x=0; $x<15;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2);
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
?>
php 图片 中文验证码
<img" width=100% src="verify_image.php" alt="点此刷新验证码" name="verify_code" width="65" height="20" border="0" id="verify_code" onclick="document.getElementById('verify_code').src='verify_image.php?'+Math.random();" style="cursor:pointer;" />
<?php
session_start();
$vi = new vCodeImage;
$vi->SetImage(1,4,65,20,80,1);
class vCodeImage{
var $mode; //1:数字模式,2:字母模式,3:数字字母模式,其他:数字字母优化模式
var $v_num; //验证码个数
var $img_w; //验证码图像宽度
var $img_h; //验证码图像高度
var $int_pixel_num; //干扰像素个数
var $int_line_num; //干扰线条数
var $font_dir; //字体文件相对路径
var $border; //图像边框
var $borderColor; //图像边框颜色
function SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir='../font',$border=true,$borderColor='255,200,85'){
if(!isset($_SESSION['vCode'])){
session_register('vCode');
}
$_SESSION['vCode']="";
$this->mode = $made;
$this->v_num = $v_num;
$this->img_w = $img_w;
$this->img_h = $img_h;
$this->int_pixel_num = $int_pixel_num;
$this->int_line_num = $int_line_num;
$this->font_dir = $font_dir;
$this->border = $border;
$this->borderColor = $borderColor;
$this->GenerateImage();
}
function GetChar($mode){
if($mode == "1"){
$ychar = "0,1,2,3,4,5,6,7,8,9";
}
else if($mode == "2"){
$ychar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
}
else if($mode == "3"){
$ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
}
else
$ychar = "3,4,5,6,7,8,9,A,B,C,D,H,K,P,R,S,T,W,X,Y";
return $ychar;
}
function RandColor($rs,$re,$gs,$ge,$bs,$be){
$r = mt_rand($rs,$re);
$g = mt_rand($gs,$ge);
$b = mt_rand($bs,$be);
return array($r,$g,$b);
}
function GenerateImage(){
$im = imagecreate($this->img_w,$this->img_h);
$black = imagecolorallocate($im, 0,0,0);
$white = imagecolorallocate($im, 255,255,255);
$bgcolor = imagecolorallocate($im, 250,250,250);
imagefill($im,0,0,$bgcolor);
$fonts = ScanDir($this->font_dir);
$fmax = count($fonts) - 2;
$ychar = $this->GetChar($this->mode);
$list = explode(",",$ychar);
$x = mt_rand(2,$this->img_w/($this->v_num+2));
$cmax = count($list) - 1;
$v_code = '';
for($i=0;$i<$this->v_num;$i++) //验证码
{
$randnum = mt_rand(0,$cmax);
$this_char = $list[$randnum];
$v_code .= $this_char;
$size = mt_rand(intval($this->img_w/5),intval($this->img_w/4));
$angle = mt_rand(-20,20);
$y = mt_rand(($size+2),($this->img_h-2));
if($this->border)
$y = mt_rand(($size+3),($this->img_h-3));
$rand_color = $this->RandColor(0,200,0,100,0,250);
$randcolor = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
$fontrand = mt_rand(2, $fmax);
$font = "$this->font_dir/".$fonts[$fontrand];
imagettftext($im, $size, $angle, $x, $y, $randcolor, $font, $this_char);
$x = $x + intval($this->img_w/($this->v_num+1));
}
for($i=0;$i<$this->int_pixel_num;$i++){//干扰像素
$rand_color = $this->RandColor(50,250,0,250,50,250);
$rand_color_pixel = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
imagesetpixel($im, mt_rand()%$this->img_w, mt_rand()%$this->img_h, $rand_color_pixel);
}
for($i=0;$i<$this->int_line_num;$i++){ //干扰线
$rand_color = $this->RandColor(0,250,0,250,0,250);
$rand_color_line = imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);
imageline($im, mt_rand(0,intval($this->img_w/3)), mt_rand(0,$this->img_h), mt_rand(intval($this->img_w - ($this->img_w/3)),$this->img_w), mt_rand(0,$this->img_h), $rand_color_line);
}
if($this->border) //画出边框
{
if(preg_match("/^\d{1,3},\d{1,3},\d{1,3}$/",$this->borderColor)){
$borderColor = explode(',',$this->borderColor);
}
$border_color_line = imagecolorallocate($im,$borderColor[0],$borderColor[1],$borderColor[2]);
imageline($im, 0, 0, $this->img_w, 0, $border_color_line); //上横
imageline($im, 0, 0, 0, $this->img_h, $border_color_line); //左竖
imageline($im, 0, $this->img_h-1, $this->img_w, $this->img_h-1, $border_color_line); //下横
imageline($im, $this->img_w-1, 0, $this->img_w-1, $this->img_h, $border_color_line); //右竖
}
imageantialias($im,true); //抗锯齿
$time = time();
$_SESSION['vCode'] = $v_code."|".$time; //把验证码和生成时间负值给$_SESSION[vCode]
//生成图像给浏览器
if (function_exists("imagegif")) {
header ("Content-type: image/gif");
imagegif($im);
}
elseif (function_exists("imagepng")) {
header ("Content-type: image/png");
imagepng($im);
}
elseif (function_exists("imagejpeg")) {
header ("Content-type: image/jpeg");
imagejpeg($im, "", 80);
}
elseif (function_exists("imagewbmp")) {
header ("Content-type: image/vnd.wap.wbmp");
imagewbmp($im);
}
else
die("No Image Support On This Server !");
imagedestroy($im);
}
}
?>