编写PHP脚本过滤用户上传的图片

yipeiwu_com7年前PHP代码库

示例   下载

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

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

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

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

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

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

include ('nf.php');

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

$filter = new ImageFilter;

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

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

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

 

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

下面是所有的PHP代码:

 
<?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表单上传图片.
 

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

相关文章

解析php中的fopen()函数用打开文件模式说明

fopen() 函数用于在 PHP 中打开文件。此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪种模式来打开文件:复制代码 代码如下:<?php$file=fope...

浅析php中三个等号(===)和两个等号(==)的区别

先举个列子:比如你一个函数会返回这几种情况:1、大于0的数2、小于0的数3、等于0的数(也就是0啦)4、False(失败时)这时候如果你想捕获失败的情况,你就必须用===,而不能用==因...

php中strstr、strrchr、substr、stristr四个函数的区别总结

php中strstr、strrchr、substr、stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混...

Thinkphp事务操作实例(推荐)

实例如下: //开启mysql事务操作 $model = M(); $model->startTrans(); $flag=false; $deal1 = M('ppdd')-...

php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍

最近一直在忙,赶在这个假期结束的时候,发表一下此文,为了是让这些源码开源出来当然这些方法可能不可取,但大致应该是这种方向了吧,由于目前做的一个UCHOME港台的站点咱们策划说要改一下好友...