mirror of
https://github.com/ivuorinen/palette.git
synced 2026-03-13 17:01:40 +00:00
CodeStyle changes, documentation, small fixes
This commit is contained in:
@@ -16,7 +16,9 @@ before_script:
|
|||||||
- phpenv global "$TRAVIS_PHP_VERSION"
|
- phpenv global "$TRAVIS_PHP_VERSION"
|
||||||
- composer config extra.platform.php $TRAVIS_PHP_VERSION
|
- composer config extra.platform.php $TRAVIS_PHP_VERSION
|
||||||
- composer require php-coveralls/php-coveralls
|
- composer require php-coveralls/php-coveralls
|
||||||
- composer install
|
install:
|
||||||
|
- flags="--ansi --prefer-dist --no-interaction --optimize-autoloader --no-suggest --no-progress"
|
||||||
|
- composer install $flags
|
||||||
script:
|
script:
|
||||||
- ./vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml
|
- ./vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml
|
||||||
after_script:
|
after_script:
|
||||||
|
|||||||
@@ -23,6 +23,6 @@
|
|||||||
"php": ">=5.6.0"
|
"php": ">=5.6.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^5.7 || ^6.3"
|
"phpunit/phpunit": "~5.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<phpunit backupGlobals="false"
|
<phpunit backupGlobals="false"
|
||||||
backupStaticAttributes="false"
|
backupStaticAttributes="false"
|
||||||
colors="true"
|
colors="true"
|
||||||
@@ -8,17 +7,15 @@
|
|||||||
convertWarningsToExceptions="true"
|
convertWarningsToExceptions="true"
|
||||||
processIsolation="false"
|
processIsolation="false"
|
||||||
stopOnFailure="false"
|
stopOnFailure="false"
|
||||||
syntaxCheck="false"
|
|
||||||
bootstrap="vendor/autoload.php"
|
bootstrap="vendor/autoload.php"
|
||||||
>
|
>
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite name="Test Suite">
|
<testsuite name="Test Suite">
|
||||||
<directory suffix="Test.php" phpVersion="5.6.0" phpVersionOperator=">=">./tests</directory>
|
<directory suffix="Test.php">./tests</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist>
|
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||||
<directory>./src/</directory>
|
<directory>./src/</directory>
|
||||||
</whitelist>
|
</whitelist>
|
||||||
</filter>
|
</filter>
|
||||||
|
|||||||
@@ -41,45 +41,26 @@ namespace ivuorinen\Palette;
|
|||||||
/**
|
/**
|
||||||
* Palette
|
* Palette
|
||||||
*
|
*
|
||||||
* @category Default
|
|
||||||
* @package Palette
|
|
||||||
* @author Ismo Vuorinen <ivuorinen@me.com>
|
* @author Ismo Vuorinen <ivuorinen@me.com>
|
||||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||||
* @link https://github.com/ivuorinen/palette
|
* @link https://github.com/ivuorinen/palette
|
||||||
* @version 1.0.0
|
|
||||||
* @example example/example.php Usage examples
|
* @example example/example.php Usage examples
|
||||||
**/
|
**/
|
||||||
class Palette
|
class Palette
|
||||||
{
|
{
|
||||||
/**
|
/** @var int Precision of palette, higher is more precise */
|
||||||
* Precision of palette, higher is more precise
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
public $precision;
|
public $precision;
|
||||||
|
|
||||||
/**
|
/** @var int Number of colors to return */
|
||||||
* Number of colors to return
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
public $returnColors;
|
public $returnColors;
|
||||||
|
|
||||||
/**
|
/** @var array Array of colors we use internally */
|
||||||
* Array of colors we use internally
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
public $colorsArray;
|
public $colorsArray;
|
||||||
|
|
||||||
/**
|
/** @var string Full path to image file name */
|
||||||
* Full path to image file name
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $filename;
|
public $filename;
|
||||||
|
|
||||||
/**
|
/** @var string Destination for .json-file, full path and filename */
|
||||||
* Destination for .json-file, full path and filename
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
**/
|
|
||||||
public $destination;
|
public $destination;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,23 +71,25 @@ class Palette
|
|||||||
* and saves the result as an json-file to datafiles -folder.
|
* and saves the result as an json-file to datafiles -folder.
|
||||||
*
|
*
|
||||||
* @param string $filename Full path to image
|
* @param string $filename Full path to image
|
||||||
**/
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
public function __construct($filename = null)
|
public function __construct($filename = null)
|
||||||
{
|
{
|
||||||
// Define shortcut to directory separator
|
// Define shortcut to directory separator
|
||||||
if (! defined('DS')) {
|
if (!defined('DS')) {
|
||||||
define("DS", DIRECTORY_SEPARATOR);
|
define('DS', DIRECTORY_SEPARATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->precision = 10;
|
$this->precision = 10;
|
||||||
$this->returnColors = 10;
|
$this->returnColors = 10;
|
||||||
$this->colorsArray = array();
|
$this->colorsArray = array();
|
||||||
$this->filename = $filename;
|
$this->filename = $filename;
|
||||||
$this->destination = dirname(__FILE__)
|
$this->destination = __DIR__
|
||||||
. DS . 'datafiles'
|
. DS . 'datafiles'
|
||||||
. DS . basename($filename) . '.json';
|
. DS . basename($filename) . '.json';
|
||||||
|
|
||||||
if (! empty($this->filename)) {
|
if (!empty($this->filename)) {
|
||||||
$this->run();
|
$this->run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,11 +101,12 @@ class Palette
|
|||||||
* settings and after that run the palette generation and saving
|
* settings and after that run the palette generation and saving
|
||||||
*
|
*
|
||||||
* @return bool Returns true always
|
* @return bool Returns true always
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
if (empty($this->destination)) {
|
if (empty($this->destination)) {
|
||||||
throw new Exception("No destination provided, can't save.");
|
throw new \ErrorException("No destination provided, can't save.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->getPalette();
|
$this->getPalette();
|
||||||
@@ -136,22 +120,23 @@ class Palette
|
|||||||
* Returns colors used in an image specified in $filename
|
* Returns colors used in an image specified in $filename
|
||||||
*
|
*
|
||||||
* @return array|bool If we get array that has colors return the array
|
* @return array|bool If we get array that has colors return the array
|
||||||
**/
|
* @throws \Exception
|
||||||
|
*/
|
||||||
public function getPalette()
|
public function getPalette()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// We check for input
|
// We check for input
|
||||||
if (empty($this->filename)) {
|
if (empty($this->filename)) {
|
||||||
throw new \Exception("Image was not provided");
|
throw new \ErrorException('Image was not provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
// We check for readability
|
// We check for readability
|
||||||
if (! is_readable($this->filename)) {
|
if (!is_readable($this->filename)) {
|
||||||
throw new \Exception("Image {$this->filename} is not readable");
|
throw new \ErrorException("Image {$this->filename} is not readable");
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\ErrorException $e) {
|
||||||
user_error($e->getMessage(), E_USER_ERROR);
|
user_error($e->getMessage(), E_USER_ERROR);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -159,7 +144,7 @@ class Palette
|
|||||||
|
|
||||||
$this->colorsArray = $this->countColors();
|
$this->colorsArray = $this->countColors();
|
||||||
|
|
||||||
if (! empty($this->colorsArray) and is_array($this->colorsArray)) {
|
if (!empty($this->colorsArray) && is_array($this->colorsArray)) {
|
||||||
return $this->colorsArray;
|
return $this->colorsArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,24 +154,24 @@ class Palette
|
|||||||
/**
|
/**
|
||||||
* countColors returns an array of colors in the image
|
* countColors returns an array of colors in the image
|
||||||
*
|
*
|
||||||
* @return array Array of colors sorted by times used
|
* @return array|boolean Array of colors sorted by times used
|
||||||
*/
|
*/
|
||||||
private function countColors()
|
private function countColors()
|
||||||
{
|
{
|
||||||
$this->precision = max(1, abs((int) $this->precision));
|
$this->precision = max(1, abs((int)$this->precision));
|
||||||
$colors = array();
|
$colors = array();
|
||||||
|
|
||||||
// Test for image type
|
// Test for image type
|
||||||
$img = $this->imageTypeToResource();
|
$img = $this->imageTypeToResource();
|
||||||
|
|
||||||
if (! $img && $img !== null) {
|
if (!$img && $img !== null) {
|
||||||
user_error("Unable to open: {$this->filename}", E_USER_ERROR);
|
user_error("Unable to open: {$this->filename}", E_USER_ERROR);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get image size and check if it's really an image
|
// Get image size and check if it's really an image
|
||||||
$size = @getimagesize($this->filename);
|
$size = @getimagesize($this->filename);
|
||||||
|
|
||||||
if ($size === false) {
|
if ($size === false) {
|
||||||
user_error("Unable to get image size data: {$this->filename}", E_USER_ERROR);
|
user_error("Unable to get image size data: {$this->filename}", E_USER_ERROR);
|
||||||
@@ -194,14 +179,16 @@ class Palette
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is pretty naive approach,
|
||||||
|
// but looping through the image is only way I thought of
|
||||||
for ($x = 0; $x < $size[0]; $x += $this->precision) {
|
for ($x = 0; $x < $size[0]; $x += $this->precision) {
|
||||||
for ($y = 0; $y < $size[1]; $y += $this->precision) {
|
for ($y = 0; $y < $size[1]; $y += $this->precision) {
|
||||||
$thisColor = imagecolorat($img, $x, $y);
|
$thisColor = imagecolorat($img, $x, $y);
|
||||||
$rgb = imagecolorsforindex($img, $thisColor);
|
$rgb = imagecolorsforindex($img, $thisColor);
|
||||||
$red = round(round(($rgb['red'] / 0x33)) * 0x33);
|
$red = round(round($rgb['red'] / 0x33) * 0x33);
|
||||||
$green = round(round(($rgb['green'] / 0x33)) * 0x33);
|
$green = round(round($rgb['green'] / 0x33) * 0x33);
|
||||||
$blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
|
$blue = round(round($rgb['blue'] / 0x33) * 0x33);
|
||||||
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
|
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
|
||||||
|
|
||||||
if (array_key_exists($thisRGB, $colors)) {
|
if (array_key_exists($thisRGB, $colors)) {
|
||||||
$colors[$thisRGB]++;
|
$colors[$thisRGB]++;
|
||||||
@@ -219,17 +206,15 @@ class Palette
|
|||||||
* save
|
* save
|
||||||
* Get array of colors, json_encode it and save to destination
|
* Get array of colors, json_encode it and save to destination
|
||||||
*
|
*
|
||||||
* @param string $destination Where to save json-file
|
* @return bool
|
||||||
*
|
* @throws \Exception
|
||||||
* @return false
|
*/
|
||||||
**/
|
|
||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Check for destination
|
// Check for destination
|
||||||
if (empty($this->destination)) {
|
if (empty($this->destination)) {
|
||||||
throw new \Exception("No destination given for save");
|
throw new \InvalidArgumentException('No destination given for save');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check destination writability
|
// Check destination writability
|
||||||
@@ -237,10 +222,10 @@ class Palette
|
|||||||
|
|
||||||
// Check for data we should write
|
// Check for data we should write
|
||||||
if (empty($this->colorsArray)) {
|
if (empty($this->colorsArray)) {
|
||||||
throw new \Exception("Couldn't detect colors from image: {$this->filename}");
|
throw new \ErrorException("Couldn't detect colors from image: {$this->filename}");
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\ErrorException $e) {
|
||||||
user_error($e->getMessage(), E_USER_ERROR);
|
user_error($e->getMessage(), E_USER_ERROR);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -251,10 +236,8 @@ class Palette
|
|||||||
|
|
||||||
// Save and return the result of save operation
|
// Save and return the result of save operation
|
||||||
file_put_contents($this->destination, $colorsData);
|
file_put_contents($this->destination, $colorsData);
|
||||||
if (is_readable($this->destination)) {
|
|
||||||
return true;
|
return is_readable($this->destination);
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -270,23 +253,17 @@ class Palette
|
|||||||
$type = exif_imagetype($this->filename);
|
$type = exif_imagetype($this->filename);
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case '1': // IMAGETYPE_GIF
|
case '1': // IMAGETYPE_GIF
|
||||||
$img = @imagecreatefromgif($this->filename);
|
return @imagecreatefromgif($this->filename);
|
||||||
break;
|
|
||||||
case '2': // IMAGETYPE_JPEG
|
case '2': // IMAGETYPE_JPEG
|
||||||
$img = @imagecreatefromjpeg($this->filename);
|
return @imagecreatefromjpeg($this->filename);
|
||||||
break;
|
|
||||||
case '3': // IMAGETYPE_PNG
|
case '3': // IMAGETYPE_PNG
|
||||||
$img = @imagecreatefrompng($this->filename);
|
return @imagecreatefrompng($this->filename);
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
$image_type_code = image_type_to_mime_type($type);
|
$image_type_code = image_type_to_mime_type($type);
|
||||||
user_error("Unknown image type: {$image_type_code} ({$type}): {$this->filename}");
|
user_error("Unknown image type: {$image_type_code} ({$type}): {$this->filename}");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $img;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -297,7 +274,8 @@ class Palette
|
|||||||
* - Is it writable?
|
* - Is it writable?
|
||||||
* - Can it be made writable?
|
* - Can it be made writable?
|
||||||
*
|
*
|
||||||
* @return bool|exception True or false, with exceptions
|
* @return boolean True or false, with exceptions
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
private function checkDestination()
|
private function checkDestination()
|
||||||
{
|
{
|
||||||
@@ -305,14 +283,10 @@ class Palette
|
|||||||
|
|
||||||
// Test if we have destination directory
|
// Test if we have destination directory
|
||||||
try {
|
try {
|
||||||
if (! file_exists($destination_dir)) {
|
if (!@mkdir($destination_dir, 0755) && !is_dir($destination_dir)) {
|
||||||
mkdir($destination_dir, 0755);
|
throw new \ErrorException("Couldn't create missing destination dir: {$destination_dir}");
|
||||||
}
|
}
|
||||||
|
} catch (\ErrorException $e) {
|
||||||
if (! file_exists($destination_dir)) {
|
|
||||||
throw new \Exception("Couldn't create missing destination dir: {$destination_dir}");
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
user_error($e->getMessage());
|
user_error($e->getMessage());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -325,10 +299,10 @@ class Palette
|
|||||||
}
|
}
|
||||||
chmod($destination_dir, 0755);
|
chmod($destination_dir, 0755);
|
||||||
|
|
||||||
if (! is_writable($destination_dir)) {
|
if (!is_writable($destination_dir)) {
|
||||||
throw new \Exception("Destination directory not writable: {$destination_dir}");
|
throw new \ErrorException("Destination directory not writable: {$destination_dir}");
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\ErrorException $e) {
|
||||||
user_error($e->getMessage());
|
user_error($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class PaletteTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
foreach ($images as $imageFile => $hex) {
|
foreach ($images as $imageFile => $hex) {
|
||||||
$image = $location . $imageFile;
|
$image = $location . $imageFile;
|
||||||
$this->assertTrue(file_exists($image));
|
$this->assertFileExists($image);
|
||||||
|
|
||||||
$palette = new \ivuorinen\Palette\Palette($image);
|
$palette = new \ivuorinen\Palette\Palette($image);
|
||||||
$this->assertCount(1, $palette->colorsArray);
|
$this->assertCount(1, $palette->colorsArray);
|
||||||
|
|||||||
Reference in New Issue
Block a user