note 70783 added to function.imagecolorat

From: Date: Fri, 27 Oct 2006 18:31:16 +0000
Subject: note 70783 added to function.imagecolorat
Groups: php.notes 
Request: Send a blank email to php-notes+get-119191@lists.php.net to get a copy of this message
I made a function that calculates the average color of a given image resource and returns it in "#rrggbb" format (hex): function average($img) { $w = imagesx($img); $h = imagesy($img); $r = $g = $b = 0; for($y = 0; $y < $h; $y++) { for($x = 0; $x < $w; $x++) { $rgb = imagecolorat($img, $x, $y); $r += $rgb >> 16; $g += $rgb >> 8 & 255; $b += $rgb & 255; } } $pxls = $w * $h; $r = dechex(round($r / $pxls)); $g = dechex(round($g / $pxls)); $b = dechex(round($b / $pxls)); if(strlen($r) < 2) { $r = 0 . $r; } if(strlen($g) < 2) { $g = 0 . $g; } if(strlen($b) < 2) { $b = 0 . $b; } return "#" . $r . $g . $b; } Although, I've noticed that you can also get a fairly good average color generating a 1px by 1px copy with imagecopyresampled (the pixel generated is colored with the average color). ---- Server IP: 66.163.161.117 Probable Submitter: 190.2.14.225 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php Edit -- https://master.php.net/note/edit/70783 Del: integrated -- https://master.php.net/note/delete/70783/integrated Del: useless -- https://master.php.net/note/delete/70783/useless Del: bad code -- https://master.php.net/note/delete/70783/bad+code Del: spam -- https://master.php.net/note/delete/70783/spam Del: non-english -- https://master.php.net/note/delete/70783/non-english Del: in docs -- https://master.php.net/note/delete/70783/in+docs Del: other reasons-- https://master.php.net/note/delete/70783 Reject -- https://master.php.net/note/reject/70783 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119191) next »