php下载防盗链图片,php中图片、文件下载防盗链技巧

//存放图片的文件夹在服务器上的绝对地址$imgRootPath="pics/2005/11/";//例外允许连接的网址,注意:自身域名不需要填入,设定为肯定可以连接图片$excludeReferArr=array("www.163.com","163.com");

doJudgeReferer($excludeReferArr);//判断是否$imgRelPath=$_REQUEST['id'];//src参数if(empty($imgRelPath)){

doOutputMsgImg("未指定要查看的图片!");exit;

}$srcSplitArr=explode(".",$imgRelPath);//获得图片后缀名$srcSuffix=$srcSplitArr[count($srcSplitArr)-1];$srcSuffix=strtolower($srcSuffix);$imgAbsPath="$imgRootPath$imgRelPath";//更换复杂地址if(!file_exists($imgAbsPath))

{

doOutputMsgImg("对不起,此图片链接已经失效!");

}elseif($srcSuffix=="gif") {header("Content-type: image/gif");$image=imagecreatefromgif ($imgAbsPath);

imagegif ($image);

imagedestroy ($image);

}elseif($srcSuffix=="jpg") {header("Content-type: image/jpeg");$image=imagecreatefromjpeg ($imgAbsPath);

imagejpeg ($image);

imagedestroy ($image);

}elseif($srcSuffix=="png") {header("Content-type: image/png");$image=imagecreatefrompng ($imgAbsPath);

imagepng ($image);

imagedestroy ($image);

}else{

doOutputMsgImg("图像类型不支持");

}//**************

//相关函数

//*************functiondoJudgeReferer($excludeReferArr) {$referUrl=parse_url($_SERVER["HTTP_REFERER"]);$referHost=$referUrl[host];if($referHost!=""&&$referHost!=$_SERVER["HTTP_HOST"]&&!in_array($referHost,$excludeReferArr))

{

doOutputMsgImg("这是来自".$referUrl[host]."的盗链!");exit;

}

}functiondoMarkImage($inImg,$inMarkStr="Powered by Wreny.com") {$black=imagecolorallocate ($inImg,0,0,0);$imgWidth=imagesx($inImg);$imgHeight=imagesy($inImg);//289-108,86drawTxt($inImg,$inMarkStr,($imgWidth-strlen($inMarkStr)*9),($imgHeight-16),$black);

}functiondoOutputMsgImg($msg,$imgWidth=468,$imgHeight=60,$imgFgColorArr=array(0,0,0),$imgBgColorArr=array(255,255,255)) {$img=imagecreatetruecolor($imgWidth,$imgHeight);//用白色背景加黑色边框画个方框$backColor=imagecolorallocate($img,255,255,255);$borderColor=imagecolorallocate($img,0,0,0);

imagefilledrectangle($img,0,0,$imgWidth-1,$imgHeight-1,$backColor);

imagerectangle($img,0,0,$imgWidth-1,$imgHeight-1,$borderColor);$imgFgColor=imagecolorallocate ($img,$imgFgColorArr[0],$imgFgColorArr[1],$imgFgColorArr[2]);

drawTxt($img,$msg,($imgWidth-strlen($msg)*9)/2,($imgHeight/2-16),$imgFgColor);

doMarkImage($img);header('Content-type: image/png');

imagepng($img);

imagedestroy($img);

}functionisCharVilid($inStr,$inPos) {if(strlen($inStr)

}else{for($iLoop=0,$iCounter=0;$iLoop<=$inPos;$iLoop++){if(substr($inStr,$iLoop,1)<='~') {$iCounter++;

}

}return( ($iCounter%2)==0);

}

}functiondrawTxt($image,$string,$x,$y,$color) {$fp=fopen("chinese.fon","r");//WIN98中,此文件在:c:\windows\command 下if(feof($fp)) {fclose($fp);return0;

}//GBK$strings=preg_split('/((?:[\\x80-\\xFF][\\x40-\\xFF])+)/',$string,-1,PREG_SPLIT_DELIM_CAPTURE

);//print_r($strings);$isch=false;for($p=0,$count=count($strings);$p0;$xbit>>=1,$xpos++)  {if($bits&$xbit) {

imagesetpixel($image,$xpos,$ypos,$color);

}

}

}$x+=16;

}

}else{

imagestring($image,12,$x,$y,$strings[$p],$color);$x+=strlen($strings[$p])*9;

}$isch=!$isch;

}return0;

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值