public static getPalette ( mixed $sourceImage, integer $colorCount = 10, integer $quality = 10, array $area = null ) : array | ||
$sourceImage | mixed | Path/URL to the image, GD resource, Imagick instance, or image as binary string |
$colorCount | integer | It determines the size of the palette; the number of colors returned. |
$quality | integer | 1 is the highest quality. |
$area | array | |
return | array |
public static function getPalette($sourceImage, $colorCount = 10, $quality = 10, array $area = null)
{
if ($colorCount < 2 || $colorCount > 256) {
throw new \InvalidArgumentException("The number of palette colors must be between 2 and 256 inclusive.");
}
if ($quality < 1) {
throw new \InvalidArgumentException("The quality argument must be an integer greater than one.");
}
$pixelArray = static::loadImage($sourceImage, $quality, $area);
if (!count($pixelArray)) {
throw new \RuntimeException("Unable to compute the color palette of a blank or transparent image.", 1);
}
// Send array to quantize function which clusters values
// using median cut algorithm
$cmap = static::quantize($pixelArray, $colorCount);
$palette = $cmap->palette();
return $palette;
}
/** * Gets color palette for image * * @param AssetFileModel|string $image * @param $colorCount * @param $quality * @param $colorValue * @return array * @throws Exception */ public function getColorPalette($image, $colorCount, $quality, $colorValue) { $pathsModel = new Imager_ImagePathsModel($image); if (!IOHelper::getRealPath($pathsModel->sourcePath)) { throw new Exception(Craft::t('Source folder “{sourcePath}” does not exist', array('sourcePath' => $pathsModel->sourcePath))); } if (!IOHelper::fileExists($pathsModel->sourcePath . $pathsModel->sourceFilename)) { throw new Exception(Craft::t('Requested image “{fileName}” does not exist in path “{sourcePath}”', array('fileName' => $pathsModel->sourceFilename, 'sourcePath' => $pathsModel->sourcePath))); } $palette = ColorThief::getPalette($pathsModel->sourcePath . $pathsModel->sourceFilename, $colorCount, $quality); return $colorValue == 'hex' ? $this->_paletteToHex($palette) : $palette; }