load() public method

public load ( mixed $source ) : ColorThief\Image\Adapter\ImageAdapter
$source mixed Path/URL to the image, GD resource, Imagick instance, or image as binary string
return ColorThief\Image\Adapter\ImageAdapter
Example #1
0
 private static function loadImage($sourceImage, $quality, array $area = null)
 {
     $loader = new ImageLoader();
     $image = $loader->load($sourceImage);
     $startx = 0;
     $starty = 0;
     $width = $image->getWidth();
     $height = $image->getHeight();
     if ($area) {
         $startx = isset($area['x']) ? $area['x'] : 0;
         $starty = isset($area['y']) ? $area['y'] : 0;
         $width = isset($area['w']) ? $area['w'] : $width - $startx;
         $height = isset($area['h']) ? $area['h'] : $height - $starty;
         if ($startx + $width > $image->getWidth() || $starty + $height > $image->getHeight()) {
             throw new \InvalidArgumentException("Area is out of image bounds.");
         }
     }
     $pixelCount = $width * $height;
     // Store the RGB values in an array format suitable for quantize function
     // SplFixedArray is faster and more memory-efficient than normal PHP array.
     $pixelArray = new SplFixedArray(ceil($pixelCount / $quality));
     $j = 0;
     for ($i = 0; $i < $pixelCount; $i = $i + $quality) {
         $x = $startx + $i % $width;
         $y = (int) ($starty + $i / $width);
         $color = $image->getPixelColor($x, $y);
         // If pixel is mostly opaque and not white
         if ($color->alpha <= 62) {
             if (!($color->red > 250 && $color->green > 250 && $color->blue > 250)) {
                 $pixelArray[$j++] = self::getColorIndex($color->red, $color->green, $color->blue, 8);
                 // TODO : Compute directly the histogram here ? (save one iteration over all pixels)
             }
         }
     }
     $pixelArray->setSize($j);
     // Don't destroy a ressource passed by the user !
     if (is_string($sourceImage)) {
         $image->destroy();
     }
     return $pixelArray;
 }
Example #2
0
 private static function loadImage($sourceImage, $quality)
 {
     $loader = new ImageLoader();
     $image = $loader->load($sourceImage);
     $width = $image->getWidth();
     $height = $image->getHeight();
     $pixelCount = $width * $height;
     // Store the RGB values in an array format suitable for quantize function
     // SplFixedArray is faster and more memory-efficient than normal PHP array.
     $pixelArray = new SplFixedArray(ceil($pixelCount / $quality));
     $j = 0;
     for ($i = 0; $i < $pixelCount; $i = $i + $quality) {
         $x = $i % $width;
         $y = (int) ($i / $width);
         $color = $image->getPixelColor($x, $y);
         // If pixel is mostly opaque and not white
         if ($color->alpha <= 62) {
             if (!($color->red > 250 && $color->green > 250 && $color->blue > 250)) {
                 $pixelArray[$j++] = self::getColorIndex($color->red, $color->green, $color->blue, 8);
             }
         }
     }
     $pixelArray->setSize($j);
     // Don't destroy a ressource passed by the user !
     if (is_string($sourceImage)) {
         $image->destroy();
     }
     return $pixelArray;
 }
Example #3
0
 /**
  * @param mixed $sourceImage Path/URL to the image, GD resource, Imagick instance, or image as binary string
  * @param int $quality
  * @param array|null $area
  * @return SplFixedArray
  */
 private static function loadImage($sourceImage, $quality, array $area = null)
 {
     $loader = new ImageLoader();
     $image = $loader->load($sourceImage);
     $startX = 0;
     $startY = 0;
     $width = $image->getWidth();
     $height = $image->getHeight();
     if ($area) {
         $startX = isset($area['x']) ? $area['x'] : 0;
         $startY = isset($area['y']) ? $area['y'] : 0;
         $width = isset($area['w']) ? $area['w'] : $width - $startX;
         $height = isset($area['h']) ? $area['h'] : $height - $startY;
         if ($startX + $width > $image->getWidth() || $startY + $height > $image->getHeight()) {
             throw new \InvalidArgumentException("Area is out of image bounds.");
         }
     }
     $pixelCount = $width * $height;
     // Store the RGB values in an array format suitable for quantize function
     // SplFixedArray is faster and more memory-efficient than normal PHP array.
     $pixelArray = new SplFixedArray(ceil($pixelCount / $quality));
     $size = 0;
     for ($i = 0; $i < $pixelCount; $i = $i + $quality) {
         $x = $startX + $i % $width;
         $y = (int) ($startY + $i / $width);
         $color = $image->getPixelColor($x, $y);
         if (static::isClearlyVisible($color) && static::isNonWhite($color)) {
             $pixelArray[$size++] = static::getColorIndex($color->red, $color->green, $color->blue, 8);
             // TODO : Compute directly the histogram here ? (save one iteration over all pixels)
         }
     }
     $pixelArray->setSize($size);
     // Don't destroy a resource passed by the user !
     // TODO Add a method in ImageLoader to know if the image should be destroy (or to know the detected image source type)
     if (is_string($sourceImage)) {
         $image->destroy();
     }
     return $pixelArray;
 }