mirror of
https://github.com/ivuorinen/palette.git
synced 2026-01-26 11:34:09 +00:00
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
This commit is contained in:
@@ -44,3 +44,6 @@ foreach ($testimages_controlled_process as $image) {
|
||||
. "</pre>\n";
|
||||
|
||||
}
|
||||
|
||||
echo "<h1>This one fails</h1>\n";
|
||||
$test = new \ivuorinen\Palette\Palette('/bin/sh');
|
||||
|
||||
79
palette.php
79
palette.php
@@ -22,7 +22,8 @@ namespace ivuorinen\Palette;
|
||||
* @author Ismo Vuorinen <ivuorinen@me.com>
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user