Skip to content

Commit

Permalink
Merge pull request #66 from jdecool/allow-distant-initfrompath
Browse files Browse the repository at this point in the history
Allow ImageWorkshop::initFromPath work with remote file/URL
  • Loading branch information
jdecool committed Mar 22, 2015
2 parents 540918d + d184abd commit 63c8a45
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
77 changes: 38 additions & 39 deletions src/PHPImageWorkshop/ImageWorkshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ImageWorkshop
/**
* @var integer
*/
const ERROR_NOT_WRITABLE_FILE = 3;
const ERROR_NOT_READABLE_FILE = 3;

/**
* @var integer
Expand All @@ -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;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions tests/ImageWorkshopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
}
}
}

0 comments on commit 63c8a45

Please sign in to comment.