PHP: How to determine if jpeg is completely black (no image)?

I built a community site where users upload their photo and they can crop the thumbnail, but on Facebook it’s clear.

But for some reason, some of them generate empty (actually black jpeg thumbnails).

I tried different solutions, but it looks like this is happening with large images, or maybe on computers where JS is not enabled? I dont know...

On the bottom line, since the number of users with this is very small, I was thinking about creating a patch: detect when the user generates an empty jpeg. Then I could warn them.

Do you know how to do this?

+5
source share
5 answers

1x1 PX. , . , , 1px . , .

+4

, , , imagecolorat().

, - CMYK JPG , GD . - . , CMYK, getimagesize() channels .

+3

,

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

80% , 99% . .

+2

, , :

-

shortcut (if its always the same black image) - compare the byte byte file (or md5 hash) with the black image sample

+1
source

If everything is black, the file size will be low since it will be compressed. I know that JPG uses perceptual rather than RLE, but it will still be less. Why not ban jpg up to 20k for example? Take some examples, compare the sizes, set your thresholds.

0
source

All Articles