PHP 图片防盗链的一段代码

该代码还未测试,有时间在测试。。
 
<?
// ========================== 文件说明 ==========================//
// 文件说明:图片防盗链程序
// --------------------------------------------------------------//
// 程序作者: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:\windows\command 下
        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;
    }
?>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值