How to Prevent Uploading of Drape and Adult Images to your Website using PHP and AngularJs ~ Hybrid Mobile Apps Development, React Native, Flutter, JavaScript, Darts, iOS, Android, NodeJS
Coding Savvy FB Twitter Google
» »

How to Prevent Uploading of Drape and Adult Images to your Website using PHP and AngularJs

There are lot of restriction on Adult Images and contents on the web, some user abuse the opportunity you give to them to upload images that might get your website into trouble, Not all webmasters have the time to be screening all images uploaded to their server before it is published. It can be painful to know which type of Image that exposes body parts through just a little line of coding for this reason we are going to borrow a class from phpclasses.org to help use view this images and screen them out, Do you know an interesting part, This class has a tolerance control that can help you set the amount of expose able parts, We don't want to block a picture of woman breast feeding her baby when we are building a charity site.
How to Prevent Uploading of Drape and Adult Images to your Website using PHP



Download Script Live Demo

Let start by creating out index.html
<html> 
<head>
 <script src="./lib/angularjs.js"></script>
</head> 

<body ng-app = "uploader"> 
 <div ng-controller = "myCtrl">
 <input type = "file" file-model = "theImage"/>
 <button ng-click = "uploadFile()">upload me</button> 

<div id="resp" ng-bind-html="out" >
</div> 
</div> 
 <script>
  var uploader = angular.module('uploader', []); 

uploader.directive('fileModel', ['$parse', function ($parse) { 
return { restrict: 'A', link: function(scope, element, attrs) 

var model = $parse(attrs.fileModel); 
var modelSetter = model.assign;

 element.bind('change', function(){ 
 scope.$apply(function(){ 
modelSetter(scope, element[0].files[0]); 
});
 }); 
} }; 
 }]);

 uploader.factory('uploadService', function ($http) { 
var _upload = function (file,uploadUrl,resultCallback) { 
var fd = new FormData();
 fd.append('myPhoto', file); 
$http.post(uploadUrl, fd, { 
 transformRequest: angular.identity, headers: {'Content-Type': undefined} }) 
 .success(function(data, status, headers, config) { 
resultCallback(data); 
 }) 
.error(function(data, status, headers, config) { 
switch(status) {
  case 401: { break;
 case 500: { break; } } 
console.log(data, status); }); } ;
 return { upload: _upload }; }); 

uploader.controller('myCtrl', ['$scope', 'uploadService','$sce', 
function($scope, uploadService,$sce){ 
 $scope.uploadFile = function(){ 
 var file = $scope.theImage; //console.dir(file); 

var uploadUrl = "./server.php"; 

uploadService.upload(file,uploadUrl,function(result){



 $scope.out=$sce.trustAsHtml(result); }); 

delete $scope.theImage; }; 

 $scope.setResult = function(result){ $scope.output=result; } }]); 

</script> 
</body> 

</html>

We need to use our server side code to receive the image and treat it with our Image Filter class, This will enable use to properly screen this image and check if it contains more that 45% exposure of any part of human body.
Here is our server.php The server side PHP file to handle our request.
<?php

if(isset($_FILES['myPhoto'])){ 
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP"); 
 $name = $_FILES['myPhoto']['name']; $size = $_FILES['myPhoto']['size']; 
$tmp = $_FILES['myPhoto']['tmp_name']; 
//$db = mysqli_connect("localhost","root","","myImagesDB"); 
$ext = pathinfo($name, PATHINFO_EXTENSION); 
 if(in_array($ext,$valid_formats)) { 
if($size<(1024*1024)) { //import the adult checking class require_once('class.ImageFilter.php'); $filter = new ImageFilter; 
$score = $filter->GetScore($tmp); 
 if(isset($score)) { 
//check the score 45% expose setting should be okay because of light skins 
if($score >= 45){echo "Image scored ".$score."%, It seems that you have uploaded an Adult picture :-(";} 
else { 
$actual_image_name = time().".".$ext; 
$path = "uploads/";
 $newname = $path.$actual_image_name; if(move_uploaded_file($tmp,$newname )) {
 //$query = "INSERT INTO images (name,location) VALUES('{$actual_image_name}','{$newname}') "; 
//$data = mysqli_query($db, $query); 

echo "<img src='uploads/".$actual_image_name."' class='preview'>"; } 
else echo "Fail upload folder with read access."; } }
else echo "Sorry, unable to process your Picture at this time, please try again later."; }
else echo "The image you uploaded is too big, please try a smaller sized image, 1MB max."; }
else echo "Invalid Image Format: ".$ext; }
Was this article helpful?
Thanks! Your feedback helps us improve tutorials.

You May Also Like...

1 comment: