I am looking for some PHP nudity image filter library and I found this one on phpclasses.org: PHP Image Nudity Filter.
The class was written by Bakr Alsharif. According to the web site, the class “analyses the colors used in different sections of an image to determine whether those colors match the human skin color tones.”
I downloaded the code and it does use this interesting approach to analyze the image color pixel by pixel and see if they match the skin tone. Every match will add to a score and the higher the final score, the more likely the image contains nudity. For example, if an image scored above 30, it is likely a nude picture.
The class library is easy to understand and very simple to use. However I did discover one possbile bug. The upper bound for the “for” loop on the pixels may need to be changed to “<” instead of “<=”. Otherwise it will generate tons of “array out of bound” PHP warnings.
I test run the program using some pictures and it was quite interesting.
I used some baby photos, and it recognized them. I used some pictures with mountains and trees and it worked great.
But here is the issue. I used it to scan a bunch of real estate pictures and wow, one of them has a score of 100! I checked the photo and it is a kitchen photo with mostly beige cabinet and counter tops. The tone of the image is also light so that’s why the filter totally misjudged it. Beside that particular one, there are quite a few scored over 50 or 60. And they all have the similar characters.
I wonder if it would be help if we set a “ceiling score”. For example, if an image scores over 80 it is not likely a nude picture since you are not likely to have nudity filling up the whole screen. So I did another test and this time only single out the ones which scored between 30 and 80. Still, there are still too many “good” picutures being filtered out.
Although the test wasn’t quite successful I still think this PHP class isÂ a heading to the right direction. Maybe some more sophisticated algorithm can improve the accuracy. For example, in stead of checking for the color for each pixel, we can figure out an “average tone” of an image and then analyze each pixel color to make the decision. Standard deviation, maybe?