The PHP manual has the following instructions: imagecolorat (PHP4, PHP5) imagecoloratGettheindexofthecolorofapixel? Php?imimagecreatefrompng(php.png); $ rgbimagecolorat ($ im,); $ r ($ rgb16) 0xFF; $ g ($ rgb8) 0xFF; $ B $ rg
The PHP manual has the following instructions: imagecolorat (PHP 4, PHP 5) imagecolorat Get the index of the color of a pixel? Php $ im = imagecreatefrompng (php.png); $ rgb = imagecolorat ($ im, 10, 15); $ r = ($ rgb 16) 0xFF; $ g = ($ rgb 8) 0xFF; $ B = $ rg
The PHP Manual provides the following instructions:
Imagecolorat
(PHP 4, PHP 5)
Imagecolorat-Get the index of the color of a pixel
$ Im = imagecreatefrompng (“php.png ");
$ Rgb = imagecolorat ($ im, 10, 15 );
$ R = ($ rgb> 16) & 0xFF;
$ G = ($ rgb> 8) & 0xFF;
$ B = $ rgb & 0xFF;
Var_dump ($ r, $ g, $ B );
?>
Therefore, you can write a special processing function:
Function getImageMainColor ($ strUrl ){
$ ImageInfo = getimagesize ($ strUrl );
// Image type
$ ImgType = strtolower (substr (image_type_to_extension ($ imageInfo [2]), 1 ));
// Corresponding function
$ ImageFun = 'imagecreatefrom '. ($ imgType = 'jpg '? 'Jpeg ': $ imgType );
$ I = $ imageFun ($ strUrl );
// Cyclic color value
$ RColorNum = $ gColorNum = $ bColorNum = $ total = 0;
For ($ x = 0; $ x
For ($ y = 0; $ y
$ Rgb = imagecolorat ($ I, $ x, $ y );
// Three channels
$ R = ($ rgb> 16) & 0xFF;
$ G = ($ rgb >>& 0xFF;
$ B = $ rgb & 0xFF;
$ RColorNum + = $ r;
$ GColorNum + = $ g;
$ BColorNum + = $ B;
$ Total ++;
}
}
$ Rgb = array ();
$ Rgb ['R'] = round ($ rColorNum/$ total );
$ Rgb ['G'] = round ($ gColorNum/$ total );
$ Rgb ['B'] = round ($ bColorNum/$ total );
Return $ rgb;
}
Original article address: How php recognizes the main color of the image, thanks to the original author for sharing.