Write a PHP script to filter uploaded images

Source: Internet
Author: User
This article mainly introduces how to write a PHP script to filter images uploaded by users. As to whether it can accurately filter those images as expected, you can refer to this article.

This article mainly introduces how to write a PHP script to filter images uploaded by users. As to whether it can accurately filter those images as expected, you can refer to this article.

Sample download

On phpclasses.org, I accidentally found a very useful file. Developed by Bakr Alsharif, it can help developers detect bare image files based on skin pixels.

It analyzes the colors used in different parts of an image and determines whether it matches the human skin color.

As a result of the analysis, the system returns a score that reflects the possibility of image exposure.

In addition, he can output the analyzed image and mark the pixels with the color given.

Currently, it can analyze PNG, GIF, and JPEG images.

The following shows how to use this PHP class.
Let's start with the inclusion of the native filter, nf. php file.

Include ('nf. php ');

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

$ Filter = new ImageFilter;

Get the image score 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%, an alarm message is displayed.


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

Below are all the PHP code:

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

Markup Language

We can use a basic HTML form to upload images.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.