Home > Article > Backend Development > PHP detects vulgar images -_-!
<?php /** * PHP检查低俗图片 * 作者:书中蠹鱼 */ if($argc == 2){ $file_name = $argv[1]; if(is_file($file_name)){ //文件存在 $image = openImageFile($file_name); $rate = testSkin($image['image_handle'],$image['width'],$image['height']); if($rate > 0.3){ echo "$file_name 貌似是一张低俗图片.肤色比例:$rate/n"; }else { echo "$file_name 貌似不是一张低俗图片.肤色比例:$rate/n"; } }else { echo "$file_name can'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('image_handle'=>$image_handle,'width'=>$width,'height'=>$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? !