PHP code example of fredtux / photo-color-names

1. Go to this page and download the library: Download fredtux/photo-color-names library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fredtux / photo-color-names example snippets



### OPTIONAL (depending on settings) ###
// Include autoload - may not be necessary if it has already been done
oColors\PhotoColorOptions;
/// For color naming
use PhotoColorNames\ColorNames\ColorNamesFactory;
use PhotoColorNames\ColorNames\NamePool;
use PhotoColorNames\NameColor;
### END OF OPTIONAL ###

### MAIN ###
// Extract colors
/// Use the factory to retrieve a new object for color extraction
$photoColors = PhotoColorFactory::getColorExtractor('ColorThief'); // Has to be installed via composer - 'GetMostCommonColors' can be used instead
/// Specify options - the most important is the file path
$photoColorOptions = new PhotoColorNames\PhotoColors\PhotoColorExtractorOptions(__DIR__ . '/test/images/test.jpg'); // Assuming there is a test image at this path
// Manually set each option, or set all options except the file path to their default value
$photoColorOptions->setDefaultOptions();

// Name the colors
/// Use the factory to retrieve a new object for color naming
$nameGiver = ColorNamesFactory::getColorNameGiver('ntc');
/// Use a list of names
$nameList = NamePool::getNtcNames();

// Construct object using the 4 variables declared before
$nameColor = new NameColor($photoColors, $photoColorOptions, $nameGiver, $nameList);

// Use the newly created object to get the names of the most frequently used colors in the file
$x = $nameColor->getNamesOfFrequentlyUsedColors();
### END OF MAIN ###

// Print result (not part of MAIN because it's not necessary)
print_r($x);

   PhotoColorFactory::getColorExtractor('ColorExtractorLibrary');
   

   ColorNamesFactory::getColorNameGiver('ColorNamingLibrary');