Cheetah
AbstractDecoder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Intervention\Image;
4 
5 abstract class AbstractDecoder
6 {
13  abstract public function initFromPath($path);
14 
21  abstract public function initFromBinary($data);
22 
29  abstract public function initFromGdResource($resource);
30 
37  abstract public function initFromImagick(\Imagick $object);
38 
44  private $data;
45 
51  public function __construct($data = null)
52  {
53  $this->data = $data;
54  }
55 
62  public function initFromUrl($url)
63  {
64  if ($data = @file_get_contents($url)) {
65  return $this->initFromBinary($data);
66  }
67 
68  throw new \Intervention\Image\Exception\NotReadableException(
69  "Unable to init from given url (".$url.")."
70  );
71  }
72 
79  public function initFromStream($stream)
80  {
81  $offset = ftell($stream);
82  rewind($stream);
83  $data = @stream_get_contents($stream);
84  fseek($stream, $offset);
85  if ($data) {
86  return $this->initFromBinary($data);
87  }
88 
89  throw new \Intervention\Image\Exception\NotReadableException(
90  "Unable to init from given stream"
91  );
92  }
93 
99  public function isGdResource()
100  {
101  if (is_resource($this->data)) {
102  return (get_resource_type($this->data) == 'gd');
103  }
104 
105  return false;
106  }
107 
113  public function isImagick()
114  {
115  return is_a($this->data, 'Imagick');
116  }
117 
123  public function isInterventionImage()
124  {
125  return is_a($this->data, '\Intervention\Image\Image');
126  }
127 
133  public function isSplFileInfo()
134  {
135  return is_a($this->data, 'SplFileInfo');
136  }
137 
143  public function isSymfonyUpload()
144  {
145  return is_a($this->data, 'Symfony\Component\HttpFoundation\File\UploadedFile');
146  }
147 
153  public function isFilePath()
154  {
155  if (is_string($this->data)) {
156  return is_file($this->data);
157  }
158 
159  return false;
160  }
161 
167  public function isUrl()
168  {
169  return (bool) filter_var($this->data, FILTER_VALIDATE_URL);
170  }
171 
177  public function isStream()
178  {
179  if (!is_resource($this->data)) return false;
180  if (get_resource_type($this->data) !== 'stream') return false;
181 
182  return true;
183  }
184 
190  public function isBinary()
191  {
192  if (is_string($this->data)) {
193  $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $this->data);
194  return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
195  }
196 
197  return false;
198  }
199 
205  public function isDataUrl()
206  {
207  $data = $this->decodeDataUrl($this->data);
208 
209  return is_null($data) ? false : true;
210  }
211 
217  public function isBase64()
218  {
219  return base64_encode(base64_decode($this->data)) === $this->data;
220  }
221 
228  public function initFromInterventionImage($object)
229  {
230  return $object;
231  }
232 
239  private function decodeDataUrl($data_url)
240  {
241  $pattern = "/^data:(?:image\/[a-zA-Z\-\.]+)(?:charset=\".+\")?;base64,(?P<data>.+)$/";
242  preg_match($pattern, $data_url, $matches);
243 
244  if (is_array($matches) && array_key_exists('data', $matches)) {
245  return base64_decode($matches['data']);
246  }
247 
248  return null;
249  }
250 
257  public function init($data)
258  {
259  $this->data = $data;
260 
261  switch (true) {
262 
263  case $this->isGdResource():
264  return $this->initFromGdResource($this->data);
265 
266  case $this->isImagick():
267  return $this->initFromImagick($this->data);
268 
269  case $this->isInterventionImage():
270  return $this->initFromInterventionImage($this->data);
271 
272  case $this->isSplFileInfo():
273  return $this->initFromPath($this->data->getRealPath());
274 
275  case $this->isBinary():
276  return $this->initFromBinary($this->data);
277 
278  case $this->isUrl():
279  return $this->initFromUrl($this->data);
280 
281  case $this->isStream():
282  return $this->initFromStream($this->data);
283 
284  case $this->isFilePath():
285  return $this->initFromPath($this->data);
286 
287  case $this->isDataUrl():
288  return $this->initFromBinary($this->decodeDataUrl($this->data));
289 
290  case $this->isBase64():
291  return $this->initFromBinary(base64_decode($this->data));
292 
293  default:
294  throw new Exception\NotReadableException("Image source not readable");
295  }
296  }
297 
303  public function __toString()
304  {
305  return (string) $this->data;
306  }
307 }
Intervention\Image\AbstractDecoder\isUrl
isUrl()
Definition: AbstractDecoder.php:167
Intervention\Image\AbstractDecoder\__toString
__toString()
Definition: AbstractDecoder.php:303
Intervention\Image\AbstractDecoder\isStream
isStream()
Definition: AbstractDecoder.php:177
Intervention\Image\AbstractDecoder\initFromInterventionImage
initFromInterventionImage($object)
Definition: AbstractDecoder.php:228
Intervention\Image\Exception\NotReadableException
Definition: NotReadableException.php:6
Intervention\Image\AbstractDecoder\isGdResource
isGdResource()
Definition: AbstractDecoder.php:99
php
$url
URI MungeSecretKey $url
Definition: URI.MungeSecretKey.txt:14
Intervention\Image\AbstractDecoder\isImagick
isImagick()
Definition: AbstractDecoder.php:113
Intervention\Image\AbstractDecoder\isDataUrl
isDataUrl()
Definition: AbstractDecoder.php:205
Intervention\Image\AbstractDecoder\initFromUrl
initFromUrl($url)
Definition: AbstractDecoder.php:62
Intervention\Image\AbstractDecoder
Definition: AbstractDecoder.php:6
Intervention\Image\AbstractDecoder\isFilePath
isFilePath()
Definition: AbstractDecoder.php:153
$path
$path
Definition: header.inc.php:12
Intervention\Image\AbstractDecoder\initFromBinary
initFromBinary($data)
Intervention\Image\AbstractDecoder\isBase64
isBase64()
Definition: AbstractDecoder.php:217
Intervention\Image\AbstractDecoder\isInterventionImage
isInterventionImage()
Definition: AbstractDecoder.php:123
Intervention\Image\AbstractDecoder\isBinary
isBinary()
Definition: AbstractDecoder.php:190
Intervention\Image\AbstractDecoder\init
init($data)
Definition: AbstractDecoder.php:257
Intervention\Image\AbstractDecoder\initFromPath
initFromPath($path)
Intervention\Image\AbstractDecoder\initFromImagick
initFromImagick(\Imagick $object)
Intervention\Image\AbstractDecoder\isSymfonyUpload
isSymfonyUpload()
Definition: AbstractDecoder.php:143
Intervention\Image\AbstractDecoder\initFromStream
initFromStream($stream)
Definition: AbstractDecoder.php:79
Intervention\Image\AbstractDecoder\__construct
__construct($data=null)
Definition: AbstractDecoder.php:51
Intervention\Image\AbstractDecoder\initFromGdResource
initFromGdResource($resource)
false
if(!defined("FALSE_VAL")) define("FALSE_VAL" false
Definition: constants.inc.php:9
Intervention\Image\AbstractDecoder\isSplFileInfo
isSplFileInfo()
Definition: AbstractDecoder.php:133
Intervention\Image
Definition: AbstractColor.php:3