PHP imagick get picture RGB color value _php Tips

Source: Internet
Author: User

Many image sites will be based on the user uploaded pictures to retrieve the main color value of the picture, and then search through the color related pictures.

Before the online method to zoom (or mosaic) and then iterate over each pixel, and then the statistics at the highest number of RGB, this approach is too inefficient and the RGB value taken is not accurate. It was later found that using Imagick's Quantizeimage method was a convenient way to get the average RGB value in the picture.

$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) {
//www.jb51.net
$colorarr [] = $pixe L->getcolor ();
}
return $colorarr;
}
$colorarr = Getimagescolor ($average);
foreach ($colorarr as $val) {
echo ' <div style= ' Background-color:rgb ({$val [' R ']},{$val [' G ']},{$val [' B ']}); Width:50px;height:50px;float:left; ' ></div> ";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.