PHP 如何阻止用户上传成人照片或者裸照

在这份教程中,我们将会学习到如何阻止用户通过PHP上传成人照片或者裸照.

  示例   下载

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

  它会分析在一张图片的不同部分使用的颜色,并决定其是否匹配人类皮肤颜色的色调.

  作为分析的结果,他会返回一个反映图片包含裸露的可能性的分值.

  此外,他还可以输出被分析的图片,上面对使用给定颜色的肤色的像素进行了标记.

  当前它可以对PNG,GIF和JPEG图片进行分析.

  PHP

  下面展示了如何使用这个PHP类.

  让我们先从包含裸体过滤器,nf.php文件开始.

1
include ('nf.php');

  接下来,创建一个新的名叫ImageFilter的类,然后把它放到一个叫做$filter的变量中.

1
$filter new ImageFilter;

  获取图片的分值并将其放到一个$score变量中.

1
$score $filter -> GetScore($_FILES['img']['tmp_name']);

  如果图片分值大于或等于60%,那就展示一条(告警)消息.

1
2
3
if($score >= 60){
/*Message*/
}

  下面是所有的PHP代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/*Include the Nudity Filter file*/
include ('nf.php');
/*Create a new class called $filter*/
$filter new ImageFilter;
/*Get the score of the image*/
$score $filter -> GetScore($_FILES['img']['tmp_name']);
/*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 is considered an adult image.*/
    if ($score >= 60) {
        echo "Image scored " $score "%, It seems that you have uploaded a nude picture.";
    /*If the image doesn't contain nudity*/    
    else if ($score < 0) {
        echo "Congratulations, you have uploaded an non-nude image.";
    }
}
?>

  标记语言

  我们可以使用一个基础的HTML表单上传图片.

1
2
3
4
5
<form method="post" enctype="multipart/form-data" action="<?<span id="1_nwp" style="width: auto; height: auto; float: none;"><a id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=d38684659f046a0d&k=php&k0=php&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=d6a049f658486d3&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F4320%2Ehtml&urlid=0" target="_blank" mpid="1" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span> echo $SERVER['PHP_SELF'];?> ">
Upload image: 
<input type="file" name="<span id="2_nwp" style="width: auto; height: auto; float: none;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=d38684659f046a0d&k=img&k0=img&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=d6a049f658486d3&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F4320%2Ehtml&urlid=0" target="_blank" mpid="2" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">img</span></a></span>" id="img" />
<input type="submit" value="Sumit Image" />
</form>

  总结

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

  原文地址:http://www.rrpowered.com/2014/04/prevent-uploads-of-adult-or-nude-pictures-using-php/

posted @ 2015-11-17 10:50  务观  阅读(431)  评论(0编辑  收藏  举报