首页 > 解决方案 > PHP - Black image detection and Ignore text. How?

问题描述

My security cam has a failure, sometimes at the movement, the first snaps are black. That’s not a really a problem but I was looking for a way to delete those black images via a PHP script.

This one works great with a fully black images:

<?php
function check_if_black($src){

   $img = imagecreatefromjpeg($src);
    list($width_orig, $height_orig)=getimagesize($src);
    for($i=0;$i<20;$i++){
        $rand_width=rand ( 0 , $width_orig );
        $rand_height=rand ( 0 , $height_orig );
        $rgb = imagecolorat($img, $rand_width, $rand_height);
        if($rgb!=0){
            return "not black";
        }
    }
    return "black";
}
?>

My snaps have a white timestamp on it. Always in the same position and always with white text. With the code I posted above it says the image isn't black. But 80% of it is. Is there a way to detect if the snapshot is black and ignores the white timestamp on it?

A sample of a snapshot: Sample screenshot

标签: phptexttimestamp

解决方案


如果时间戳将放置在图像的底部意味着,您可以忽略底部的一些高度,然后获取图像的宽度和高度。现在你将只有黑色图像

function check_if_black($src){

   $img = imagecreatefromjpeg($src);
    list($width_orig, $height_orig)=getimagesize($src);
    $height_orig=$height_orig-20;//Reduce some height(Ex 20px) here
    for($i=0;$i<20;$i++){
        $rand_width=rand ( 0 , $width_orig );
        $rand_height=rand ( 0 , $height_orig );
        $rgb = imagecolorat($img, $rand_width, $rand_height);
        if($rgb!=0){
            return "not black";
        }
    }
    return "black";
}

推荐阅读