您的位置:首页 > 编程语言 > Go语言

pdo文字水印类,验证码类,缩略图类,logo类

2015-06-26 23:03 531 查看
文字水印类image.class.php
<?php

/**

*webrx.cnqq:7031633

*@authorwebrx

*@copyrightcopyright(c)2003-2014webrxInc.(http://www.webrx.cn)

*@version2014-9-7webrx@126.com

*/

classImage{

private$f;

private$i;

private$path;

publicfunction__construct($i=''){

$this->f=dirname(__FILE__).'/f.ttf';

if(empty($i)){

}else{

$this->i=imagecreatefromjpeg($i);

$this->path=$i;

}

}

publicfunctioncheck($len=4){

session_start();

header('content-type:image/png');

$fs=[

'/a.ttf',

'/b.ttf',

'/f.ttf'

];

$font=dirname(__FILE__).$fs[mt_rand(0,1)];

$w=35*$len;

$h=50;

$this->i=imagecreatetruecolor($w,$h);

$c=imagecolorallocatealpha($this->i,0,0,0,127);

//imagecolortransparent($i,$c);

//imagefill($i,0,0,$c);

imagefilledrectangle($this->i,0,0,$w,$h,$this->gc($this->i,'ffffff',mt_rand(0,2)));

$sss='';

for($j=0;$j<$len;$j++){

$st=$this->gs(1);

$sss.=$st;

imagettftext($this->i,mt_rand(15,25),mt_rand(-30,30),$j*35+10,mt_rand(28,38),$this->gc($this->i),$font,$st);

}

$_SESSION['code']=$sss;

imagesetthickness($this->i,mt_rand(2,8));

for($j=0;$j<mt_rand(5,10);$j++){

imagefilledarc($this->i,mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,360),mt_rand(0,360),$this->gc($this->i,'rand',mt_rand(100,120)),IMG_ARC_NOFILL);

}

for($j=0;$j<10;$j++){

imagettftext($this->i,mt_rand(10,15),mt_rand(-5,5),mt_rand(0,$w),mt_rand(0,$h),$this->gc($this->i,'rand',mt_rand(100,120)),$font,$this->gs(1));

}

imagepng($this->i);

}

publicfunctiongs($n=4){

$s='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$t='';

for($i=0;$i<$n;$i++){

$t.=substr($s,mt_rand(0,strlen($s)-1),1);

}

return$t;

}

publicfunctionsetImg($i){

$this->i=imagecreatefromjpeg($i);

$this->path=$i;

}

publicfunctiontext($s=30,$t='版权所有',$c='rand',$a=0,$p=5,$f=true,$fn='t_'){

$ii=getimagesize($this->path);

if($ii[2]==2){

if($ii[0]>300){

$pos=imagettfbbox($s,0,$this->f,$t);

$pad=30;

switch($p){

case1://左上角

$x=0-$pos[0]+$pad;

$y=0-$pos[7]+$pad;

break;

case2://上边水平中央

$x=($ii[0]-$pos[2])/2;

$y=0-$pos[7]+$pad;

break;

case3:

$x=$ii[0]-$pos[2]-$pad;

$y=0-$pos[7]+$pad;

break;

case4:

$x=0-$pos[0]+$pad;

$y=($ii[1]-$pos[6])/2;

break;

case5:

$x=($ii[0]-$pos[2])/2;

$y=($ii[1]-$pos[6])/2;

break;

case6:

$x=$ii[0]-$pos[2]-$pad;

$y=($ii[1]-$pos[6])/2;

break;


case7:

$x=0-$pos[0]+$pad;

$y=$ii[1]-$pos[6]-$pad;

break;


case8:

$x=($ii[0]-$pos[2])/2;

$y=$ii[1]-$pos[6]-$pad;

break;


case9:

$x=$ii[0]-$pos[2]-$pad;

$y=$ii[1]-$pos[6]-$pad;

break;

}

imagettftext($this->i,$s,0,$x,$y,$this->gc($this->i,$c,$a),$this->f,$t);


if($f){

imagejpeg($this->i,$this->path);

}else{

$path=dirname($this->path).'/';

$name=$fn.substr($this->path,strrpos($this->path,'/')+1);

imagejpeg($this->i,$path.$name);

}

}

}

}

publicfunctionlogo($p=5,$f=true,$fn='logo_'){

$ii=getimagesize($this->path);

if($ii[2]==2){

if($ii[0]>300){

$w=$ii[0];

$h=$ii[1];


//水银图标logo.png格式

$logo=dirname(__FILE__).'/logo.png';

$li=imagecreatefrompng($logo);

$liw=imagesx($li);

$lih=imagesy($li);


$x=($w-$liw)/2;

$y=($h-$lih)/2;


$pad=35;

switch($p){

case1:

$x=0+$pad;

$y=0+$pad;

break;


case2:

$y=0+$pad;

break;


case3:

$x=$w-$liw-$pad;

$y=0+$pad;

break;


case4:

$x=0+$pad;

break;


case6:

$x=$w-$liw-$pad;

break;


case7:

$x=0+$pad;

$y=$h-$lih-$pad;

break;


case8:

$y=$h-$lih-$pad;

break;


case9:

$x=$w-$liw-$pad;

$y=$h-$lih-$pad;

break;

}

imagecopy($this->i,$li,$x,$y,0,0,$liw,$lih);

if($f){

imagejpeg($this->i,$this->path);

}else{

$path=dirname($this->path).'/';

$name=$fn.substr($this->path,strrpos($this->path,'/')+1);

imagejpeg($this->i,$path.$name);

}

imagedestroy($li);

}

}

}

publicfunctionthumb($f=false,$w=220,$h=0,$fn='s_'){

$ii=getimagesize($this->path);

if($ii[2]==2){

if($ii[0]>$w){

$sw=$ii[0];

$sh=$ii[1];

$h=$h==0?$w/$sw*$sh:$h;

$dst=imagecreatetruecolor($w,$h);

imagecopyresampled($dst,$this->i,0,0,0,0,$w,$h,$sw,$sh);

if($f){

imagejpeg($dst,$this->path);

}else{

$path=dirname($this->path).'/';

$name=$fn.substr($this->path,strrpos($this->path,'/')+1);

imagejpeg($dst,$path.$name);

}

imagedestroy($dst);

}

}

}

publicfunctiongc($i,$c='rand',$a=0){

$color='';

switch($c){

case'white':

$color=imagecolorallocatealpha($i,255,255,255,$a);

break;

case'black':

$color=imagecolorallocatealpha($i,0,0,0,$a);

break;

case'red':

$color=imagecolorallocatealpha($i,255,0,0,$a);

break;

case'green':

$color=imagecolorallocatealpha($i,0,255,0,$a);

break;

case'rand':

$color=imagecolorallocatealpha($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255),$a);

break;

default:

$cc=str_split($c,2);

$color=imagecolorallocatealpha($i,hexdec($cc[0]),hexdec($cc[1]),hexdec($cc[2]),$a);

break;

}

return$color;

}

publicfunction__destruct(){

if(isset($this->i)){

imagedestroy($this->i);

}

}

}

[/code]类的调用及使用
<?php

include'inc/Image.class.php';

$i=newImage('c:/a.jpg');

$i->text();



[/code]给多张图片添加水印文字
<?php

include'inc/Image.class.php';

$i=newImage('c:/a.jpg');

$i->text();


$i->setImg('c:/b.jpg');

$i->text(50);


$i->setImg('c:/c.jpg');

$i->text(100);


[/code]验证码的调用
<?php

include'inc/Image.class.php';

$i=newImage();

$i->check(4);



[/code]缩略图类的调用,如果写200,100表示固定宽高,只写200,高度自适应,
<?php

include'inc/Image.class.php';

$i=newImage();

$i->setImg('c:/d.jpg');

$i->thumb(false,200);

[/code]logo图片放在图上的调用
<?php

include'inc/Image.class.php';

$i=newImage();

$i->setImg('c:/d.jpg');

$i->logo();



[/code]
<?php

include'inc/Image.class.php';

$i=newImage();

$i->setImg('c:/d.jpg');

$i->logo(5,false);//5表示位置,false表示新建一张图片

[/code]

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: