Home >Backend Development >PHP Tutorial >How to prevent users from uploading adult or nude photos in PHP
In this tutorial, we will learn how to prevent users from uploading adult photos or nude photos through PHP.
I accidentally discovered a very useful one on phpclasses.org, developed by Bakr Alsharif, which can help developers to create pixel-based images based on skin. Class file to detect nudity in pictures.
It will analyze the colors used in different parts of a picture and decide whether it matches the hue of human skin color.
As a result of the analysis, it will return a reflection that the picture contains nudity score of likelihood.
Additionally, it can output the analyzed image with pixels labeled with skin tones using a given color.
Currently it can analyze PNG, GIF and JPEG images.
PHP
Below shows how to use this PHP class.
Let’s start by including the nudity filter, nf.php file.
include ('nf.php');
Next, Create a new class called ImageFilter and put it in a variable called $filter.
$filter = new ImageFilter;
Get the score of the image and put it in a $score variable .
$score = $filter -> GetScore($_FILES['img']['tmp_name']);
If the image score is greater than or equal to 60%, then display an (alert) message.
if($score >= 60){/*Message*/}
The following is all the PHP code:
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.";
}
}?>
markup language
We can use a Basic HTML form to upload images.