diff options
author | Jérémy DECOOL <contact@jdecool.fr> | 2015-03-21 10:35:58 +0100 |
---|---|---|
committer | Jérémy DECOOL <contact@jdecool.fr> | 2015-03-21 11:12:28 +0100 |
commit | d184abdff7d5b494e3f9172ea99d0ed74c4d651c (patch) | |
tree | 99177e50017b46d3fd5ac3d9d7226607a3b0d0aa | |
parent | 540918dc2fe27d6d96a46507fcbd82b888dac1a6 (diff) | |
download | ImageWorkshop-d184abdff7d5b494e3f9172ea99d0ed74c4d651c.zip ImageWorkshop-d184abdff7d5b494e3f9172ea99d0ed74c4d651c.tar.gz ImageWorkshop-d184abdff7d5b494e3f9172ea99d0ed74c4d651c.tar.bz2 |
Update ImageWorkshop::initFromPath to allow load image from URL
-rw-r--r-- | src/PHPImageWorkshop/ImageWorkshop.php | 77 | ||||
-rw-r--r-- | tests/ImageWorkshopTest.php | 11 |
2 files changed, 47 insertions, 41 deletions
diff --git a/src/PHPImageWorkshop/ImageWorkshop.php b/src/PHPImageWorkshop/ImageWorkshop.php index 88330f7..914edd0 100644 --- a/src/PHPImageWorkshop/ImageWorkshop.php +++ b/src/PHPImageWorkshop/ImageWorkshop.php @@ -36,7 +36,7 @@ class ImageWorkshop /**
* @var integer
*/
- const ERROR_NOT_WRITABLE_FILE = 3;
+ const ERROR_NOT_READABLE_FILE = 3;
/**
* @var integer
@@ -55,51 +55,50 @@ class ImageWorkshop */
public static function initFromPath($path, $fixOrientation = false)
{
- if (file_exists($path) && !is_dir($path)) {
-
- if (!is_readable($path)) {
- throw new ImageWorkshopException('Can\'t open the file at "'.$path.'" : file is not writable, did you check permissions (755 / 777) ?', static::ERROR_NOT_WRITABLE_FILE);
- }
-
- $imageSizeInfos = @getImageSize($path);
- $mimeContentType = explode('/', $imageSizeInfos['mime']);
-
- if (!$mimeContentType || !array_key_exists(1, $mimeContentType)) {
- throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE);
- }
-
- $mimeContentType = $mimeContentType[1];
- $exif = array();
-
- switch ($mimeContentType) {
- case 'jpeg':
- $image = imageCreateFromJPEG($path);
- $exif = read_exif_data($path);
- break;
+ if (false === ($imageSizeInfos = @getImageSize($path))) {
+ throw new ImageWorkshopException('Can\'t open the file at "'.$path.'" : file is not readable, did you check permissions (755 / 777) ?', static::ERROR_NOT_READABLE_FILE);
+ }
- case 'gif':
- $image = imageCreateFromGIF($path);
- break;
+ $mimeContentType = explode('/', $imageSizeInfos['mime']);
+ if (!$mimeContentType || !isset($mimeContentType[1])) {
+ throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE);
+ }
- case 'png':
- $image = imageCreateFromPNG($path);
- break;
+ $mimeContentType = $mimeContentType[1];
+ $exif = array();
- default:
- throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE);
- break;
- }
+ switch ($mimeContentType) {
+ case 'jpeg':
+ $image = imageCreateFromJPEG($path);
+ if (false === ($exif = @read_exif_data($path))) {
+ $exif = array();
+ }
+ break;
- $layer = new ImageWorkshopLayer($image, $exif);
+ case 'gif':
+ $image = imageCreateFromGIF($path);
+ break;
- if ($fixOrientation) {
- $layer->fixOrientation();
- }
+ case 'png':
+ $image = imageCreateFromPNG($path);
+ break;
- return $layer;
+ default:
+ throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE);
+ break;
}
-
- throw new ImageWorkshopException('No such file found at "'.$path.'"', static::ERROR_IMAGE_NOT_FOUND);
+
+ if (false === $image) {
+ throw new ImageWorkshopException('Unable to create image with file found at "'.$path.'"');
+ }
+
+ $layer = new ImageWorkshopLayer($image, $exif);
+
+ if ($fixOrientation) {
+ $layer->fixOrientation();
+ }
+
+ return $layer;
}
/**
diff --git a/tests/ImageWorkshopTest.php b/tests/ImageWorkshopTest.php index c4a1c17..dc5fab8 100644 --- a/tests/ImageWorkshopTest.php +++ b/tests/ImageWorkshopTest.php @@ -37,8 +37,15 @@ class ImageWorkshopTest extends \PHPUnit_Framework_TestCase $this->assertTrue(is_object($layer) === true, 'Expect $layer to be an object');
$this->assertTrue(get_class($layer) === 'PHPImageWorkshop\Core\ImageWorkshopLayer', 'Expect $layer to be an ImageWorkshopLayer object');
-
+
// test 2
+
+ $layer = ImageWorkshop::initFromPath('file://'.__DIR__.static::IMAGE_SAMPLE_PATH);
+
+ $this->assertTrue(is_object($layer) === true, 'Expect $layer to be an object');
+ $this->assertTrue(get_class($layer) === 'PHPImageWorkshop\Core\ImageWorkshopLayer', 'Expect $layer to be an ImageWorkshopLayer object');
+
+ // test 3
$this->setExpectedException('PHPImageWorkshop\Exception\ImageWorkshopException');
$layer = ImageWorkshop::initFromPath('fakePath');
@@ -87,4 +94,4 @@ class ImageWorkshopTest extends \PHPUnit_Framework_TestCase $this->assertTrue(is_object($layer) === true, 'Expect $layer to be an object');
$this->assertTrue(get_class($layer) === 'PHPImageWorkshop\Core\ImageWorkshopLayer', 'Expect $layer to be an ImageWorkshopLayer object');
}
-}
\ No newline at end of file +}
|