当前位置:多学网学习教育电脑学习编程入门PHP教程PHP图片防盗链程序

PHP图片防盗链程序

[08-23 22:09:15]   来源:http://www.duoxue8.com  PHP教程   阅读:673
PHP图片防盗链程序,标签:PHP技巧,php培训,php学习,php安装,http://www.duoxue8.com
php 代码复制内容到剪贴板
  1. <?   
  2. // ========================== 文件说明 ==========================//   
  3. // 文件说明:图片防盗链程序   
  4. // --------------------------------------------------------------//   
  5. // 程序作者:Sindo(锐利工作室)   
  6. // --------------------------------------------------------------//   
  7. // 程序版本:0.1   
  8. // --------------------------------------------------------------//   
  9. // 运行依赖:一个中文字体文件 chinese.fon   
  10. // --------------------------------------------------------------//   
  11. // 程序主页:http://www.wreny.com   
  12. // --------------------------------------------------------------//   
  13. // Copyright (C) Wreny Studio. All Rights Reserved.   
  14. // ==============================================================//   
  15.     main();   
  16.     function main() {   
  17.         //存放图片的文件夹在服务器上的绝对地址,注意以/结尾   
  18.         //$imgRootPath = "D:/WorkSpas/Apache/";   
  19.         $imgRootPath = "/home/610c00cfe2827dcf2ebf27314fb027a1/";   
  20.         //例外允许连接的网址,注意:自身域名不需要填入,设定为肯定可以连接图片   
  21.         $excludeReferArr = array("www.im286.com", "im286.com");   
  22.         doJudgeReferer($excludeReferArr);   
  23.         $imgRelPath=$_REQUEST['src'];   
  24.         if(emptyempty($imgRelPath)){   
  25.             doOutputMsgImg("未指定要查看的图片!");   
  26.             exit;   
  27.         }   
  28.         $srcSplitArr = explode(".", $imgRelPath);   
  29.         $srcSuffix = $srcSplitArr[count($srcSplitArr)-1];   
  30.         $srcSuffix = strtolower($srcSuffix);   
  31.         $imgAbsPath = $imgRootPath.$imgRelPath;   
  32.         if(!file_exists($imgAbsPath)){   
  33.             doOutputMsgImg("对不起,此图片链接已经失效!");   
  34.         }   
  35.         else if($srcSuffix == "gif") {   
  36.             header ("Content-type: image/gif");   
  37.             $imgFlag = $_REQUEST['flag'];   
  38.             if($imgFlag == "dynGif") {   
  39.                 $fp = fopen($imgAbsPath, 'r');   
  40.                 fpassthru($fp);   
  41.             }   
  42.             else {   
  43.                 $image = imagecreatefromgif ($imgAbsPath);   
  44.                 imagegif ($image);   
  45.                 imagedestroy ($image);   
  46.             }   
  47.         }   
  48.         else if($srcSuffix == "jpg") {   
  49.             header ("Content-type: image/jpeg");   
  50.             $image = imagecreatefromjpeg (imgAbsPath);   
  51.             imagejpeg ($image);   
  52.             imagedestroy ($image);   
  53.         }   
  54.         else if($srcSuffix == "png") {   
  55.             header ("Content-type: image/png");   
  56.             $image = imagecreatefrompng ($imgAbsPath);   
  57.             imagepng ($image);   
  58.             imagedestroy ($image);   
  59.         }   
  60.         else {   
  61.             doOutputMsgImg("图像类型不支持");   
  62.         }   
  63.     }   
  64.     function doJudgeReferer($excludeReferArr) {   
  65.         $referUrl=parse_url($_SERVER["HTTP_REFERER"]);   
  66.         $referHost = $referUrl[host];   
  67.         if($referHost!="" && $referHost!=$_SERVER["HTTP_HOST"] && !in_array($referHost, $excludeReferArr)){   
  68.             doOutputMsgImg("BS来自".$referUrl[host]."的盗链!");   
  69.             exit;   
  70.         }   
  71.     }   
  72.     function doMarkImage($inImg,$inMarkStr="Powered by Wreny.com") {   
  73.         $black = imagecolorallocate ($inImg, 0, 0, 0);   
  74.         $imgWidth = imagesx($inImg);   
  75.         $imgHeight = imagesy($inImg);   
  76.         //289-108,86   
  77.         drawTxt($inImg,$inMarkStr, ($imgWidth-strlen($inMarkStr)*9),($imgHeight-16), $black);   
  78.     }   
  79.     function doOutputMsgImg($msg, $imgWidth=468,   
  80.         $imgHeight=60, $imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) {   
  81.         $img = imagecreatetruecolor($imgWidth, $imgHeight);   
  82.         // 用白色背景加黑色边框画个方框   
  83.         $backColor = imagecolorallocate($img, 255, 255, 255);   
  84.         $borderColor = imagecolorallocate($img, 0, 0, 0);   
  85.         imagefilledrectangle($img, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor);   
  86.         imagerectangle($img, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor);   
  87.         $imgFgColor = imagecolorallocate ($img, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]);   
  88.         drawTxt($img, $msg, ($imgWidth-strlen($msg)*9)/2,($imgHeight/2-16),$imgFgColor);   
  89.         doMarkImage($img);   
  90.         header('Content-type: image/png');   
  91.         imagepng($img);   
  92.         imagedestroy($img);   
  93.     }   
  94.     function isCharVilid($inStr, $inPos) {   
  95.         if(strlen($inStr) < ($inPos+1)) {   
  96.             return true;   
  97.         }   
  98.         else {   
  99.             for($iLoop=0,$iCounter=0;$iLoop<=$inPos; $iLoop++){   
  100.                 if(substr($inStr, $iLoop, 1)<='~') {   
  101.               $iCounter++;   
  102.              }   
  103.             }   
  104.             return ( ($iCounter % 2) == 0 );   
  105.         }   
  106.     }   
  107.     function drawTxt($image, $string, $x, $y, $color) {   
  108.         $fp = fopen("chinese.fon", "r"); //WIN98中,此文件在:c:windowscommand 下   
  109.         if (feof($fp)) {   
  110.             fclose($fp);   
  111.             return 0;   
  112.         }   
  113.         //GBK   
  114.         $strings = preg_split(   
  115.             '/((?:[\x80-\xFF][\x40-\xFF])+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE   
  116.         );   
  117.         //print_r($strings);   
  118.         $isch = false;   
  119.         for ($p = 0, $count = count($strings); $p < $count; $p ++) {   
  120.             if ($isch) {   
  121.                 $string = $strings[$p];   
  122.                 for ($i = 0, $l = strlen($string) - 1; $i < $l; $i += 2) {   
  123.                     $qh = ord($string{$i}); // get ascii code   
  124.                     $offset = (94 * ($qh - 0xA0 - 1) + (ord($string{$i + 1}) - 0xA0 - 1)) * 32;   
  125.                     fseek($fp, $offset, SEEK_SET);   
  126.                     $buffer = unpack('n*', fread($fp, 32));   
  127.                     //        $buffers[$offset] = $buffer;   
  128.                     for ($yy = 1, $ypos = $y; $yy <= 16; $yy ++, $ypos ++) {   
  129.                         $bits = $buffer[$yy];   
  130.                         for ($xbit = 32768, $xpos = $x; $xbit > 0; $xbit >>= 1, $xpos ++)  {   
  131.                             if ($bits & $xbit) {   
  132.                                 imagesetpixel($image, $xpos, $ypos, $color);   
  133.                             }   
  134.                         }   
  135.                     }   
  136.                     $x += 16;   
  137.                 }   
  138.             }   
  139.             else {   
  140.                 imagestring($image, 12, $x, $y, $strings[$p], $color);   
  141.                 $x += strlen($strings[$p]) * 9;   
  142.             }   
  143.             $isch = !$isch;   
  144.         }   
  145.         return 0;   
  146.     }   
  147. ?>    
  148.     



PHP图片防盗链程序 结束。
Tag:PHP教程PHP技巧,php培训,php学习,php安装电脑学习 - 编程入门 - PHP教程
PHP图片防盗链程序相关文章