gd2我们能做什么?
图像处理:
第一:创建一个图像资源 【创建一张画布或者是打开一张现有图片】
第二:分配颜色
第三:使用图像处理函数来操作这个图片资源。【画线,画点,画饼,画矩】
第四:输出header头【告诉浏览器,这是一张图片,可选】
第五:保存图片或者输出图片
第六:销毁图像资源
输出图像的时候:
Imagepng();
Imagejpeg();
Imagegif();
Imagwbmp();
如何写字:
Imagechar
Imagecharup
Imagestring
Imagestringup
Imagettftext
GD.php
<?php
//imagecreate(); 它的画布颜色较少 256种
//
//imagecreatetruecolor(); 它的画布颜色较多 65536
$img=imagecreatetruecolor(500,500);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$pink=imagecolorallocate($img,255,102,204);
imagefilledrectangle($img,0,0,500,500,$pink);
imageline($img,0,0,500,500,$green);
imageline($img,500,0,0,500,$green);
imagefilledellipse($img,250,250,200,200,$red);
imagefilledrectangle($img,180,180,320,320,$blue);
//imagecharup($img,5,300,400,'',$green);
//imagestringup($img,5,300,400,'我愿变成童话里',$red);
imagettftext($img,50,0,0,200,$green,'STXINWEI.TTF','拼命赚钱,美貌如花');
header('Content-type:image/jpeg');
imagejpeg($img,'bcdc.jpeg');
imagedestroy($img);
?>
imgjoin.php
<?php
//将两张图片合并到一起
//imagecreatefromjpeg
//imagecreatefrompng
//imagecreatefromwbmp
//imagecreatefromgif
//
$src=imagecreatefromjpeg('zxy.jpg');
$dst=imagecreatefromjpeg('yz.jpg');
//imagecopy($dst,$src,440,512,215,25,376,446);
//imagecopymerge($dst,$src,1130,466,267,174,238,292,20);
//$newImg=imagecreatetruecolor(238,292);
//imagecopymerge($newImg,$src,0,0,267,174,238,292,100);
imagecopyresampled($dst,$src,700,704,267,187,100,100,230,281);
imagepng($dst);
?>
shuiyin.php
<?php
//$y 原图
//$w 水印图
//$tm=透明度
//$prefix 另存为文件时的前缀名
water('yz.jpg','logo.gif',1,100,'wa1_');
water('yz.jpg','logo.gif',2,100,'wa2_');
water('yz.jpg','logo.gif',3,100,'wa3_');
water('yz.jpg','logo.gif',4,100,'wa4_');
water('yz.jpg','logo.gif',5,100,'wa5_');
water('yz.jpg','logo.gif',6,100,'wa6_');
water('yz.jpg','logo.gif',7,100,'wa7_');
water('yz.jpg','logo.gif',8,100,'wa8_');
water('yz.jpg','logo.gif',9,100,'wa9_');
water('yz.jpg','logo.gif',0,100,'wa0_');
function water($y,$w,$pos=0,$tm=100,$prefix='wa_'){
$yInfo=getinfo($y);
$wInfo=getinfo($w);
if(!$yRes=wnOpen($y,$yInfo['type'])){
exit('文件打开失败');
}
if(!$wRes=wnOpen($w,$wInfo['type'])){
exit('水印文件打开失败');
}
switch($pos){
case 1:
$x=0;
$y=0;
break;
case 2:
$x=ceil(($yInfo['width']-$wInfo['width'])/2);
$y=0;
break;
case 3:
$x=$yInfo['width']-$wInfo['width'];
$y=0;
break;
case 4:
$x=0;
$y=floor(($yInfo['height']-$wInfo['height'])/2);
break;
case 5:
$x=ceil(($yInfo['width']-$wInfo['width'])/2);
$y=floor(($yInfo['height']-$wInfo['height'])/2);
break;
case 6:
$x=$yInfo['width']-$wInfo['width'];
$y=floor(($yInfo['height']-$wInfo['height'])/2);
break;
case 7:
$x=0;
$y=$yInfo['height']-$wInfo['height'];
break;
case 8:
$x=ceil(($yInfo['width']-$wInfo['width'])/2);
$y=$yInfo['height']-$wInfo['height'];
break;
case 9:
$x=$yInfo['width']-$wInfo['width'];
$y=$yInfo['height']-$wInfo['height'];
break;
case 0:
default:
$x=mt_rand(0,$yInfo['width']-$wInfo['width']);
$y=mt_rand(0,$yInfo['height']-$wInfo['height']);
break;
}
imagecopymerge($yRes,$wRes,$x,$y,0,0,$wInfo['width'],$wInfo['height'],$tm);
wnSave($yRes,$yInfo,$prefix);
imagedestroy($yRes);
imagedestroy($wRes);
}
function wnSave($res,$info,$prefix){
$newName=$prefix.$info['name'];
switch($info['type']){
case 'image/png':
case 'image/x-png':
$res=imagepng($res,$newName);
break;
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
$res=imagejpeg($res,$newName);
break;
case 'image/gif':
$res=imagegif($res,$newName);
break;
case 'image/wbmp':
$res=imagewbmp($res,$newName);
break;
default:
return false;
}
return $res;
}
function wnOpen($path,$type){
switch($type){
case 'image/png':
case 'image/x-png':
$res=imagecreatefrompng($path);
break;
case 'image/jpeg':
case 'image/pjpeg':
case 'image/jpg':
$res=imagecreatefromjpeg($path);
break;
case 'image/gif':
$res=imagecreatefromgif($path);
break;
case 'image/wbmp':
$res=imagecreatefromwbmp($path);
break;
default:
return false;
}
return $res;
}
function getInfo($path){
$data=getimagesize($path);
//info是我随意命名的一个数组变量
$info['width']=$data[0];
$info['height']=$data[1];
$info['type']=$data['mime'];
$info['name']=basename($path);
return $info;
}
?>
yzm.php
<?php
//width=100
//height=60
//type 数字,字母,数字字母混合
yzm();
function yzm($width=100,$height=60,$type=3,$length=4){
$img=imagecreatetruecolor($width,$height);
imagefilledrectangle($img,0,0,$width,$height,qColor($img));
switch($type){
case 1:
$string=join('',array_rand(range(0,9),$length));
break;
case 2:
$string=join('',array_rand(array_flip(range('a','z')),$length));
break;
case 3:
default:
for($i=0;$i<$length;$i++){
$rand=mt_rand(0,2);
switch($rand){
case 0:
$as=mt_rand(48,57); //0-9
break;
case 1:
$as=mt_rand(65,90); //A-Z
break;
case 2:
$as=mt_rand(97,122);//a-z
break;
}
$string.=chr($as);
}
break;
}
//干扰元素
for($i=0;$i<50;$i++){
imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),sColor($img));
}
//随机直线,随机曲线
//写字
for($i=0;$i<$length;$i++){
$x=$i*floor($width/$length);
$y=mt_rand(0,$height-15);
imagechar($img,5,$x,$y,$string[$i],sColor($img));
}
header('content-type:image/png');
imagepng($img);
imagedestroy($img);
//记住啊,把字符串返回回去,以后存到session里面使用
return $string;
}
//浅色,供背景使用
//值越高,颜色越浅
function qColor($img){
return imagecolorallocate($img,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));
}
function sColor($img){
return imagecolorallocate($img,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
}
?>