当前位置:Gxlcms > PHP教程 > PHP检测低俗图片-_-!

PHP检测低俗图片-_-!

时间:2021-07-01 10:21:17 帮助过:1人阅读

<?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;
}
?>

需要GD库的支持。在php.ini里面打开gd扩展。

我突然有个不错的想法……把这个检测和上一篇爬虫结合在一起。这找图不就方便了么?!

人气教程排行