Two ways to get Image color values in PHP
<?php$i=imagecreatefromjpeg ("photo3.jpg");//test picture for ($x =0; $x <imagesx ($i); $x + +) {for ($y =0; $y <imagesy ($i) ; $y + +) {$rgb = Imagecolorat ($i, $x, $y); $r = ($rgb >>16) & 0xFF; $r = ($rgb >>16) & 0xFF; $g = ($rgb >> & Amp 0xFF, $b = $rgb & 0xFF, $rTotal + = $r, $gTotal + = $g, $bTotal + = $b, $total + +;}} $rAverage = Round ($rTotal/$total); $gAverage = round ($gTotal/$total); $bAverage = round ($bTotal/$total);//Example: Echo $ Raverage;? >
Example 2,php gets the image primary RGB color value. Retrieves the primary color value of the image based on the image uploaded by the user, and then searches the relevant image by color.
The image is scaled (or mosaic) on the web and then traversed by each pixel, and then the most RGB values are counted, which is too inefficient and the RGB values taken are not accurate enough. It was later found that the Quantizeimage method using Imagick can easily fetch the average RGB value in the image. (bbs.it-home.org Script Academy) Code: Php$average = new Imagick ("xiaocai.jpg"); $average->quantizeimage (Imagick::colorspace_rgb, 0, False, false); $ Average->uniqueimagecolors (); function Getimagescolor (Imagick $im) {$colorarr = array (); $it = $im Getpixeliterator (); $it->resetiterator (); while ($row = $it->getnextiteratorrow ()) {foreach ($row as $pixel) {$ colorarr[] = $pixel->getcolor ();}} Bbs.it-home.orgreturn $colorarr;} $colorarr = Getimagescolor ($average); foreach ($colorarr as $val) {echo "";}
Output Result:
|