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

Bug. Warning: max(): Array must contain at least one element #6

Open
Ne-Lexa opened this issue Jun 29, 2017 · 1 comment
Open

Bug. Warning: max(): Array must contain at least one element #6

Ne-Lexa opened this issue Jun 29, 2017 · 1 comment

Comments

@Ne-Lexa
Copy link

Ne-Lexa commented Jun 29, 2017

An array of colors can be empty when there are no colors in the image.
In the function max and min, you can not pass an empty array, otherwise the function will return false and throw warning.

Stacktrace:

Warning: max(): Array must contain at least one element

Exception trace:
 () at vendor/marijnvdwerf/material-palette/src/ColorCutQuantizer.php:101
 marijnvdwerf\palette\Vbox->getVolume() at vendor/marijnvdwerf/material-palette/src/ColorCutQuantizer.php:39
 marijnvdwerf\palette\ColorCutQuantizer->quantize() at vendor/marijnvdwerf/material-palette/src/PaletteGenerator.php:47
 marijnvdwerf\palette\PaletteGenerator->generate() at vendor/marijnvdwerf/material-palette/src/Palette.php:59

Sample code

$manager = new ImageManager();
$image = $manager->make('filename');
$palette = Palette::generate($image);
$palette->getVibrantSwatch()->getColor();

Problem image

icon

@on2
Copy link
Contributor

on2 commented Oct 13, 2019

I've taken a look at this and I can only replicate the error when using the problem image. If I try to recreate the image the recreated image works fine. I note as well that I couldn't load the problem image in MacOS's Preview application so I'm thinking it might be the image at fault.

That said, the findSplitPoint() method in the ColorCutQuantizer class returns 0 for this image at some point during the processing:

private function findSplitPoint()
{
$longestDimension = $this->getLongestColorDimension();
usort($this->swatches, self::sortSwatchesByComponent($longestDimension));
$dimensionMidpoint = $this->midPoint($longestDimension);
for ($i = 0; $i < count($this->swatches); $i++) {
$swatch = $this->swatches[$i];
if ($swatch->getColor()->asRGBColor()->$longestDimension >= $dimensionMidpoint) {
return $i;
}
}
return 0;
}

When Vbox::split() is called later the first section contains no swatches which causes the error as the array is empty.

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