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