Home  >  Article  >  Backend Development  >  PHP detects vulgar images -_-!

PHP detects vulgar images -_-!

巴扎黑
巴扎黑Original
2016-11-24 13:44:321830browse

<?php
/**
 * PHP检查低俗图片
 * 作者:书中蠹鱼
 */
if($argc == 2){
$file_name = $argv[1];
if(is_file($file_name)){
//文件存在
$image = openImageFile($file_name);
$rate = testSkin($image[&#39;image_handle&#39;],$image[&#39;width&#39;],$image[&#39;height&#39;]);
if($rate > 0.3){
echo "$file_name 貌似是一张低俗图片.肤色比例:$rate/n";
}else {
echo "$file_name 貌似不是一张低俗图片.肤色比例:$rate/n";
}
}else {
echo "$file_name can&#39;t be find!/n";
}
}else{
echo "Usage: testskin.php FileName/n";
}
function openImageFile($file_name){
list($width, $height, $type, $attr) = getimagesize($file_name);
switch ($type){
case 2:
$image_handle = imagecreatefromjpeg($file_name);
break;
}
return array(&#39;image_handle&#39;=>$image_handle,&#39;width&#39;=>$width,&#39;height&#39;=>$height);
}
function testSkin($image_handle,$width,$height){
$skin_pix = 0;
for($w=0;$w<$width;$w++){
for ($h=0;$h<$height;$h++){
//验证图片
$rgb = imagecolorat($image_handle,$w,$h);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$Y=0.299*$r+0.587*$g+0.114*$b;
$Cb=0.564*($b-$Y)+128;
$Cr=0.713*($r-$Y)+128;
if($Cb >= 86 && $Cb <= 117 && $Cr >= 140 && $Cr <= 168){
$skin_pix ++;
}
}
}
$skin_rate =  $skin_pix/($width*$height);
return $skin_rate;
}
?>

Requires GD library support. Open the gd extension in php.ini.

I suddenly had a good idea... combine this detection with the previous crawler. Wouldn’t it be convenient to find pictures? !


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn