Skip to content

Commit

Permalink
Added support to open remote URLs
Browse files Browse the repository at this point in the history
For ImageMagick and Imagick (the latter requires ‘allow_url_fopen’ to be enabled).
  • Loading branch information
mcaskill committed Apr 10, 2017
1 parent 635b867 commit 4c9cab1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 12 additions & 5 deletions src/Charcoal/Image/Imagemagick/ImagemagickImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ public function open($source = null)
'Source must be a string (file path)'
);
}

$source = ($source) ? $source : $this->source();
$this->resetTmp();
if (!file_exists($source)) {
throw new Exception(
sprintf('File "%s" does not exist', $source)
);
if (null === parse_url($source, PHP_URL_HOST)) {
throw new Exception(
sprintf('File "%s" does not exist', $source)
);
}
}
$tmp = $this->tmp();
copy($source, $tmp);

copy($source, $this->tmp());

return $this;
}

Expand All @@ -139,13 +143,16 @@ public function save($target = null)
'Target must be a string (file path)'
);
}

$target = ($target) ? $target : $this->target();
if (!is_writable(dirname($target))) {
throw new Exception(
sprintf('Target "%s" is not writable', $target)
);
}

copy($this->tmp(), $target);

return $this;
}

Expand Down
13 changes: 10 additions & 3 deletions src/Charcoal/Image/Imagick/ImagickImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,15 @@ public function open($source = null)
'Source must be a string'
);
}

$source = ($source) ? $source : $this->source();
$this->imagick()->readImage($source);
if (parse_url($source, PHP_URL_HOST)) {
$handle = fopen($source, 'rb');
$this->imagick()->readImageFile($handle);
} else {
$this->imagick()->readImage($source);
}

return $this;
}

Expand All @@ -107,13 +114,13 @@ public function save($target = null)
'Target must be a string (file path)'
);
}
$target = ($target) ? $target : $this->target();

$target = ($target) ? $target : $this->target();
$fileExt = pathinfo($target, PATHINFO_EXTENSION);

$this->imagick()->setImageFormat($fileExt);

$this->imagick()->writeImage($target);

return $this;
}

Expand Down

0 comments on commit 4c9cab1

Please sign in to comment.