防止图片盗链的程序 - IM286 - sindo

[ 2007-12-22 10:02:26 | 作者: Admin ] 字号: | |
<?
// ========================== 文件说明 ==========================//
// 文件说明:图片防盗链程序
// --------------------------------------------------------------//
// 程序作者:Sindo(锐利工作室)
// --------------------------------------------------------------//
// 程序版本:0.1
// --------------------------------------------------------------//
// 运行依赖:一个中文字体文件 chinese.fon
// --------------------------------------------------------------//
// 程序主页:http://www.wreny.com
// --------------------------------------------------------------//
// Copyright (C) Wreny Studio. All Rights Reserved.
// ==============================================================//
 main();

 function main() {
 //存放图片的文件夹在服务器上的绝对地址,注意以/结尾
 //$imgRootPath = "D:/WorkSpas/Apache/";
 $imgRootPath = "/home/610c00cfe2827dcf2ebf27314fb027a1/";
 //例外允许连接的网址,注意:自身域名不需要填入,设定为肯定可以连接图片
 $excludeReferArr = array("www.im286.com", "im286.com");

 doJudgeReferer($excludeReferArr);

 $imgRelPath=$_REQUEST['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("对不起,此图片链接已经失效!");
 }
 else if($srcSuffix == "gif") {
 header ("Content-type: image/gif");

 $imgFlag = $_REQUEST['flag'];
 if($imgFlag == "dynGif") {
 $fp = fopen($imgAbsPath, 'r');
 fpassthru($fp);
 }
 else {
 $image = imagecreatefromgif ($imgAbsPath);
 imagegif ($image);
 imagedestroy ($image);
 }
 }
 else if($srcSuffix == "jpg") {
 header ("Content-type: image/jpeg");
 $image = imagecreatefromjpeg (imgAbsPath);
 imagejpeg ($image);
 imagedestroy ($image);
 }
 else if($srcSuffix == "png") {
 header ("Content-type: image/png");

 $image = imagecreatefrompng ($imgAbsPath);
 imagepng ($image);
 imagedestroy ($image);
 }
 else {
 doOutputMsgImg("图像类型不支持");
 }
 }

 function doJudgeReferer($excludeReferArr) {
 $referUrl=parse_url($_SERVER["HTTP_REFERER"]);
 $referHost = $referUrl[host];
 if($referHost!="" && $referHost!=$_SERVER["HTTP_HOST"] && !in_array($referHost, $excludeReferArr)){
 doOutputMsgImg("BS来自".$referUrl[host]."的盗链!");
 exit;
 }
 }

 function doMarkImage($inImg,$inMarkStr="Powered by Wreny.com") {
 $black = imagecolorallocate ($inImg, 0, 0, 0);

 $imgWidth = imagesx($inImg);
 $imgHeight = imagesy($inImg);
 //289-108,86
 drawTxt($inImg,$inMarkStr, ($imgWidth-strlen($inMarkStr)*9),($imgHeight-16), $black);
 }

 function doOutputMsgImg($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);
 }

 function isCharVilid($inStr, $inPos) {
 if(strlen($inStr) < ($inPos+1)) {
 return true;
 }
 else {

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

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

 function drawTxt($image, $string, $x, $y, $color) {
 $fp = fopen("chinese.fon", "r"); //WIN98中,此文件在:c:windowscommand 下
 if (feof($fp)) {
 fclose($fp);
 return 0;
 }

 //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); $p < $count; $p ++) {
 if ($isch) {
 $string = $strings[$p];
 for ($i = 0, $l = strlen($string) - 1; $i < $l; $i += 2) {
 $qh = ord($string{$i}); // get ascii code
 $offset = (94 * ($qh - 0xA0 - 1) + (ord($string{$i + 1}) - 0xA0 - 1)) * 32;

 fseek($fp, $offset, SEEK_SET);
 $buffer = unpack('n*', fread($fp, 32));
 // $buffers[$offset] = $buffer;
 for ($yy = 1, $ypos = $y; $yy <= 16; $yy ++, $ypos ++) {
 $bits = $buffer[$yy];
 for ($xbit = 32768, $xpos = $x; $xbit > 0; $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;
 }

 return 0;
 }
?>
Share
评论Feed 评论Feed: http://www.85815.com/feed.asp?q=comment&id=244
UTF-8 Encoding 引用链接: http://www.85815.com/trackback.asp?id=244&key=
这篇日志没有评论.
发表
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:   注册?
验证码 * 请输入验证码