diff options
Diffstat (limited to 'app/Controller/File.php')
-rw-r--r-- | app/Controller/File.php | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/app/Controller/File.php b/app/Controller/File.php index 3255fe84..3963e2d7 100644 --- a/app/Controller/File.php +++ b/app/Controller/File.php @@ -102,6 +102,70 @@ class File extends Base } /** + * Return image thumbnails + * + * @access public + */ + public function thumbnail() + { + $task = $this->getTask(); + $file = $this->file->getById($this->request->getIntegerParam('file_id')); + $width_param = $this->request->getIntegerParam('width'); + $height_param = $this->request->getIntegerParam('height'); + $filename = FILES_DIR.$file['path']; + + if ($file['task_id'] == $task['id'] && file_exists($filename)) { + + // Get new sizes + list($width, $height) = getimagesize($filename); + + if ($width_param == 0 && $height_param == 0) { + $newwidth = 100; + $newheight = 100; + } elseif ($width_param > 0 && $height_param == 0) { + $newwidth = $width_param; + $newheight = floor($height * ($width_param / $width)); + } elseif ($width_param == 0 && $height_param > 0) { + $newwidth = floor($width * ($height_param / $height)); + $newheight = $height_param; + } else { + $newwidth = $width_param; + $newheight = $height_param; + } + + // Load + $thumb = imagecreatetruecolor($newwidth, $newheight); + $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); + + switch ($extension) { + case 'jpeg': + case 'jpg': + $source = imagecreatefromjpeg($filename); + break; + case 'png': + $source = imagecreatefrompng($filename); + break; + case 'gif': + $source = imagecreatefromgif($filename); + break; + default: + die('File "' . $filename . '" is not valid jpg, png or gif image.'); + break; + } + + // Resize + imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); + + $metadata = getimagesize($filename); + + if (isset($metadata['mime'])) { + $this->response->contentType($metadata['mime']); + imagejpeg($thumb); + } + } + } + + /** * Remove a file * * @access public |