Use Imagick in php to implement the image histogram.

Source: Internet
Author: User
People who have played SLR cameras should know the image histogram (ImageHistogram). Simply put, it calculates the proportion of each color level in the total pixel to reflect the image exposure. People who have played SLR cameras should know the Image Histogram. Simply put, it calculates the proportion of each color level in the total pixel to reflect the Image exposure.

I am not going to explain the terms in detail. if you are interested, you can refer to the reference link at the end of the article. there are easy-to-understand explanations:

Let's first look for an example image (shot with Canon 550D ):
Let's take a look at how to use Imagick to implement the image histogram:

  256, 'height' => 100, ); $image = new Imagick($file); $histogram = array_fill_keys(range(0, 255), 0); foreach ($image->getImageHistogram() as $pixel) { $rgb = $pixel->getColor(); $histogram[$rgb['r']] += $pixel->getColorCount(); $histogram[$rgb['g']] += $pixel->getColorCount(); $histogram[$rgb['b']] += $pixel->getColorCount(); } $max = max($histogram); $threshold = ($image->getImageWidth() * $image->getImageHeight()) / 256 * 12; if ($max > $threshold) { $max = $threshold; } $image = new Imagick(); $draw = new ImagickDraw(); $image->newImage($size['width'], $size['height'], 'white'); foreach ($histogram as $x => $count) { if ($count == 0) { continue; } $draw->setStrokeColor('black'); $height = min($count, $max) / $max * $size['height']; $draw->line($x, $size['height'], $x, $size['height'] - $height); $image->drawImage($draw); $draw->clear(); } $image->setImageFormat('png'); $image->writeImage('histogram.png'); ?>

Note: The reason why the $ threshold value is added to the code is that sometimes the values of certain levels may be very large. if not processed, the final generation effect will be affected. There is no reason why we should first divide 256 and then multiply 12. it's all determined by my head. you can also use other methods.
The final histogram is basically the same as that of Photoshop. here we will post the Photoshop:
Finally, if you are interested in photography, refer to: how to interpret the histogram of a digital camera.

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.