From 7aabe2f86143ceb74fcc607278e7a29135ff0e95 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Mon, 1 Jul 2013 16:03:59 +0300 Subject: [PATCH] .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 --- .gitignore | 3 +- composer.json | 10 +-- example/example.php | 34 +++++-- .../ivuorinen/Palette/Palette.php | 90 ++++++++++++------- 4 files changed, 95 insertions(+), 42 deletions(-) rename palette.php => src/ivuorinen/Palette/Palette.php (80%) diff --git a/.gitignore b/.gitignore index f2078d8..4b298b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.json -!composer.json \ No newline at end of file +!composer.json +vendor diff --git a/composer.json b/composer.json index fc7ff32..c468d44 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "name": "ivuorinen/palette", + "description": "Get most used colors from an image", "keywords": ["image", "colors", "palette", "psr-2"], "homepage": "https://github.com/ivuorinen/palette", "type": "library", @@ -14,12 +15,11 @@ "source": "https://github.com/ivuorinen/palette", "issues": "https://github.com/ivuorinen/palette/issues" }, + "autoload": { + "psr-0": { "ivuorinen\\Palette\\": "src/" }, + "classmap": ["src/"] + }, "require": { "php": ">=5.2.0" - }, - "extra": { - "branch-alias": { - "dev-master": "master" - } } } \ No newline at end of file diff --git a/example/example.php b/example/example.php index 6006fad..0148399 100644 --- a/example/example.php +++ b/example/example.php @@ -1,6 +1,8 @@ precision = 10; // Bigger is faster, smaller returns more colors $test->returnColors = 5; // How many colors we want in our array at most - // Get the colors as an array - $colors = $test->getPalette(); + // Configure your destination file name fully we get + // an array encoded to json, so .json extension is good choice + $test->destination = dirname(__FILE__) . '/' . md5($test->filename) . '.json'; echo "
"
-       . "Processing {$test->filename}\n"
-       . print_r($colors, true)
+       . "Processing {$test->filename}\n";
+
+    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)
        . "
\n"; } diff --git a/palette.php b/src/ivuorinen/Palette/Palette.php similarity index 80% rename from palette.php rename to src/ivuorinen/Palette/Palette.php index 6c36a24..acd75ce 100644 --- a/palette.php +++ b/src/ivuorinen/Palette/Palette.php @@ -1,16 +1,42 @@ -* @license http://opensource.org/licenses/gpl-license.php GNU Public License -* @link https://github.com/ivuorinen/palette -*/ + * Palette + * Parses image and returns most used colors + * + * PHP version 5 + * + * MIT License + * =========== + * + * Copyright (c) 2013 Ismo Vuorinen + * + * 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 + * @license http://www.opensource.org/licenses/mit-license.php MIT License + * @copyright 2013 Ismo Vuorinen + * @link https://github.com/ivuorinen/palette + */ namespace ivuorinen\Palette; @@ -20,7 +46,7 @@ namespace ivuorinen\Palette; * @category Default * @package Palette * @author Ismo Vuorinen - * @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 * @version 1.0.0 * @example example/example.php Usage examples @@ -115,25 +141,23 @@ class Palette **/ public function getPalette() { - // We check for input try { - if (empty($this->filename)) { - return false; - } - } catch (\Exception $e) { - throw new \Exception("Image was not provided"); - } - // We check for readability - try { + // We check for input + if (empty($this->filename)) { + throw new \Exception("Image was not provided"); + } + + // We check for readability if (! is_readable($this->filename)) { throw new \Exception("Image {$this->filename} is not readable"); - return false; } + } catch (\Exception $e) { user_error($e->getMessage(), E_USER_ERROR); - } + return false; + } $this->colorsArray = $this->countColors(); @@ -159,6 +183,7 @@ class Palette if (! $img && $img !== null) { user_error("Unable to open: {$this->filename}", E_USER_ERROR); + return false; } @@ -167,6 +192,7 @@ class Palette if ($size === false) { user_error("Unable to get image size data: {$this->filename}", E_USER_ERROR); + return false; } @@ -187,6 +213,7 @@ class Palette } } arsort($colors); + return array_slice($colors, 0, $this->returnColors, true); } @@ -201,25 +228,24 @@ class Palette public function save() { try { + + // Check for destination if (empty($this->destination)) { throw new \Exception("No destination given for save"); } - } catch (\Exception $e) { - user_error($e->getMessage(), E_USER_ERROR); - } - try { + // Check destination writability $this->checkDestination(); - } catch (\Exception $e) { - user_error($e->getMessage(), E_USER_ERROR); - } - try { + // Check for data we should write if (empty($this->colorsArray)) { throw new \Exception("Couldn't detect colors from image: {$this->filename}"); } + } catch (\Exception $e) { user_error($e->getMessage(), E_USER_ERROR); + + return false; } // Encode for saving @@ -258,6 +284,7 @@ class Palette default: $image_type_code = image_type_to_mime_type($type); user_error("Unknown image type: {$image_type_code} ({$type}): {$this->filename}"); + return false; break; } @@ -290,6 +317,7 @@ class Palette } } catch (Exception $e) { user_error($e->getMessage()); + return false; }