1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-01 18:02:45 +02:00

added ImageNotWritableException

This commit is contained in:
Oliver Vogel
2013-09-24 16:55:17 +02:00
parent fcefaf097e
commit 8242dee952
3 changed files with 64 additions and 48 deletions

View File

@@ -0,0 +1,8 @@
<?php
namespace Intervention\Image\Exception;
class ImageNotWritableException extends \RuntimeException
{
# nothing to override
}

View File

@@ -107,7 +107,7 @@ class Image
$this->initFromString($source); $this->initFromString($source);
} else { } else {
// image properties come from image file // image properties come from image file
$this->initFromPath($source); $this->initFromPath($source);
} }
@@ -158,9 +158,9 @@ class Image
* Create new cached image and run callback * Create new cached image and run callback
* (requires additional package intervention/imagecache) * (requires additional package intervention/imagecache)
* *
* @param Closure $callback * @param Closure $callback
* @param integer $lifetime * @param integer $lifetime
* @param boolean $returnObj * @param boolean $returnObj
* *
* @return Image * @return Image
*/ */
@@ -183,7 +183,7 @@ class Image
/** /**
* Set properties for image resource from image file * Set properties for image resource from image file
* *
* @param string $path * @param string $path
* @return void * @return void
*/ */
private function initFromPath($path) private function initFromPath($path)
@@ -204,7 +204,7 @@ class Image
/** /**
* Set properties for image resource from string * Set properties for image resource from string
* *
* @param string $string * @param string $string
* @return void * @return void
*/ */
private function initFromString($string) private function initFromString($string)
@@ -229,9 +229,9 @@ class Image
/** /**
* Set properties for empty image resource * Set properties for empty image resource
* *
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param mixed $bgcolor * @param mixed $bgcolor
* @return void * @return void
*/ */
private function initEmpty($width, $height, $bgcolor = null) private function initEmpty($width, $height, $bgcolor = null)
@@ -336,6 +336,7 @@ class Image
// catch legacy call // catch legacy call
if (is_array($width)) { if (is_array($width)) {
$dimensions = $width; $dimensions = $width;
return $this->legacyResize($dimensions); return $this->legacyResize($dimensions);
} }
@@ -420,13 +421,14 @@ class Image
/** /**
* Legacy method to support old resizing calls * Legacy method to support old resizing calls
* *
* @param array $dimensions * @param array $dimensions
* @return Image * @return Image
*/ */
public function legacyResize($dimensions = array()) public function legacyResize($dimensions = array())
{ {
$width = array_key_exists('width', $dimensions) ? intval($dimensions['width']) : null; $width = array_key_exists('width', $dimensions) ? intval($dimensions['width']) : null;
$height = array_key_exists('height', $dimensions) ? intval($dimensions['height']) : null; $height = array_key_exists('height', $dimensions) ? intval($dimensions['height']) : null;
return $this->resize($width, $height, true); return $this->resize($width, $height, true);
} }
@@ -455,11 +457,11 @@ class Image
/** /**
* Resize image canvas * Resize image canvas
* *
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param string $anchor * @param string $anchor
* @param boolean $relative * @param boolean $relative
* @param mixed $bgcolor * @param mixed $bgcolor
* @return Image * @return Image
*/ */
public function resizeCanvas($width, $height, $anchor = null, $relative = false, $bgcolor = null) public function resizeCanvas($width, $height, $anchor = null, $relative = false, $bgcolor = null)
@@ -596,10 +598,10 @@ class Image
/** /**
* Crop the current image * Crop the current image
* *
* @param integer $width * @param integer $width
* @param integer $height * @param integer $height
* @param integer $pos_x * @param integer $pos_x
* @param integer $pos_y * @param integer $pos_y
* *
* @return Image * @return Image
*/ */
@@ -628,8 +630,8 @@ class Image
/** /**
* Cut out a detail of the image in given ratio and resize to output size * Cut out a detail of the image in given ratio and resize to output size
* *
* @param integer $width * @param integer $width
* @param integer $height * @param integer $height
* *
* @return Image * @return Image
*/ */
@@ -638,6 +640,7 @@ class Image
// catch legacy call // catch legacy call
if (is_array($width)) { if (is_array($width)) {
$dimensions = $width; $dimensions = $width;
return $this->legacyGrab($dimensions); return $this->legacyGrab($dimensions);
} }
@@ -677,7 +680,7 @@ class Image
/** /**
* Legacy Method to support older grab calls * Legacy Method to support older grab calls
* *
* @param array $dimensions * @param array $dimensions
* @return Image * @return Image
*/ */
public function legacyGrab($dimensions = array()) public function legacyGrab($dimensions = array())
@@ -891,9 +894,9 @@ class Image
/** /**
* Rotate image with given angle * Rotate image with given angle
* *
* @param float $angle * @param float $angle
* @param string $color * @param string $color
* @param int $ignore_transparent * @param int $ignore_transparent
* @return Image * @return Image
*/ */
public function rotate($angle = 0, $bgcolor = '#000000', $ignore_transparent = 0) public function rotate($angle = 0, $bgcolor = '#000000', $ignore_transparent = 0)
@@ -1068,7 +1071,7 @@ class Image
/** /**
* Changes the brightness of the current image * Changes the brightness of the current image
* *
* @param int $level [description] * @param int $level [description]
* *
* @return Image * @return Image
*/ */
@@ -1079,7 +1082,7 @@ class Image
'Brightness level must be between -100 and +100' 'Brightness level must be between -100 and +100'
); );
} }
imagefilter($this->resource, IMG_FILTER_BRIGHTNESS, ($level * 2.55)); imagefilter($this->resource, IMG_FILTER_BRIGHTNESS, ($level * 2.55));
return $this; return $this;
@@ -1088,7 +1091,7 @@ class Image
/** /**
* Changes the contrast of the current image * Changes the contrast of the current image
* *
* @param int $level * @param int $level
* *
* @return Image * @return Image
*/ */
@@ -1173,8 +1176,8 @@ class Image
/** /**
* Set a maximum number of colors for the current image * Set a maximum number of colors for the current image
* *
* @param integer $count * @param integer $count
* @param mixed $matte * @param mixed $matte
* @return Image * @return Image
*/ */
public function limitColors($count = null, $matte = null) public function limitColors($count = null, $matte = null)
@@ -1227,8 +1230,8 @@ class Image
/** /**
* Encode image in different formats * Encode image in different formats
* *
* @param string $format * @param string $format
* @param integer $quality * @param integer $quality
* @return string * @return string
*/ */
public function encode($format = null, $quality = 90) public function encode($format = null, $quality = 90)
@@ -1270,14 +1273,15 @@ class Image
$data = ob_get_contents(); $data = ob_get_contents();
ob_end_clean(); ob_end_clean();
return $data; return $data;
} }
/** /**
* Picks and formats color at position * Picks and formats color at position
* *
* @param int $x * @param int $x
* @param int $y * @param int $y
* @param string $format * @param string $format
* @return mixed * @return mixed
*/ */
@@ -1370,7 +1374,7 @@ class Image
// parse color string in format rgba(255, 0, 0, 0.5) // parse color string in format rgba(255, 0, 0, 0.5)
$rgbaPattern = '/^rgba ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\)$/i'; $rgbaPattern = '/^rgba ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\)$/i';
if (preg_match($hexPattern, $value, $matches)) { if (preg_match($hexPattern, $value, $matches)) {
$r = strlen($matches[1]) == '1' ? '0x'.$matches[1].$matches[1] : '0x'.$matches[1]; $r = strlen($matches[1]) == '1' ? '0x'.$matches[1].$matches[1] : '0x'.$matches[1];
$g = strlen($matches[2]) == '1' ? '0x'.$matches[2].$matches[2] : '0x'.$matches[2]; $g = strlen($matches[2]) == '1' ? '0x'.$matches[2].$matches[2] : '0x'.$matches[2];
@@ -1388,11 +1392,9 @@ class Image
} }
if (isset($allocatedColor)) { if (isset($allocatedColor)) {
return $allocatedColor; return $allocatedColor;
} elseif (isset($r) && isset($g) && isset($b)) { } elseif (isset($r) && isset($g) && isset($b)) {
return imagecolorallocatealpha($this->resource, $r, $g, $b, $a); return imagecolorallocatealpha($this->resource, $r, $g, $b, $a);
} else { } else {
@@ -1411,23 +1413,27 @@ class Image
public function save($path = null, $quality = 90) public function save($path = null, $quality = 90)
{ {
$path = is_null($path) ? ($this->dirname .'/'. $this->basename) : $path; $path = is_null($path) ? ($this->dirname .'/'. $this->basename) : $path;
file_put_contents($path, $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality)); $saved = @file_put_contents($path, $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality));
if ($saved === false) {
throw new Exception\ImageNotWritableException;
}
return $this; return $this;
} }
/** /**
* Read Exif data from the current image * Read Exif data from the current image
* *
* Note: Windows PHP Users - in order to use this method you will need to * Note: Windows PHP Users - in order to use this method you will need to
* enable the mbstring and exif extensions within the php.ini file. * enable the mbstring and exif extensions within the php.ini file.
* *
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function exif($key = null) public function exif($key = null)
{ {
if (!function_exists('exif_read_data')) { if (! function_exists('exif_read_data')) {
throw new Exception\ExifFunctionsNotAvailableException; throw new Exception\ExifFunctionsNotAvailableException;
} }
@@ -1463,7 +1469,7 @@ class Image
/** /**
* Convert gd alpha (0-127) value to rgba alpha value (0-1) * Convert gd alpha (0-127) value to rgba alpha value (0-1)
* *
* @param int $input * @param int $input
* @return float * @return float
*/ */
private function alpha2rgba($input) private function alpha2rgba($input)
@@ -1483,7 +1489,7 @@ class Image
/** /**
* Checks if string contains printable characters * Checks if string contains printable characters
* *
* @param mixed $input * @param mixed $input
* @return boolean * @return boolean
*/ */
private function isBinary($input) private function isBinary($input)
@@ -1498,7 +1504,7 @@ class Image
/** /**
* Checks if the input object is image resource * Checks if the input object is image resource
* *
* @param mixed $input * @param mixed $input
* @return boolean * @return boolean
*/ */
private function isImageResource($input) private function isImageResource($input)
@@ -1522,8 +1528,10 @@ class Image
if (get_resource_type($input) != 'gd') { if (get_resource_type($input) != 'gd') {
throw new Exception\InvalidImageResourceException; throw new Exception\InvalidImageResourceException;
} }
return true; return true;
} }
return false; return false;
} }

View File

@@ -6,12 +6,12 @@ class ImageTest extends PHPUnit_Framework_Testcase
{ {
protected function setUp() protected function setUp()
{ {
} }
protected function tearDown() protected function tearDown()
{ {
} }
private function getTestImage() private function getTestImage()
@@ -1235,7 +1235,7 @@ class ImageTest extends PHPUnit_Framework_Testcase
$img = $img->pixel('fdf5e4', $coords[0][0], $coords[0][1]); $img = $img->pixel('fdf5e4', $coords[0][0], $coords[0][1]);
$img = $img->pixel(array(255, 255, 255), $coords[1][0], $coords[1][1]); $img = $img->pixel(array(255, 255, 255), $coords[1][0], $coords[1][1]);
$this->assertEquals('#fdf5e4', $img->pickColor($coords[0][0], $coords[0][1], 'hex')); $this->assertEquals('#fdf5e4', $img->pickColor($coords[0][0], $coords[0][1], 'hex'));
$this->assertEquals('#ffffff', $img->pickColor($coords[1][0], $coords[1][1], 'hex')); $this->assertEquals('#ffffff', $img->pickColor($coords[1][0], $coords[1][1], 'hex'));
} }
public function testTextImage() public function testTextImage()
@@ -1262,8 +1262,8 @@ class ImageTest extends PHPUnit_Framework_Testcase
$img = $this->getTestImage(); $img = $this->getTestImage();
$img = $img->rectangle('cccccc', 10, 10, 100, 100, true); $img = $img->rectangle('cccccc', 10, 10, 100, 100, true);
$this->assertEquals('#ffffff', $img->pickColor(0, 0, 'hex')); $this->assertEquals('#ffffff', $img->pickColor(0, 0, 'hex'));
$this->assertEquals('#cccccc', $img->pickColor(10, 10, 'hex')); $this->assertEquals('#cccccc', $img->pickColor(10, 10, 'hex'));
$this->assertEquals('#cccccc', $img->pickColor(50, 50, 'hex')); $this->assertEquals('#cccccc', $img->pickColor(50, 50, 'hex'));
} }
public function testLineImage() public function testLineImage()
@@ -1374,7 +1374,7 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertEquals($color2['r'], 140); $this->assertEquals($color2['r'], 140);
$this->assertEquals($color2['g'], 140); $this->assertEquals($color2['g'], 140);
$this->assertEquals($color2['b'], 140); $this->assertEquals($color2['b'], 140);
$this->assertEquals($color2['a'], 1); $this->assertEquals($color2['a'], 1);
} }
public function testSaveImage() public function testSaveImage()