mirror of
https://github.com/ivuorinen/palette.git
synced 2026-02-28 09:56:04 +00:00
.gitignore:
- Added vendor folder
composer.json:
- Added required description
- Fixed autoloader classmap
- Removed extra-section
example.php:
- Now has working autoloader and non-autoloader examples
- Better wording in comments and caching example, also includes destination configuration example
Palette.php:
- Added MIT license block and changed the license links from GPL2 to MIT to reflect composer.json file
- Moved file from from a directory to a better one, reflecting class namespace.
- Combined tests to fewer try {} catch {} -blocks
- Added comments to help understand the code
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*.json
|
*.json
|
||||||
!composer.json
|
!composer.json
|
||||||
|
vendor
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ivuorinen/palette",
|
"name": "ivuorinen/palette",
|
||||||
|
"description": "Get most used colors from an image",
|
||||||
"keywords": ["image", "colors", "palette", "psr-2"],
|
"keywords": ["image", "colors", "palette", "psr-2"],
|
||||||
"homepage": "https://github.com/ivuorinen/palette",
|
"homepage": "https://github.com/ivuorinen/palette",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
@@ -14,12 +15,11 @@
|
|||||||
"source": "https://github.com/ivuorinen/palette",
|
"source": "https://github.com/ivuorinen/palette",
|
||||||
"issues": "https://github.com/ivuorinen/palette/issues"
|
"issues": "https://github.com/ivuorinen/palette/issues"
|
||||||
},
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-0": { "ivuorinen\\Palette\\": "src/" },
|
||||||
|
"classmap": ["src/"]
|
||||||
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.2.0"
|
"php": ">=5.2.0"
|
||||||
},
|
|
||||||
"extra": {
|
|
||||||
"branch-alias": {
|
|
||||||
"dev-master": "master"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
require '../palette.php';
|
// require_once '../src/ivuorinen/Palette/Palette.php'; /* If we are not using Composer autoloader */
|
||||||
|
require_once '../vendor/autoload.php'; /* We are using Composer autoloader (run composer install in project root) */
|
||||||
|
|
||||||
|
|
||||||
$testimages_full_process = array(
|
$testimages_full_process = array(
|
||||||
'example.gif',
|
'example.gif',
|
||||||
@@ -35,12 +37,34 @@ foreach ($testimages_controlled_process as $image) {
|
|||||||
$test->precision = 10; // Bigger is faster, smaller returns more colors
|
$test->precision = 10; // Bigger is faster, smaller returns more colors
|
||||||
$test->returnColors = 5; // How many colors we want in our array at most
|
$test->returnColors = 5; // How many colors we want in our array at most
|
||||||
|
|
||||||
// Get the colors as an array
|
// Configure your destination file name fully we get
|
||||||
$colors = $test->getPalette();
|
// an array encoded to json, so .json extension is good choice
|
||||||
|
$test->destination = dirname(__FILE__) . '/' . md5($test->filename) . '.json';
|
||||||
|
|
||||||
echo "<pre>"
|
echo "<pre>"
|
||||||
. "Processing {$test->filename}\n"
|
. "Processing {$test->filename}\n";
|
||||||
. print_r($colors, true)
|
|
||||||
|
if (is_readable($test->destination)) {
|
||||||
|
|
||||||
|
echo "Returning data from cache: {$test->destination}\n";
|
||||||
|
|
||||||
|
// We have already processed these files, return the array
|
||||||
|
// from cached version to make everything work faster
|
||||||
|
$colors = json_decode(file_get_contents($test->destination));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
echo "Processing data and saving to {$test->destination}\n";
|
||||||
|
|
||||||
|
// We don't have cached versions so process colors as an array
|
||||||
|
$colors = $test->getPalette();
|
||||||
|
|
||||||
|
// And then save the data to file specified in $test->destination
|
||||||
|
$test->save();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
echo print_r($colors, true)
|
||||||
. "</pre>\n";
|
. "</pre>\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Palette
|
* Palette
|
||||||
* Parses image and returns most used colors
|
* Parses image and returns most used colors
|
||||||
*
|
*
|
||||||
* PHP version 5
|
* PHP version 5
|
||||||
*
|
*
|
||||||
* @category Default
|
* MIT License
|
||||||
* @package Palette
|
* ===========
|
||||||
* @author Ismo Vuorinen <ivuorinen@me.com>
|
*
|
||||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
* Copyright (c) 2013 Ismo Vuorinen <ivuorinen@me.com>
|
||||||
* @link https://github.com/ivuorinen/palette
|
*
|
||||||
*/
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @category Default
|
||||||
|
* @package Palette
|
||||||
|
* @author Ismo Vuorinen <ivuorinen@me.com>
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||||
|
* @copyright 2013 Ismo Vuorinen
|
||||||
|
* @link https://github.com/ivuorinen/palette
|
||||||
|
*/
|
||||||
|
|
||||||
namespace ivuorinen\Palette;
|
namespace ivuorinen\Palette;
|
||||||
|
|
||||||
@@ -20,7 +46,7 @@ namespace ivuorinen\Palette;
|
|||||||
* @category Default
|
* @category Default
|
||||||
* @package Palette
|
* @package Palette
|
||||||
* @author Ismo Vuorinen <ivuorinen@me.com>
|
* @author Ismo Vuorinen <ivuorinen@me.com>
|
||||||
* @license http://opensource.org/licenses/gpl-license.php GNU Public 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
|
* @version 1.0.0
|
||||||
* @example example/example.php Usage examples
|
* @example example/example.php Usage examples
|
||||||
@@ -115,25 +141,23 @@ class Palette
|
|||||||
**/
|
**/
|
||||||
public function getPalette()
|
public function getPalette()
|
||||||
{
|
{
|
||||||
// We check for input
|
|
||||||
try {
|
try {
|
||||||
if (empty($this->filename)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
throw new \Exception("Image was not provided");
|
|
||||||
}
|
|
||||||
|
|
||||||
// We check for readability
|
// We check for input
|
||||||
try {
|
if (empty($this->filename)) {
|
||||||
|
throw new \Exception("Image was not provided");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 \Exception("Image {$this->filename} is not readable");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
user_error($e->getMessage(), E_USER_ERROR);
|
user_error($e->getMessage(), E_USER_ERROR);
|
||||||
}
|
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$this->colorsArray = $this->countColors();
|
$this->colorsArray = $this->countColors();
|
||||||
|
|
||||||
@@ -159,6 +183,7 @@ class Palette
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,6 +192,7 @@ class Palette
|
|||||||
|
|
||||||
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);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +213,7 @@ class Palette
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
arsort($colors);
|
arsort($colors);
|
||||||
|
|
||||||
return array_slice($colors, 0, $this->returnColors, true);
|
return array_slice($colors, 0, $this->returnColors, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,25 +228,24 @@ class Palette
|
|||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
// Check for destination
|
||||||
if (empty($this->destination)) {
|
if (empty($this->destination)) {
|
||||||
throw new \Exception("No destination given for save");
|
throw new \Exception("No destination given for save");
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
|
||||||
user_error($e->getMessage(), E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
// Check destination writability
|
||||||
$this->checkDestination();
|
$this->checkDestination();
|
||||||
} catch (\Exception $e) {
|
|
||||||
user_error($e->getMessage(), E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
// 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 \Exception("Couldn't detect colors from image: {$this->filename}");
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
user_error($e->getMessage(), E_USER_ERROR);
|
user_error($e->getMessage(), E_USER_ERROR);
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode for saving
|
// Encode for saving
|
||||||
@@ -258,6 +284,7 @@ class Palette
|
|||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
@@ -290,6 +317,7 @@ class Palette
|
|||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
user_error($e->getMessage());
|
user_error($e->getMessage());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user