0

I'my trying to use color-extractor to get the colours of my images but I'm having trouble getting it working.

I noticed a missing autoload.php file in the package and after some googling it seems it requires that you use Composer. I haven't used composer and don't have much experience using the command line yet. Something I am working on but hoping not to have to learn it all before using this php package.

I tried changing some of the php lines from this:

require 'vendor/autoload.php';

use League\ColorExtractor\Color;
use League\ColorExtractor\ColorExtractor;
use League\ColorExtractor\Palette;

to this:

require ..\lib\League\ColorExtractor\Color;
require ..\lib\League\ColorExtractor\ColorExtractor;
require ..\lib\League\ColorExtractor\Palette;

But it didn't work and I got these errors:

[14-Jan-2019 07:00:43 Australia/Sydney] PHP Fatal error:  require(): Failed opening required 'lib/League/ColorExtractor/Color.php' (include_path='.:/opt/alt/php56/usr/share/pear:/opt/alt/php56/usr/share/php') in /home/windowvi/public_html/arena/examples/grid2/php/get_collection.php on line 3
[14-Jan-2019 07:07:14 Australia/Sydney] PHP Fatal error:  Class 'Palette' not found in /home/windowvi/public_html/arena/examples/grid2/php/get_collection.php on line 55

Can this package be used without learning and using composer and if so, how would I require/include the files?

Thanks!

Joe Hamilton
  • 665
  • 2
  • 9
  • 19
  • 1
    you should be able to manually include/require all the classes/files, yes. Where did you copy the lib files to? – Jeff Jan 13 '19 at 20:44
  • Composer is standardized dependency manager for PHP. Is very easy and you can use it in any platform. Why you avoid usage? – SilvioQ Jan 13 '19 at 20:48
  • Hi Jeff, ok great. It must be something else I'm doing wrong. I've copied the lib files to: joehamilton.info/arena/examples/grid2/lib/League/ColorExtractor And php file that tries to reqire it is here: joehamilton.info/arena/examples/grid2 SilvioQ - I'm sure it is easy for more experienced programmers who are familiar with using the command line. – Joe Hamilton Jan 13 '19 at 21:31
  • Possible duplicate of https://stackoverflow.com/questions/22388647/how-to-use-a-php-library-with-namespacing-without-composer-as-dependency-psr-0. You only need an autoloader and the right file structure. –  Jan 13 '19 at 22:40
  • 1
    An autoloader might be preferred in most cases, it is however not at all a necessity in this case. The source files can be required and used without much effort. – lovelace Jan 13 '19 at 22:45

1 Answers1

2

Hopefully this will help you on your way.

Create a project folder called e.g. ‘colorextractor’

Copy-paste the 3 files from thephpleague/color-extractor/src/League/ColorExtractor

  1. Color.php
  2. ColorExtractor.php
  3. Palette.php

Into your project folder.

Then create an index.php file (see below) that will run the examples from the README at thephpleague/color-extractor – to ensure it all works as expected.

Your project folder should have the following content: enter image description here

Note: I used a ‘testimage.png’ to test the package in index.php

index.php

<?php
// import package namespaces
use League\ColorExtractor\Color;
use League\ColorExtractor\ColorExtractor;
use League\ColorExtractor\Palette;

// if you don't use an autoloader
// you need to require the package files
require __DIR__ . "/Color.php";
require __DIR__ . "/ColorExtractor.php";
require __DIR__ . "/Palette.php";

// the example from the README at ColorExtractor
$palette = Palette::fromFilename('./testimage.png');
// $palette is an iterator on colors sorted by pixel count
foreach($palette as $color => $count) {
    // colors are represented by integers
    echo Color::fromIntToHex($color), ': ', $count, "\n";
}
echo '<br />';
// it offers some helpers too
$topFive = $palette->getMostUsedColors(5);
echo '<br />';
echo 'top 5 most used colors:';
echo '<pre>';
print_r($topFive);
echo '</pre>';

$colorCount = count($palette);
echo '<br />';
echo "color count: " . $colorCount;
echo '<br />';

// this example gave me a 'notice: undefined offset'
//$blackCount = $palette->getColorCount(Color::fromHexToInt('#000000'));
//echo '<br />';
//echo "black count " . $blackCount;


// an extractor is built from a palette
$extractor = new ColorExtractor($palette);
// it defines an extract method which return the most “representative” colors
$colors = $extractor->extract(5);
echo '<br />';
echo 'most representative colors:';
echo '<pre>';
print_r($colors);
echo '</pre>';
lovelace
  • 1,195
  • 1
  • 7
  • 10
  • Thank you. This worked. My mistake was not realising that I needed to keep the 'use' lines. – Joe Hamilton Jan 14 '19 at 07:32
  • 1
    Cool, happy it works. For what it's worth, it might be worth your while though to check out Composer at some point. Good luck. – lovelace Jan 14 '19 at 07:59