Home > Backend Development > PHP Tutorial > Write a PHP script to filter images uploaded by users, _PHP tutorial

Write a PHP script to filter images uploaded by users, _PHP tutorial

WBOY
Release: 2016-07-13 09:47:25
Original
1156 people have browsed it

Write a PHP script to filter images uploaded by users,

Example Download

I accidentally discovered a very useful class file on phpclasses.org, developed by Bakr Alsharif, which can help developers detect nude photos based on skin pixels.

It analyzes the colors used in different parts of an image and decides if they match the hue of human skin color.

As a result of the analysis, it returns a score that reflects the likelihood that the image contains nudity.

In addition, it can output the analyzed image, with pixels marked using a given color skin tone.

Currently it can analyze PNG, GIF and JPEG images.

The following shows how to use this PHP class.
Let’s start with the nf.php file that contains the nudity filter.

include ('nf.php');
Copy after login

Next, create a new class called ImageFilter and put it in a variable called $filter.

$filter = new ImageFilter;
Copy after login

Get the score of the image and put it into a $score variable.

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

If the image score is greater than or equal to 60%, then display a (alert) message.


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

Here are all the PHP codes:

 
<?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.";
  }
}
?>
Copy after login

Markup language

We can upload images using a basic HTML form.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1026544.htmlTechArticleWrite a PHP script to filter the images uploaded by users, sample download I accidentally found a very useful one on phpclasses.org, Developed by Bakr Alsharif, it helps developers based on skinned pixels...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template