ColorThief\Image\ImageLoader::load PHP Method

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
    public function load($source)
    {
        $image = null;
        if (is_string($source)) {
            if ($this->isImagickLoaded()) {
                $image = $this->getAdapter("Imagick");
            } elseif ($this->isGmagickLoaded()) {
                $image = $this->getAdapter("Gmagick");
            } else {
                $image = $this->getAdapter("GD");
            }
            // Tries to detect if the source string is a binary string or a path to an existing file
            // This test is based on the way that PHP detects an invalid path and throws a warning
            // saying "xxx expects to be a valid path, string given" (see zend_parse_arg_path_str).
            if (strpos($source, "") !== false) {
                // Binary string
                $image->loadBinaryString($source);
            } else {
                // Path or URL
                $is_remote = filter_var($source, FILTER_VALIDATE_URL);
                if (!$is_remote && (!file_exists($source) || !is_readable($source))) {
                    throw new \RuntimeException("Image '" . $source . "' is not readable or does not exists.");
                }
                $image->loadFile($source);
            }
        } else {
            if (is_resource($source) && get_resource_type($source) == 'gd') {
                $image = $this->getAdapter("GD");
            } elseif (is_a($source, 'Imagick')) {
                $image = $this->getAdapter("Imagick");
            } elseif (is_a($source, 'Gmagick')) {
                $image = $this->getAdapter("Gmagick");
            } else {
                throw new \InvalidArgumentException("Passed variable is not a valid image source");
            }
            $image->load($source);
        }
        return $image;
    }

Usage Example

 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;
 }
All Usage Examples Of ColorThief\Image\ImageLoader::load