Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Palette::fromImagick() #37

Open
vingtcent123 opened this issue Feb 8, 2016 · 2 comments
Open

Palette::fromImagick() #37

vingtcent123 opened this issue Feb 8, 2016 · 2 comments

Comments

@vingtcent123
Copy link

Hello

Here is a proposition for Imagick integration in Palette.



    /**
     * @param \Imagick $image
     *
     * @return Palette
     *
     * @throws \InvalidArgumentException
     */
    public static function fromImagick(\Imagick $image)
    {
        if (!is_object($image) || $image instanceof \Imagick === FALSE) {
            throw new \InvalidArgumentException('Image must be an Imagick instance');
        }

        $palette = new self();
        $palette->colors = [];

        $iterator = $image->getPixelIterator();

        foreach($iterator as $row) {

            foreach($row as $pixel) {

                $colorInfo = $pixel->getColor();
                $color = ($colorInfo['r'] * 65536) + ($colorInfo['g'] * 256) + ($colorInfo['b']);

                isset($palette->colors[$color]) ?
                    $palette->colors[$color] += 1 :
                    $palette->colors[$color] = 1;

            }

        }

        arsort($palette->colors);

        return $palette;
    }
@MatTheCat
Copy link
Member

How about iterating on getImageHistogram?

@MatTheCat
Copy link
Member

Well I'll be testing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants