From d184abdff7d5b494e3f9172ea99d0ed74c4d651c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sat, 21 Mar 2015 10:35:58 +0100 Subject: [PATCH] Update ImageWorkshop::initFromPath to allow load image from URL --- src/PHPImageWorkshop/ImageWorkshop.php | 77 +++++++++++++------------- 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 @@ public function testInitFromPath() $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 @@ public function testInitFromString() $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 +}