Detecting empty generated images using php?

How can I find that an image is empty (only one, arbitrary color, or using gif, frames of random arbitrary colors) using PHP and / or imagemagick?

I think this is what I'm going to try: http://www.php.net/manual/en/function.imagecolorat.php#97957

+7
source share
4 answers

http://www.php.net/manual/en/function.imagecolorstotal.php gives you the number of colors in the image. Hmm, this doesn’t work in my demo, sorry :( The image I created (completely red, 20x20 pixels) gives 0 colors for PNG and 3 colors for GIF.

Good: http://www.dynamicdrive.com/forums/showpost.php?p=161187&postcount=2 look at the second part of the code. Tested here: http://www.pendemo.nl/totalcolors.php

+2
source

Kevin's decision can be accelerated using random sampling. If you have an idea of ​​the percentage of pixels that should be different from the background (provided that you are not dealing with a large number of images with only one pixel), you can use the Poisson distribution:

the probability of finding a non-empty pixel = 1 - e ^ (- n * p)

where n is the number of samples to try, and p is the percentage of pixels that are expected to be non-empty. Solve for n to get the appropriate number of samples to try:

n = -log (1 - x) / p

where x is the desired probability and log is the natural log. For example, if you are sure that 0.1% of the image should be non-empty, and you want to have a 99.99% chance of finding at least one non-empty pixel,

n = -log (1-.9999) /. 001 = 9210 required samples.

Much faster than checking each pixel. To be 100% sure, you can always go back and check everything if the sample does not find.

+2
source

You can check the image inside PHP with imagecolorat (this may be slow, but it works):

 function isPngValidButBlank($filename) { $img = imagecreatefrompng($filename); if(!$img) return false; $width = imagesx($img); $height = imagesy($img); if(!$width || !$height) return false; $firstcolor = imagecolorat($img, 0, 0); for($i = 0; $i < $width; $i++) { for($j = 0; $j < $height; $j++) { $color = imagecolorat($img, $i, $j); if($color != $firstcolor) return false; } } return true; } 
+1
source

Get the standard deviation from detailed statistics for each fragment. If the standard deviation is 0, then the image has one color.

Presumably, this will also make a "number of colors"; will be 1.

Use the -format parameter: http://www.imagemagick.org/script/escape.php

0
source

All Articles