note 97957 added to function.imagecolorat

From: Date: Tue, 18 May 2010 12:39:20 +0000
Subject: note 97957 added to function.imagecolorat
Groups: php.notes 
Request: Send a blank email to php-notes+get-169349@lists.php.net to get a copy of this message
function isBlank($gd_resource, $tolerance, $percent) { list($w,$h) = array(imagesx($gd_resource), imagesy($gd_resource)); $count = 0; $hr = $hg = $hb = array(); for ($i=0; $i<$h; $i++) { for ($j=0; $j<$w; $j++) { $pos = imagecolorat($gd_resource, $j, $i); $f = imagecolorsforindex($gd_resource, $pos); $hr[$f['red']] = issetor($hr[$f['red']],0) + 1; $hg[$f['green']] = issetor($hg[$f['green']],0) + 1; $hb[$f['blue']] = issetor($hb[$f['blue']],0) + 1; $count ++; } } $rc = array_sum($hr); $gc = array_sum($hg); $bc = array_sum($hb); $r = $rc/$count; $g = $gc/$count; $b = $bc/$count; $ra = range(max(0,$r-$tolerance), min(255,$r+$tolerance)); $ga = range(max(0,$g-$tolerance), min(255,$g+$tolerance)); $ba = range(max(0,$b-$tolerance), min(255,$b+$tolerance)); foreach ($ra as $rx) { unset($hr[$rx]); } foreach ($ga as $gx) { unset($hg[$gx]); } foreach ($ba as $bx) { unset($hb[$bx]); } $red = floatval(array_sum($hr)+1) / floatval($rc); $green = floatval(array_sum($hg)+1) / floatval($gc); $blue = floatval(array_sum($hb)+1) / floatval($bc); if ($red<$percent && $green<$percent && $blue<$percent) { return true; } return false; } Example; isBlank($gd_resource, 16, 0.1); If an image is filled with a solid color (rgb 0,0,0) the average color will be 0,0,0, and the histogram will have the maximum values at r[0], g[0], and b[0]. Since we remove those values with the tolerance (+- 16), we are only left with colors outside of average. If the removed colors represent more than 90% of the image (we remove 6.25-12.5% of the histogram based on the average color), then we asume the image is blank. This detects solid filled images of any color, and with images of very little color variance (blank wall, blue sky with no clouds, nearly faded images, very low contrast and low color range images). The function is perfect for checking if a frame from a video is blank or not (in case you are generating thumbnails). ---- Server IP: 91.185.195.14 Probable Submitter: 89.212.110.169 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php Edit -- https://master.php.net/note/edit/97957 Del: integrated -- https://master.php.net/note/delete/97957/integrated Del: useless -- https://master.php.net/note/delete/97957/useless Del: bad code -- https://master.php.net/note/delete/97957/bad+code Del: spam -- https://master.php.net/note/delete/97957/spam Del: non-english -- https://master.php.net/note/delete/97957/non-english Del: in docs -- https://master.php.net/note/delete/97957/in+docs Del: other reasons-- https://master.php.net/note/delete/97957 Reject -- https://master.php.net/note/reject/97957 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#169349) next »