public function __construct($source, $params = array())
{
$this->source = $this->result = is_a($source, 'Media') ? $source : new Media($source);
$this->options = array_merge(static::$defaults, $this->params($params));
$this->destination = $this->destination();
// don't create the thumbnail if it's not necessary
if ($this->isObsolete()) {
return;
}
// don't create the thumbnail if it exists
if (!$this->isThere()) {
// try to create the thumb folder if it is not there yet
dir::make(dirname($this->destination->root));
// check for a valid image
if (!$this->source->exists() || $this->source->type() != 'image') {
throw new Error('The given image is invalid', static::ERROR_INVALID_IMAGE);
}
// check for a valid driver
if (!array_key_exists($this->options['driver'], static::$drivers)) {
throw new Error('Invalid thumbnail driver', static::ERROR_INVALID_DRIVER);
}
// create the thumbnail
$this->create();
// check if creating the thumbnail failed
if (!file_exists($this->destination->root)) {
return;
}
}
// create the result object
$this->result = new Media($this->destination->root, $this->destination->url);
}