Heim > php教程 > php手册 > Hauptteil

PHP自动识别用户上传不雅图片并发邮箱提示

WBOY
Freigeben: 2016-05-25 16:41:35
Original
1277 Leute haben es durchsucht

在这份教程中,我们将会学习到如何阻止用户通过上传不雅图片,这个测试了一下只有一点作用,但人家真的要做了估计也没有办法了,下面来给大家介绍解决办法吧.

我在phpclasses.org上面偶然发现一个很有用的,由Bakr Alsharif开发的可以帮助开发者基于皮肤像素点来检测图片的类文件.

它会分析在一张图片的不同部分使用的颜色,并决定其是否匹配人类皮肤颜色的色调,作为分析的结果,他会返回一个反映图片包含xx露的可能性的分值,此外,他还可以输出被分析的图片,上面对使用给定颜色的肤色的像素进行了标记,当前它可以对PNG,GIF和JPEG图片进行分析.

PHP下面展示了如何使用这个PHP类,让我们先从包含过滤器,nf.php文件开始,代码如下:

include('nf.php');
//接下来,创建一个新的名叫ImageFilter的类,然后把它放到一个叫做$filter的变量中,代码如下:
$filter = new ImageFilter; 
//获取图片的分值并将其放到一个$score变量中,代码如下:
$score = $filter -> GetScore($_FILES['img']['tmp_name']);
//如果图片分值大于或等于60%,那就展示一条(告警)消息,代码如下:
if($score >= 60){
/*Message*/
}
Nach dem Login kopieren

下面是所有的PHP代码:

index.php

<?php
error_reporting(0);
/*Include the Nudity Filter file*/
include (&#39;./nf.php&#39;);
/*Create a new class called $filter*/
$filter = new ImageFilter;
/*Get the score of the image*/
$score = $filter -> GetScore($_FILES[&#39;img&#39;][&#39;tmp_name&#39;]);
/*If the $score variable is set*/
if (isset($score)) {
    /*If the image contains nudity display image score and message. Score value If more than 60%, it consider as adult image.*/
    if ($score >= 60){
        $error = "<b>Image scored " . $score . "%, It seems that you have uploaded a nude picture.</b>";
    /*If the image doesn&#39;t contain nudity*/ 
    } else if ($score < 0) {
        $error = "<b>Congratulations, you have uplaoded an non-nude image.</b>";
    }
}
?>
<!DOCTYPE html>
<html>
    <meta charset="utf-8" />
    <head>
        <title>Nudity Filter - RRPowered</title>
    </head>
    <body>
        <?php echo $error;?>
        <form method="post" enctype="multipart/form-data" action="<?php echo $SERVER[&#39;PHP_SELF&#39;];?> ">
        Upload image: 
        <input type="file" name="img" id="img" />
        <br />
        <input type="submit" value="Sumit Image" />
        </form>
    </body>
</html>
Nach dem Login kopieren

nf.php

<?php
//
// +-----------------------------------+
// |        Image Filter v 1.0         |
// +-----------------------------------+
//
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the ISLAMIC RULES and GNU Lesser General Public
//   License either version 2, or (at your option) any later version.
//
//   ISLAMIC RULES should be followed and respected if they differ
//   than terms of the GNU LESSER GENERAL PUBLIC LICENSE
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the license with this software;
//   If not, please contact support @ S y s T u r n .com to receive a copy.
//

CLASS ImageFilter
{                              #R  G  B
    var $colorA = 7944996;     #79 3B 24
    var $colorB = 16696767;    #FE C5 BF


    var $arA = array();
    var $arB = array();
    
    function ImageFilter()
    {
        $this->arA[&#39;R&#39;] = ($this->colorA >> 16) & 0xFF;
        $this->arA[&#39;G&#39;] = ($this->colorA >> 8) & 0xFF;
        $this->arA[&#39;B&#39;] = $this->colorA & 0xFF;
        
        $this->arB[&#39;R&#39;] = ($this->colorB >> 16) & 0xFF;
        $this->arB[&#39;G&#39;] = ($this->colorB >> 8) & 0xFF;
        $this->arB[&#39;B&#39;] = $this->colorB & 0xFF;
    }
    
    function GetScore($image)
    {
        $x = 0; $y = 0;
        $img = $this->_GetImageResource($image, $x, $y);
        if(!$img) return false;

        $score = 0;
        
        $xPoints = array($x/8, $x/4, ($x/8 + $x/4), $x-($x/8 + $x/4), $x-($x/4), $x-($x/8));
        $yPoints = array($y/8, $y/4, ($y/8 + $y/4), $y-($y/8 + $y/4), $y-($y/8), $y-($y/8));
        $zPoints = array($xPoints[2], $yPoints[1], $xPoints[3], $y);

        
        for($i=1; $i<=$x; $i++)
        {
            for($j=1; $j<=$y; $j++)
            {
                $color = imagecolorat($img, $i, $j);
                if($color >= $this->colorA && $color <= $this->colorB)
                {
                    $color = array(&#39;R&#39;=> ($color >> 16) & 0xFF, &#39;G&#39;=> ($color >> 8) & 0xFF, &#39;B&#39;=> $color & 0xFF);
                    if($color[&#39;G&#39;] >= $this->arA[&#39;G&#39;] && $color[&#39;G&#39;] <= $this->arB[&#39;G&#39;] && $color[&#39;B&#39;] >= $this->arA[&#39;B&#39;] && $color[&#39;B&#39;] <= $this->arB[&#39;B&#39;])
                    {
                        if($i >= $zPoints[0] && $j >= $zPoints[1] && $i <= $zPoints[2] && $j <= $zPoints[3])
                        {
                            $score += 3;
                        }
                        elseif($i <= $xPoints[0] || $i >=$xPoints[5] || $j <= $yPoints[0] || $j >= $yPoints[5])
                        {
                            $score += 0.10;
                        }
                        elseif($i <= $xPoints[0] || $i >=$xPoints[4] || $j <= $yPoints[0] || $j >= $yPoints[4])
                        {
                            $score += 0.40;
                        }
                        else
                        {
                            $score += 1.50;
                        }
                    }
                }
            }
        }
        
        imagedestroy($img);
        
        $score = sprintf(&#39;%01.2f&#39;, ($score * 100) / ($x * $y));
        if($score > 100) $score = 100;
        return $score;
    }
    
    function GetScoreAndFill($image, $outputImage)
    {
        $x = 0; $y = 0;
        $img = $this->_GetImageResource($image, $x, $y);
        if(!$img) return false;

        $score = 0;

        $xPoints = array($x/8, $x/4, ($x/8 + $x/4), $x-($x/8 + $x/4), $x-($x/4), $x-($x/8));
        $yPoints = array($y/8, $y/4, ($y/8 + $y/4), $y-($y/8 + $y/4), $y-($y/8), $y-($y/8));
        $zPoints = array($xPoints[2], $yPoints[1], $xPoints[3], $y);


        for($i=1; $i<=$x; $i++)
        {
            for($j=1; $j<=$y; $j++)
            {
                $color = imagecolorat($img, $i, $j);
                if($color >= $this->colorA && $color <= $this->colorB)
                {
                    $color = array(&#39;R&#39;=> ($color >> 16) & 0xFF, &#39;G&#39;=> ($color >> 8) & 0xFF, &#39;B&#39;=> $color & 0xFF);
                    if($color[&#39;G&#39;] >= $this->arA[&#39;G&#39;] && $color[&#39;G&#39;] <= $this->arB[&#39;G&#39;] && $color[&#39;B&#39;] >= $this->arA[&#39;B&#39;] && $color[&#39;B&#39;] <= $this->arB[&#39;B&#39;])
                    {
                        if($i >= $zPoints[0] && $j >= $zPoints[1] && $i <= $zPoints[2] && $j <= $zPoints[3])
                        {
                            $score += 3;
                            imagefill($img, $i, $j, 16711680);
                        }
                        elseif($i <= $xPoints[0] || $i >=$xPoints[5] || $j <= $yPoints[0] || $j >= $yPoints[5])
                        {
                            $score += 0.10;
                            imagefill($img, $i, $j, 14540253);
                        }
                        elseif($i <= $xPoints[0] || $i >=$xPoints[4] || $j <= $yPoints[0] || $j >= $yPoints[4])
                        {
                            $score += 0.40;
                            imagefill($img, $i, $j, 16514887);
                        }
                        else
                        {
                            $score += 1.50;
                            imagefill($img, $i, $j, 512);
                        }
                    }
                }
            }
        }
        imagejpeg($img, $outputImage);

        imagedestroy($img);

        $score = sprintf(&#39;%01.2f&#39;, ($score * 100) / ($x * $y));
        if($score > 100) $score = 100;
        return $score;
    }
    
    function _GetImageResource($image, &$x, &$y)
    {
        $info = GetImageSize($image);
        
        $x = $info[0];
        $y = $info[1];
        
        switch( $info[2] )
        {
            case IMAGETYPE_GIF:
                return @ImageCreateFromGif($image);
                
            case IMAGETYPE_JPEG:
                return @ImageCreateFromJpeg($image);
                
            case IMAGETYPE_PNG:
                return @ImageCreateFromPng($image);
                
            default:
                return false;
        }
    }
}

?>
Nach dem Login kopieren


标记语言:我们可以使用一个基础的HTML表单上传图片,代码如下:

<form method="post" enctype="multipart/form-data" action="<?php echo $SERVER[&#39;PHP_SELF&#39;]; "> 
	Upload image:  
	<input type="file" name="img" id="img" /> 
	<input type="submit" value="Sumit Image" /> 
</form>
Nach dem Login kopieren

请记得,PHP不能够检测所有的图片,所以不完全可信,我希望你觉得这还有点用处.


教程地址:

欢迎转载!但请带上文章地址^^

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage