Skip to content

Commit 4c9cab1

Browse files
committed
Added support to open remote URLs
For ImageMagick and Imagick (the latter requires ‘allow_url_fopen’ to be enabled).
1 parent 635b867 commit 4c9cab1

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/Charcoal/Image/Imagemagick/ImagemagickImage.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,19 @@ public function open($source = null)
111111
'Source must be a string (file path)'
112112
);
113113
}
114+
114115
$source = ($source) ? $source : $this->source();
115116
$this->resetTmp();
116117
if (!file_exists($source)) {
117-
throw new Exception(
118-
sprintf('File "%s" does not exist', $source)
119-
);
118+
if (null === parse_url($source, PHP_URL_HOST)) {
119+
throw new Exception(
120+
sprintf('File "%s" does not exist', $source)
121+
);
122+
}
120123
}
121-
$tmp = $this->tmp();
122-
copy($source, $tmp);
124+
125+
copy($source, $this->tmp());
126+
123127
return $this;
124128
}
125129

@@ -139,13 +143,16 @@ public function save($target = null)
139143
'Target must be a string (file path)'
140144
);
141145
}
146+
142147
$target = ($target) ? $target : $this->target();
143148
if (!is_writable(dirname($target))) {
144149
throw new Exception(
145150
sprintf('Target "%s" is not writable', $target)
146151
);
147152
}
153+
148154
copy($this->tmp(), $target);
155+
149156
return $this;
150157
}
151158

src/Charcoal/Image/Imagick/ImagickImage.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ public function open($source = null)
8787
'Source must be a string'
8888
);
8989
}
90+
9091
$source = ($source) ? $source : $this->source();
91-
$this->imagick()->readImage($source);
92+
if (parse_url($source, PHP_URL_HOST)) {
93+
$handle = fopen($source, 'rb');
94+
$this->imagick()->readImageFile($handle);
95+
} else {
96+
$this->imagick()->readImage($source);
97+
}
98+
9299
return $this;
93100
}
94101

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

118+
$target = ($target) ? $target : $this->target();
112119
$fileExt = pathinfo($target, PATHINFO_EXTENSION);
113120

114121
$this->imagick()->setImageFormat($fileExt);
115-
116122
$this->imagick()->writeImage($target);
123+
117124
return $this;
118125
}
119126

0 commit comments

Comments
 (0)