aelvan / Imager-Craft

This plugin has been DEPRECATED. Check out Imager X instead.
MIT License
342 stars 69 forks source link

Get Dominant Colour #196

Closed terryupton closed 6 years ago

terryupton commented 6 years ago

I am trying to get the dominant colour with the following code; {{ craft.imager.getDominantColor(logo, {quality:10, colorValue:'hex'}) }}

However, this results in the following error: Unsupported operand types

/home/vagrant/sites/Plowman Craven V4/plowmancraven.co.uk/craft/plugins/imager/vendor/ksubileau/color-thief-php/lib/ColorThief/ColorThief.php(198)

186             $width  = isset($area['w']) ? $area['w'] : ($width  - $startX);
187             $height = isset($area['h']) ? $area['h'] : ($height - $startY);
188 
189             if ((($startX + $width) > $image->getWidth()) || (($startY + $height) > $image->getHeight())) {
190                 throw new \InvalidArgumentException("Area is out of image bounds.");
191             }
192         }
193 
194         $pixelCount = $width * $height;
195 
196         // Store the RGB values in an array format suitable for quantize function
197         // SplFixedArray is faster and more memory-efficient than normal PHP array.
198         $pixelArray = new SplFixedArray(ceil($pixelCount / $quality));
199 
200         $size = 0;
201         for ($i = 0; $i < $pixelCount; $i = $i + $quality) {
202             $x = $startX + ($i % $width);
203             $y = (int)($startY + $i / $width);
204             $color = $image->getPixelColor($x, $y);
205 
206             if (static::isClearlyVisible($color) && static::isNonWhite($color)) {
207                 $pixelArray[$size++] = static::getColorIndex($color->red, $color->green, $color->blue, 8);
208                 // TODO : Compute directly the histogram here ? (save one iteration over all pixels)
209             }
210         }

Any ideas where I am going wrong?

aelvan commented 6 years ago

Yeah, the error is due to you passing in an object/array as the second parameter of craft.imager.getDominantColor. When the docs say:

craft.imager.getDominantColor(image [, quality=10, colorValue='hex'])

It doesn't mean that the method has two parameters, the second of which is an array. It means that the method has three parameters, and the last two are optional. So the correct syntax would be:

{{ craft.imager.getDominantColor(logo, 10, 'hex') }}
terryupton commented 6 years ago

Thanks @aelvan 👍