From e9e9e635b337dc46111cdf95ccb16b7d28deb849 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 2 Mar 2015 12:14:58 +0100 Subject: Add imagemagick support Adds additional support for imagemagick if GD doesn't support a file type and extends the files displayed as thumbnails to all images and pdf files. Signed-off-by: Florian Pritz --- application/controllers/file.php | 50 ++-- application/helpers/filebin_helper.php | 8 + application/libraries/Exif.php | 37 +++ application/libraries/Image.php | 274 ++++++++------------- application/libraries/Image/Drivers/GD.php | 226 +++++++++++++++++ .../libraries/Image/Drivers/imagemagick.php | 121 +++++++++ application/models/mfile.php | 10 +- application/views/file/fragments/thumbnail.php | 2 +- .../views/file/upload_history_thumbnails.php | 2 +- 9 files changed, 533 insertions(+), 197 deletions(-) create mode 100644 application/libraries/Exif.php create mode 100644 application/libraries/Image/Drivers/GD.php create mode 100644 application/libraries/Image/Drivers/imagemagick.php (limited to 'application') diff --git a/application/controllers/file.php b/application/controllers/file.php index 3336cba37..e258f2ffb 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -159,27 +159,23 @@ class File extends MY_Controller { $this->ddownload->serveFile($file, $filedata["filename"], $filedata["mimetype"]); exit(); } else { - switch ($filedata["mimetype"]) { - // TODO: handle video/audio - // TODO: handle more image formats (thumbnails needs to be improved) - case "image/jpeg": - case "image/png": - case "image/gif": + $mimetype = $filedata["mimetype"]; + $base = explode("/", $filedata["mimetype"])[0]; + + // TODO: handle video/audio + if ($base == "image" + || in_array($mimetype, array("application/pdf"))) { $filedata["tooltip"] = $this->_tooltip_for_image($filedata); $filedata["orientation"] = libraries\Image::get_exif_orientation($file); $this->output_cache->add_merge( array("items" => array($filedata)), 'file/fragments/thumbnail' ); - - break; - - default: + } else { $this->output_cache->add_merge( array("items" => array($filedata)), 'file/fragments/uploads_table' ); - break; } continue; } @@ -316,12 +312,27 @@ class File extends MY_Controller { private function _tooltip_for_image($filedata) { $filesize = format_bytes($filedata["filesize"]); - list($width, $height) = getimagesize($this->mfile->file($filedata["hash"])); + $file = $this->mfile->file($filedata["hash"]); $upload_date = date("r", $filedata["date"]); + $height = 0; + $width = 0; + try { + list($width, $height) = getimagesize($file); + } catch (\ErrorException $e) { + // likely unsupported filetype + // TODO: support more (using identify from imagemagick is likely slow :( ) + } + $tooltip = "${filedata["id"]} - $filesize
"; $tooltip .= "$upload_date
"; - $tooltip .= "${width}x${height} - ${filedata["mimetype"]}
"; + + + if ($height > 0 && $width > 0) { + $tooltip .= "${width}x${height} - ${filedata["mimetype"]}
"; + } else { + $tooltip .= "${filedata["mimetype"]}
"; + } return $tooltip; } @@ -585,14 +596,19 @@ class File extends MY_Controller { $user = $this->muser->get_userid(); $query = $this->db - ->select('id, filename, mimetype, date, hash, filesize') + ->select('id, filename, mimetype, date, hash, filesize, user') ->from('files') - ->where('user', $user) - ->where_in('mimetype', array('image/jpeg', 'image/png', 'image/gif')) + ->where(' + (user = '.$this->db->escape($user).') + AND ( + mimetype LIKE "image%" + OR mimetype IN ("application/pdf") + )', null, false) ->order_by('date', 'desc') ->get()->result_array(); foreach($query as $key => $item) { + assert($item["user"] === $user); if (!$this->mfile->valid_id($item["id"])) { unset($query[$key]); continue; @@ -1069,7 +1085,7 @@ class File extends MY_Controller { foreach ($query as $key => $item) { $hash = $item["hash"]; $filesize = intval(filesize($this->mfile->file($hash))); - $mimetype = $this->mfile->mimetype($this->mfile->file($hash)); + $mimetype = mimetype($this->mfile->file($hash)); $this->db->where('hash', $hash) ->set(array( diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php index a1b540b1d..2604cfe4e 100644 --- a/application/helpers/filebin_helper.php +++ b/application/helpers/filebin_helper.php @@ -300,4 +300,12 @@ function cache_function($key, $ttl, $function) return $content; } +// Return mimetype of file +function mimetype($file) { + $fileinfo = new finfo(FILEINFO_MIME_TYPE); + $mimetype = $fileinfo->file($file); + + return $mimetype; +} + # vim: set noet: diff --git a/application/libraries/Exif.php b/application/libraries/Exif.php new file mode 100644 index 000000000..27dd11a65 --- /dev/null +++ b/application/libraries/Exif.php @@ -0,0 +1,37 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace libraries; + +class Exif { + static public function get_exif($file) + { + // TODO: support more types (identify or exiftool? might be slow :( ) + try { + $type = getimagesize($file)[2]; + } catch (\ErrorException $e) { + return false; + } + switch ($type) { + case IMAGETYPE_JPEG: + getimagesize($file, $info); + if (isset($info["APP1"]) && strpos($info["APP1"], "http://ns.adobe.com/xap/1.0/") === 0) { + // ignore XMP data which makes exif_read_data throw a warning + // http://stackoverflow.com/a/8864064 + return false; + } + return @exif_read_data($file); + break; + default: + } + + return false; + } + +} diff --git a/application/libraries/Image.php b/application/libraries/Image.php index 0f920358a..9c624eec1 100644 --- a/application/libraries/Image.php +++ b/application/libraries/Image.php @@ -1,12 +1,76 @@ + * Copyright 2014-2015 Florian "Bluewind" Pritz * * Licensed under AGPLv3 * (see COPYING for full license text) * */ +namespace libraries\Image; + +interface ImageDriver { + /** + * Replace the current image by reading in a file + * @param file file to read + */ + public function read($file); + + /** + * Return the current image rendered to a specific format. Passing null as + * the target_type returns the image in the format of the source image + * (loaded with read()). + * + * @param target_type one of IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG or null + * @return binary image data + */ + public function get($target_type); + + /** + * Resize the image. + * @param width + * @param height + */ + public function resize($width, $height); + + /** + * Crop the image to the area defined by x, y, width and height. + * + * @param x starting coordinate + * @param y starting coordinate + * @param width width of the area + * @param height height of the area + */ + public function crop($x, $y, $width, $height); + + /** + * Crop/resize the image to fit into the desired size. This also rotates + * the image if the source image had an EXIF orientation tag. + * + * @param width width of the resulting image + * @param height height of the resulting image + */ + public function makeThumb($target_width, $target_height); + + /** + * Rotate the image according to the sources EXIF orientation tag if any. + */ + public function apply_exif_orientation(); + + /** + * Mirror the image along the x axis. + */ + public function mirror(); + + /** + * Return priority for this driver. Higher number means a higher priority. + * + * @param mimetype mimetype of the file + * @return > 0 if supported, < 0 if the mimetype can't be handled + */ + public static function get_priority($mimetype); +} + namespace libraries; /** @@ -14,9 +78,12 @@ namespace libraries; * operate on this image. */ class Image { - private $img; - private $source_type; - private $exif; + private $driver; + + private $image_drivers = array( + "libraries\Image\Drivers\GD", + "libraries\Image\Drivers\imagemagick", + ); /** * Create a new object and load the contents of file. @@ -28,39 +95,42 @@ class Image { } /** - * Replace the current image by reading in a file - * @param file file to read + * Get the best driver supporting $mimetype. + * + * @param drivers list of driver classes + * @param mimetype mimetype the driver should support + * @return driver from $drivers or NULL if no driver supports the type */ - public function read($file) + private function best_driver($drivers, $mimetype) { - $img = imagecreatefromstring(file_get_contents($file)); - if ($img === false) { - throw new \exceptions\ApiException("libraries/Image/unsupported-image-type", "Unsupported image type"); + $best = 0; + $best_driver = null; + foreach ($drivers as $driver) { + $current = $driver::get_priority($mimetype); + if ($best == 0 || ($current > $best && $current > 0)) { + $best_driver = $driver; + $best = $current; + } } - $this->set_img_object($img); - $this->fix_alpha(); - $this->source_type = getimagesize($file)[2]; - $this->exif = self::get_exif($file); + return $best_driver; } - static private function get_exif($file) + /** + * Replace the current image by reading in a file + * @param file file to read + */ + public function read($file) { - $type = getimagesize($file)[2]; - switch ($type) { - case IMAGETYPE_JPEG: - getimagesize($file, $info); - if (isset($info["APP1"]) && strpos($info["APP1"], "http://ns.adobe.com/xap/1.0/") === 0) { - // ignore XMP data which makes exif_read_data throw a warning - // http://stackoverflow.com/a/8864064 - return false; - } - return @exif_read_data($file); - break; - default: + $mimetype = mimetype($file); + + $driver = $this->best_driver($this->image_drivers, $mimetype); + + if ($driver === NULL) { + throw new \exceptions\ApiException("libraries/Image/unsupported-image-type", "Unsupported image type"); } - return false; + $this->driver = new $driver($file); } /** @@ -73,31 +143,7 @@ class Image { */ public function get($target_type = null) { - if ($target_type === null) { - $target_type = $this->source_type; - } - - ob_start(); - switch ($target_type) { - case IMAGETYPE_GIF: - $ret = imagegif($this->img); - break; - case IMAGETYPE_JPEG: - $ret = imagejpeg($this->img); - break; - case IMAGETYPE_PNG: - $ret = imagepng($this->img); - break; - default: - assert(0); - } - $result = ob_get_clean(); - - if (!$ret || $result === false) { - throw new \exceptions\ApiException("libraries/Image/thumbnail-creation-failed", "Failed to create thumbnail"); - } - - return $result; + return $this->driver->get($target_type); } /** @@ -107,18 +153,7 @@ class Image { */ public function resize($width, $height) { - $temp_gdim = imagecreatetruecolor($width, $height); - $this->fix_alpha(); - imagecopyresampled( - $temp_gdim, - $this->img, - 0, 0, - 0, 0, - $width, $height, - imagesx($this->img), imagesy($this->img) - ); - - $this->set_img_object($temp_gdim); + return $this->driver->resize($width, $height); } /** @@ -131,17 +166,7 @@ class Image { */ public function crop($x, $y, $width, $height) { - $thumb = imagecreatetruecolor($width, $height); - $this->fix_alpha(); - imagecopy( - $thumb, - $this->img, - 0, 0, - $x, $y, - $width, $height - ); - - $this->set_img_object($thumb); + return $this->driver->crop($x, $y, $width, $height); } /** @@ -151,34 +176,14 @@ class Image { * @param width width of the resulting image * @param height height of the resulting image */ - // Source: http://salman-w.blogspot.co.at/2009/04/crop-to-fit-image-using-aspphp.html public function makeThumb($target_width, $target_height) { - $source_aspect_ratio = imagesx($this->img) / imagesy($this->img); - $desired_aspect_ratio = $target_width / $target_height; - - if ($source_aspect_ratio > $desired_aspect_ratio) { - // Triggered when source image is wider - $temp_height = $target_height; - $temp_width = round(($target_height * $source_aspect_ratio)); - } else { - // Triggered otherwise (i.e. source image is similar or taller) - $temp_width = $target_width; - $temp_height = round(($target_width / $source_aspect_ratio)); - } - - $this->resize($temp_width, $temp_height); - - $x0 = ($temp_width - $target_width) / 2; - $y0 = ($temp_height - $target_height) / 2; - $this->crop($x0, $y0, $target_width, $target_height); - - $this->apply_exif_orientation(); + return $this->driver->makeThumb($target_width, $target_height); } static public function get_exif_orientation($file) { - $exif = self::get_exif($file); + $exif = \libraries\Exif::get_exif($file); if (isset($exif["Orientation"])) { return $exif["Orientation"]; } @@ -190,45 +195,7 @@ class Image { */ public function apply_exif_orientation() { - if (isset($this->exif['Orientation'])) { - $mirror = false; - $deg = 0; - - switch ($this->exif['Orientation']) { - case 2: - $mirror = true; - break; - case 3: - $deg = 180; - break; - case 4: - $deg = 180; - $mirror = true; - break; - case 5: - $deg = 270; - $mirror = true; - break; - case 6: - $deg = 270; - break; - case 7: - $deg = 90; - $mirror = true; - break; - case 8: - $deg = 90; - break; - } - - if ($deg) { - $this->set_img_object(imagerotate($this->img, $deg, 0)); - } - - if ($mirror) { - $this->mirror(); - } - } + return $this->driver->apply_exif_orientation(); } /** @@ -236,38 +203,7 @@ class Image { */ public function mirror() { - $width = imagesx($this->img); - $height = imagesy($this->img); - - $src_x = $width -1; - $src_y = 0; - $src_width = -$width; - $src_height = $height; - - $imgdest = imagecreatetruecolor($width, $height); - imagealphablending($imgdest,false); - imagesavealpha($imgdest,true); - - imagecopyresampled($imgdest, $this->img, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height); - $this->set_img_object($imgdest); - } - - private function set_img_object($new) - { - assert($new !== false); - - $old = $this->img; - $this->img = $new; - - if ($old != null) { - imagedestroy($old); - } - } - - private function fix_alpha() - { - imagealphablending($this->img,false); - imagesavealpha($this->img,true); + return $this->driver->mirror(); } } diff --git a/application/libraries/Image/Drivers/GD.php b/application/libraries/Image/Drivers/GD.php new file mode 100644 index 000000000..5c6411ced --- /dev/null +++ b/application/libraries/Image/Drivers/GD.php @@ -0,0 +1,226 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace libraries\Image\Drivers; + +class GD implements \libraries\Image\ImageDriver { + private $img; + private $source_type; + private $exif; + + public static function get_priority($mimetype) + { + switch($mimetype) { + case "image/jpeg": + case "image/png": + case "image/gif": + return 1000; + break; + default: + return -1; + break; + } + } + + /** + * Create a new object and load the contents of file. + * @param file file to read + */ + public function __construct($file) + { + $this->read($file); + } + + public function read($file) + { + $img = imagecreatefromstring(file_get_contents($file)); + if ($img === false) { + throw new \exceptions\ApiException("libraries/Image/unsupported-image-type", "Unsupported image type"); + } + $this->set_img_object($img); + $this->fix_alpha(); + + $this->source_type = getimagesize($file)[2]; + $this->exif = \libraries\Exif::get_exif($file); + } + + public function get($target_type = null) + { + if ($target_type === null) { + $target_type = $this->source_type; + } + + ob_start(); + switch ($target_type) { + case IMAGETYPE_GIF: + $ret = imagegif($this->img); + break; + case IMAGETYPE_JPEG: + $ret = imagejpeg($this->img); + break; + case IMAGETYPE_PNG: + $ret = imagepng($this->img); + break; + default: + assert(0); + } + $result = ob_get_clean(); + + if (!$ret || $result === false) { + throw new \exceptions\ApiException("libraries/Image/thumbnail-creation-failed", "Failed to create thumbnail"); + } + + return $result; + } + + public function resize($width, $height) + { + $temp_gdim = imagecreatetruecolor($width, $height); + $this->fix_alpha(); + imagecopyresampled( + $temp_gdim, + $this->img, + 0, 0, + 0, 0, + $width, $height, + imagesx($this->img), imagesy($this->img) + ); + + $this->set_img_object($temp_gdim); + } + + public function crop($x, $y, $width, $height) + { + $thumb = imagecreatetruecolor($width, $height); + $this->fix_alpha(); + imagecopy( + $thumb, + $this->img, + 0, 0, + $x, $y, + $width, $height + ); + + $this->set_img_object($thumb); + } + + // Source: http://salman-w.blogspot.co.at/2009/04/crop-to-fit-image-using-aspphp.html + public function makeThumb($target_width, $target_height) + { + $source_aspect_ratio = imagesx($this->img) / imagesy($this->img); + $desired_aspect_ratio = $target_width / $target_height; + + if ($source_aspect_ratio > $desired_aspect_ratio) { + // Triggered when source image is wider + $temp_height = $target_height; + $temp_width = round(($target_height * $source_aspect_ratio)); + } else { + // Triggered otherwise (i.e. source image is similar or taller) + $temp_width = $target_width; + $temp_height = round(($target_width / $source_aspect_ratio)); + } + + $this->resize($temp_width, $temp_height); + + $x0 = ($temp_width - $target_width) / 2; + $y0 = ($temp_height - $target_height) / 2; + $this->crop($x0, $y0, $target_width, $target_height); + + $this->apply_exif_orientation(); + } + + static public function get_exif_orientation($file) + { + $exif = \libraries\Exif::get_exif($file); + if (isset($exif["Orientation"])) { + return $exif["Orientation"]; + } + return 0; + } + + public function apply_exif_orientation() + { + if (isset($this->exif['Orientation'])) { + $mirror = false; + $deg = 0; + + switch ($this->exif['Orientation']) { + case 2: + $mirror = true; + break; + case 3: + $deg = 180; + break; + case 4: + $deg = 180; + $mirror = true; + break; + case 5: + $deg = 270; + $mirror = true; + break; + case 6: + $deg = 270; + break; + case 7: + $deg = 90; + $mirror = true; + break; + case 8: + $deg = 90; + break; + } + + if ($deg) { + $this->set_img_object(imagerotate($this->img, $deg, 0)); + } + + if ($mirror) { + $this->mirror(); + } + } + } + + public function mirror() + { + $width = imagesx($this->img); + $height = imagesy($this->img); + + $src_x = $width -1; + $src_y = 0; + $src_width = -$width; + $src_height = $height; + + $imgdest = imagecreatetruecolor($width, $height); + imagealphablending($imgdest,false); + imagesavealpha($imgdest,true); + + imagecopyresampled($imgdest, $this->img, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height); + $this->set_img_object($imgdest); + } + + private function set_img_object($new) + { + assert($new !== false); + + $old = $this->img; + $this->img = $new; + + if ($old != null) { + imagedestroy($old); + } + } + + private function fix_alpha() + { + imagealphablending($this->img,false); + imagesavealpha($this->img,true); + } + +} diff --git a/application/libraries/Image/Drivers/imagemagick.php b/application/libraries/Image/Drivers/imagemagick.php new file mode 100644 index 000000000..bcd8da40b --- /dev/null +++ b/application/libraries/Image/Drivers/imagemagick.php @@ -0,0 +1,121 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +namespace libraries\Image\Drivers; + +class imagemagick implements \libraries\Image\ImageDriver { + private $source_file; + private $arguments = array(); + + public static function get_priority($mimetype) + { + return 100; + } + + /** + * Create a new object and load the contents of file. + * @param file file to read + */ + public function __construct($file) + { + $this->read($file); + } + + public function read($file) + { + if (!file_exists($file)) { + throw new \exceptions\ApiException("libraries/Image/drivers/imagemagick/missing-file", "Source file doesn't exist"); + } + + $this->source_file = $file; + $this->arguments = array(); + } + + private function passthru($arguments) + { + $command_string = implode(" ", array_map("escapeshellarg", $arguments)); + passthru($command_string, $ret); + return $ret; + } + + public function get($target_type = null) + { + if ($target_type === null) { + return file_get_contents($this->source_file); + } + + $command = array("convert"); + $command = array_merge($command, $this->arguments); + $command[] = $this->source_file."[0]"; + + ob_start(); + switch ($target_type) { + case IMAGETYPE_GIF: + $command[] = "gif:-"; + break; + case IMAGETYPE_JPEG: + $command[] = "jpeg:-"; + break; + case IMAGETYPE_PNG: + $command[] = "png:-"; + break; + default: + assert(0); + } + $ret = $this->passthru($command); + $result = ob_get_clean(); + + if ($ret != 0 || $result === false) { + throw new \exceptions\ApiException("libraries/Image/thumbnail-creation-failed", "Failed to create thumbnail"); + } + + return $result; + } + + public function resize($width, $height) + { + $this->arguments[] = "-resize"; + $this->arguments[] = "${width}x${height}"; + } + + public function crop($x, $y, $width, $height) + { + $this->arguments[] = "+repage"; + $this->arguments[] = "-crop"; + $this->arguments[] = "${width}x${height}+${x}+${y}"; + $this->arguments[] = "+repage"; + } + + // Source: http://salman-w.blogspot.co.at/2009/04/crop-to-fit-image-using-aspphp.html + public function makeThumb($target_width, $target_height) + { + assert(is_int($target_width)); + assert(is_int($target_height)); + + $this->apply_exif_orientation(); + + $this->arguments[] = "-thumbnail"; + $this->arguments[] = "${target_width}x${target_height}^"; + $this->arguments[] = "-gravity"; + $this->arguments[] = "center"; + $this->arguments[] = "-extent"; + $this->arguments[] = "${target_width}x${target_height}^"; + } + + public function apply_exif_orientation() + { + $this->arguments[] = "-auto-orient"; + } + + public function mirror() + { + $this->arguments[] = "-flip"; + } + +} diff --git a/application/models/mfile.php b/application/models/mfile.php index 51c865900..f0a594609 100644 --- a/application/models/mfile.php +++ b/application/models/mfile.php @@ -93,20 +93,12 @@ class Mfile extends CI_Model { return $this->folder($hash).'/'.$hash; } - // Return mimetype of file - function mimetype($file) { - $fileinfo = new finfo(FILEINFO_MIME_TYPE); - $mimetype = $fileinfo->file($file); - - return $mimetype; - } - // Add a hash to the DB function add_file($hash, $id, $filename) { $userid = $this->muser->get_userid(); - $mimetype = $this->mimetype($this->file($hash)); + $mimetype = mimetype($this->file($hash)); $filesize = filesize($this->file($hash)); $this->db->insert("files", array( diff --git a/application/views/file/fragments/thumbnail.php b/application/views/file/fragments/thumbnail.php index 4bdf5be80..603832d44 100644 --- a/application/views/file/fragments/thumbnail.php +++ b/application/views/file/fragments/thumbnail.php @@ -4,7 +4,7 @@
diff --git a/application/views/file/upload_history_thumbnails.php b/application/views/file/upload_history_thumbnails.php index a061d9676..53ac762c2 100644 --- a/application/views/file/upload_history_thumbnails.php +++ b/application/views/file/upload_history_thumbnails.php @@ -12,7 +12,7 @@

Notice!

- Currently only jpeg, png and gif images are displayed here. If you are + Currently only images and pdf files are displayed here. If you are looking for something else, please switch to the ">list view which contains your complete history. -- cgit v1.2.3-24-g4f1b