How to filter explicit images when the user uploads their profile picture in php?

0

Well here with another doubt.

We know that any person, out of simple curiosity or annoyance, when uploading their profile picture on a web page, could happen to upload images to the server with explicit content.

This is where I wonder how would a filter be made to detect that type of images? that when it is detected that the image that has been uploaded to the server is explicit, throw an insert or update error, assuming that the development environment is with PHP, using bd as mysql (if the images are stored in bd) or in otherwise in files type $ FILES.

I have researched the subject a bit and found PHP functions such as ImageMagick, Gd that I see include some tricks for image processing, I also found plugins like Jquery filterizr but I think it does not help much.

I hope that if someone knows a bit about this topic, they can guide me.

Thank you all.

    
asked by Noctis 03.08.2018 в 02:23
source

1 answer

2

A Machine Learning model can take weeks to train. Basically you have to feed him with (at least) half a million images telling him which one is NSFW and which one is safe. With that the machine could predict if a new image is or is not NSFW. But this type of Machine Learning algorithms have a steep learning curve and you need a powerful server to train.

You could use a web service. For example, VRate allows you 7500 free calls per month. In the background the person uploads the image, you make it available in a public URL (such that VRate can see it) you send a call to the VRate API with the url of the image and it tells you if it is NSFW.

Using PHP, you copy the image to a URL that is not the one that is displayed to the rest of the users. For example, if the normal URL is:

http://www.noctis.com/avatar/mi_avatar.png

You make it available first in

http://www.noctis.com/pendiente/mi_avatar.png

Then send a request to the endpoint mediarating using (for example) Unirest with a request of type application / json :

$headers = [
    "X-Mashape-Key" => "YOUR_API_KEY",
    "Accept" => "application/json"
  ];

$data = ["payload":"http://www.noctis.com/pendiente/mi_avatar.png"];

$body = Unirest\Request\Body::json($data);

$response = Unirest\Request::post(
     'https://vrate.p.mashape.com/mediarating',
     $headers,
     $data
);

The answer is a JSON of the form:

{
  "RatingCode": "V01",
  "RatingDescription": "Safe, Ok",
  "Confidence": "High"
}

Another alternative (but allowing only 1000 free calls per month) is DeepAI .

Again using Unirest but with a request of type application / x-www-form-urlencoded which is what DeepAI expects:

$headers = [ 'Api-Key': 'YOUR_API_KEY'];
$data = ['image' => 'http://www.noctis.com/pendiente/mi_avatar.png'];

$body = Unirest\Request\Body::form($data);

$response = Unirest\Request::post(
     'https://api.deepai.org/api/nsfw-detector', 
     $headers, 
     $body
);

The answer will be a JSON of the form:

{
  "nsfw_score": 0.041
}

If the answer is satisfactory, copy the image to

 http://www.noctis.com/pendiente/mi_avatar.png

And everything continues its course. Otherwise, you display an error message, you ask the user to upload another photo and, below, you delete the server image that was visible in

http://www.noctis.com/pendiente/mi_avatar.png
    
answered by 03.08.2018 в 14:10