Cheetah
Image.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Intervention\Image;
4 
49 class Image extends File
50 {
56  protected $driver;
57 
63  protected $core;
64 
70  protected $backups = array();
71 
77  public $encoded = '';
78 
85  public function __construct(AbstractDriver $driver = null, $core = null)
86  {
87  $this->driver = $driver;
88  $this->core = $core;
89  }
90 
99  public function __call($name, $arguments)
100  {
101  $command = $this->driver->executeCommand($this, $name, $arguments);
102  return $command->hasOutput() ? $command->getOutput() : $this;
103  }
104 
112  public function encode($format = null, $quality = 90)
113  {
114  return $this->driver->encode($this, $format, $quality);
115  }
116 
124  public function save($path = null, $quality = null)
125  {
126  $path = is_null($path) ? $this->basePath() : $path;
127 
128  if (is_null($path)) {
130  "Can't write to undefined path."
131  );
132  }
133 
134  $data = $this->encode(pathinfo($path, PATHINFO_EXTENSION), $quality);
135  $saved = @file_put_contents($path, $data);
136 
137  if ($saved === false) {
139  "Can't write image data to path ({$path})"
140  );
141  }
142 
143  // set new file info
144  $this->setFileInfoFromPath($path);
145 
146  return $this;
147  }
148 
155  public function filter(Filters\FilterInterface $filter)
156  {
157  return $filter->applyFilter($this);
158  }
159 
165  public function getDriver()
166  {
167  return $this->driver;
168  }
169 
175  {
176  $this->driver = $driver;
177 
178  return $this;
179  }
180 
186  public function getCore()
187  {
188  return $this->core;
189  }
190 
196  public function setCore($core)
197  {
198  $this->core = $core;
199 
200  return $this;
201  }
202 
209  public function getBackup($name = null)
210  {
211  $name = is_null($name) ? 'default' : $name;
212 
213  if ( ! $this->backupExists($name)) {
214  throw new \Intervention\Image\Exception\RuntimeException(
215  "Backup with name ({$name}) not available. Call backup() before reset()."
216  );
217  }
218 
219  return $this->backups[$name];
220  }
221 
227  public function getBackups()
228  {
229  return $this->backups;
230  }
231 
239  public function setBackup($resource, $name = null)
240  {
241  $name = is_null($name) ? 'default' : $name;
242 
243  $this->backups[$name] = $resource;
244 
245  return $this;
246  }
247 
254  private function backupExists($name)
255  {
256  return array_key_exists($name, $this->backups);
257  }
258 
264  public function isEncoded()
265  {
266  return ! is_null($this->encoded);
267  }
268 
274  public function getEncoded()
275  {
276  return $this->encoded;
277  }
278 
284  public function setEncoded($value)
285  {
286  $this->encoded = $value;
287 
288  return $this;
289  }
290 
296  public function getWidth()
297  {
298  return $this->getSize()->width;
299  }
300 
306  public function width()
307  {
308  return $this->getWidth();
309  }
310 
316  public function getHeight()
317  {
318  return $this->getSize()->height;
319  }
320 
326  public function height()
327  {
328  return $this->getHeight();
329  }
330 
336  public function mime()
337  {
338  return $this->mime;
339  }
340 
346  public function basePath()
347  {
348  if ($this->dirname && $this->basename) {
349  return ($this->dirname .'/'. $this->basename);
350  }
351 
352  return null;
353  }
354 
360  public function __toString()
361  {
362  return $this->encoded;
363  }
364 
368  public function __clone()
369  {
370  $this->core = $this->driver->cloneCore($this->core);
371  }
372 }
Intervention\Image\Image\__call
__call($name, $arguments)
Definition: Image.php:99
Intervention\Image\Image\$encoded
$encoded
Definition: Image.php:77
Intervention\Image\Image\setEncoded
setEncoded($value)
Definition: Image.php:284
Intervention\Image\Image\getBackup
getBackup($name=null)
Definition: Image.php:209
Intervention\Image\Image\__construct
__construct(AbstractDriver $driver=null, $core=null)
Definition: Image.php:85
Intervention\Image\File\setFileInfoFromPath
setFileInfoFromPath($path)
Definition: File.php:47
php
Intervention\Image\Image\encode
encode($format=null, $quality=90)
Definition: Image.php:112
Intervention\Image\Image\getBackups
getBackups()
Definition: Image.php:227
Intervention\Image\Image\$core
$core
Definition: Image.php:63
Intervention\Image\Image\getCore
getCore()
Definition: Image.php:186
Intervention\Image\Image\getHeight
getHeight()
Definition: Image.php:316
Intervention\Image\Image\height
height()
Definition: Image.php:326
Intervention\Image\Image\$backups
$backups
Definition: Image.php:70
Intervention\Image\Image\setDriver
setDriver(AbstractDriver $driver)
Definition: Image.php:174
Intervention\Image\Image
Definition: Image.php:50
Intervention\Image\Exception\NotWritableException
Definition: NotWritableException.php:6
$path
$path
Definition: header.inc.php:12
Intervention\Image\Image\getEncoded
getEncoded()
Definition: Image.php:274
Intervention\Image\File\$mime
$mime
Definition: File.php:12
Intervention\Image\Image\setBackup
setBackup($resource, $name=null)
Definition: Image.php:239
Intervention\Image\Image\isEncoded
isEncoded()
Definition: Image.php:264
Intervention\Image\Image\basePath
basePath()
Definition: Image.php:346
Intervention\Image\Image\width
width()
Definition: Image.php:306
Intervention\Image\Image\__clone
__clone()
Definition: Image.php:368
Intervention\Image\Image\__toString
__toString()
Definition: Image.php:360
Intervention\Image\Image\save
save($path=null, $quality=null)
Definition: Image.php:124
Intervention\Image\Image\setCore
setCore($core)
Definition: Image.php:196
Intervention\Image\Image\$driver
$driver
Definition: Image.php:56
Intervention\Image\AbstractDriver
Definition: AbstractDriver.php:6
Intervention\Image\Image\getWidth
getWidth()
Definition: Image.php:296
File
Intervention\Image\Image\filter
filter(Filters\FilterInterface $filter)
Definition: Image.php:155
Intervention\Image\Image\getDriver
getDriver()
Definition: Image.php:165
Intervention\Image\Image\mime
mime()
Definition: Image.php:336
Intervention\Image
Definition: AbstractColor.php:3