<?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扩展。
我突然有个不错的想法……把这个检测和上一篇爬虫结合在一起。这找图不就方便了么?!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号