private function _run()
{
$this->EE->load->helper('file');
// Should be using more of CodeIgniter in here
$file_info = get_file_info($this->server_path . $this->image);
// are all paths writeable
if (!file_exists($this->cache_path)) {
if (!mkdir($this->cache_path, 0755)) {
return array(false, 'fatal', 'Cache path is does not exist and cannot be created. Path: ' . $this->cache_path);
}
}
if (!is_readable($this->cache_path)) {
return array(false, 'fatal', 'Cache path is not readable. Path: ' . $this->cache_path);
}
if (!is_writable($this->cache_path)) {
return array(false, 'fatal', 'Cache path is not writable. Path: ' . $this->cache_path);
}
// ----
// is there an image, if ot try to use default
if ($this->image == '' && $this->default_image == '') {
return array(false, 'fatal', 'No image or default image are set.');
} elseif ($this->image == '' && $this->default_image != '') {
$this->image = $this->default_image;
}
// if we get here an image has been set so make sure it starts with a slash.
if ($this->image[0] != '/') {
$this->image = '/' . $this->image;
}
// For security, directories cannot contain ':', images cannot contain '..' or '<', and images must start with '/'
if (strpos(dirname($this->image), ':') || preg_match('/(\\.\\.|<|>)/', $this->image)) {
return array(false, 'fatal', 'Image path is invalid. Image: ' . $this->image);
}
// If the image doesn't exist, or we haven't been told what it is, there's nothing that we can do
if (!file_exists($this->server_path . $this->image) || is_dir($this->server_path . $this->image)) {
return array(false, 'fatal', 'The image does not exist. Server path: ' . $this->server_path . $this->image);
}
// Get the size and MIME type of the requested image
$this->size = GetImageSize($this->server_path . $this->image);
$this->mime = $this->size['mime'];
$mtime = $file_info['date'];
// Make sure that the requested file is actually an image
if (substr($this->mime, 0, 6) != 'image/') {
return array(false, 'fatal', 'The mime type of the image to be resized is not an image. ' . substr($this->mime, 0, 6));
}
$this->width = $this->size[0];
$this->height = $this->size[1];
// set the extension
if (strstr($this->mime, 'jpg') || strstr($this->mime, 'jpeg')) {
$this->ext = '.jpg';
}
if (strstr($this->mime, 'gif')) {
$this->ext = '.gif';
}
if (strstr($this->mime, 'png')) {
$this->ext = '.png';
}
// generate cached filename
$this->resized = $this->cache_path . sha1($this->image . $this->forceWidth . $this->forceHeight . $this->color . $this->maxWidth . $this->maxHeight . $this->cropratio . $this->grayscale) . $this->ext;
$cached_info = get_file_info($this->resized);
$cached_mtime = $cached_info['date'];
// is already cached?
if (file_exists($this->resized) && $cached_mtime > $mtime) {
return $this->_compileImgTag();
}
// calcualte the new dimensions that we are resizing to
// after this call we will have $this->maxWidth and height
// resize to a maximum height
if ($this->maxHeight != '' && $this->maxWidth == '') {
$this->maxWidth = 99999999999999;
} elseif ($this->maxWidth != '' && $this->maxHeight == '') {
$this->maxHeight = 99999999999999;
} elseif ($this->color != '' && $this->maxWidth == '' && $this->maxHeight == '') {
$this->maxWidth = $this->width;
$this->maxHeight = $this->height;
}
// we don't want to force width or height so compile the finished tag and return false
if (!$this->forceWidth && !$this->forceHeight && ($this->maxWidth == '' && $this->maxHeight == '') || $this->color == '' && $this->maxWidth >= $this->width && $this->maxHeight >= $this->height && !$this->forceWidth && !$this->forceHeight) {
$this->lastModifiedString = gmdate('D, d M Y H:i:s', filemtime($this->server_path . $this->image)) . ' GMT';
$this->etag = md5($this->data);
$this->resized = $this->server_path . $this->image;
// we haven't actually resized it
return $this->_compileImgTag();
}
// if we get here we are going to need to try to do some resizing
// so lets find out if we want to crop as well
if ($this->cropratio != '') {
$this->_crop();
}
// if we get here then we have enough to be getting on with so get the resizing underway.
$this->_resize();
return $this->_compileImgTag();
}