You are here

protected function BackgroundImageManager::colorSampleGdImage in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/BackgroundImageManager.php \Drupal\background_image\BackgroundImageManager::colorSampleGdImage()
  2. 2.x src/BackgroundImageManager.php \Drupal\background_image\BackgroundImageManager::colorSampleGdImage()

Determines the average color of an image using the GD toolkit.

Parameters

\Drupal\system\Plugin\ImageToolkit\GDToolkit $image: A GD image toolkit object.

string $default: A default lowercase simple color (HEX) representation to use if unable to sample the image.

Return value

string An associative array with red, green, blue and alpha keys that contains the appropriate values for the specified color index.

1 call to BackgroundImageManager::colorSampleGdImage()
BackgroundImageManager::colorSampleImage in src/BackgroundImageManager.php
Samples the average color of an image file.

File

src/BackgroundImageManager.php, line 277

Class

BackgroundImageManager

Namespace

Drupal\background_image

Code

protected function colorSampleGdImage(GDToolkit $image, $default = NULL) {
  if ($image
    ->apply('resize', [
    'width' => 1,
    'height' => 1,
  ]) && ($resource = $image
    ->getResource())) {
    return @Color::rgbToHex(array_slice(@imagecolorsforindex($resource, @imagecolorat($resource, 0, 0)), 0, 3)) ?: $default;
  }
  return $default;
}