From a152cf01abc87702e59329a935bed785e9603be3 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Thu, 27 Jun 2013 15:52:11 +0300 Subject: [PATCH] Version bump to 1.0.0 - Palette now in 1.0.0 and includes a docblock @example, also made the checking better and wrote some documentation - Example now includes a failing test to make sure what happens when given octet stream --- example/example.php | 3 ++ palette.php | 79 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/example/example.php b/example/example.php index 7acd65d..6006fad 100644 --- a/example/example.php +++ b/example/example.php @@ -44,3 +44,6 @@ foreach ($testimages_controlled_process as $image) { . "\n"; } + +echo "

This one fails

\n"; +$test = new \ivuorinen\Palette\Palette('/bin/sh'); diff --git a/palette.php b/palette.php index f318fb7..6c36a24 100644 --- a/palette.php +++ b/palette.php @@ -22,7 +22,8 @@ namespace ivuorinen\Palette; * @author Ismo Vuorinen * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @link https://github.com/ivuorinen/palette - * @since 0.1.0 + * @version 1.0.0 + * @example example/example.php Usage examples **/ class Palette { @@ -60,10 +61,11 @@ class Palette /** * Constructor * + * If you have specified $filename, Palette::run() gets triggered. + * Palette::run() uses default parameters and processes given image + * and saves the result as an json-file to datafiles -folder. + * * @param string $filename Full path to image - * - * @return null - * **/ public function __construct($filename = null) { @@ -144,19 +146,15 @@ class Palette /** * countColors returns an array of colors in the image + * * @return array Array of colors sorted by times used */ private function countColors() { $this->precision = max(1, abs((int) $this->precision)); $colors = array(); - $size = @getimagesize($this->filename); - - if ($size === false) { - user_error("Unable to get image size data", E_USER_ERROR); - return false; - } + // Test for image type $img = $this->imageTypeToResource(); if (! $img && $img !== null) { @@ -164,6 +162,14 @@ class Palette return false; } + // Get image size and check if it's really an image + $size = @getimagesize($this->filename); + + if ($size === false) { + user_error("Unable to get image size data: {$this->filename}", E_USER_ERROR); + return false; + } + for ($x = 0; $x < $size[0]; $x += $this->precision) { for ($y = 0; $y < $size[1]; $y += $this->precision) { $thisColor = imagecolorat($img, $x, $y); @@ -203,17 +209,14 @@ class Palette } try { - $destination_dir = dirname($this->destination); - if (! is_writable($destination_dir)) { - throw new \Exception("Destination directory not writable: {$destination_dir}"); - } + $this->checkDestination(); } catch (\Exception $e) { user_error($e->getMessage(), E_USER_ERROR); } try { if (empty($this->colorsArray)) { - throw new \Exception("Couldn't detect colors from image"); + throw new \Exception("Couldn't detect colors from image: {$this->filename}"); } } catch (\Exception $e) { user_error($e->getMessage(), E_USER_ERROR); @@ -261,4 +264,50 @@ class Palette return $img; } + + /** + * checkDestination tries to make sure you have directory to save to + * + * Tests done: + * - Does the destination folder exists? + * - Is it writable? + * - Can it be made writable? + * + * @return bool|exception True or false, with exceptions + */ + private function checkDestination() + { + $destination_dir = dirname($this->destination); + + // Test if we have destination directory + try { + if (! file_exists($destination_dir)) { + mkdir($destination_dir, 0755); + } + + if (! file_exists($destination_dir)) { + throw new \Exception("Couldn't create missing destination dir: {$destination_dir}"); + } + } catch (Exception $e) { + user_error($e->getMessage()); + return false; + } + + // Test if we can write to it + try { + if (! is_writable($destination_dir)) { + chmod($destination_dir, 0755); + } else { + return true; + } + + if (! is_writable($destination_dir)) { + throw new \Exception("Destination directory not writable: {$destination_dir}"); + } + } catch (\Exception $e) { + user_error($e->getMessage()); + } + + return true; + } }