summaryrefslogtreecommitdiffstats
path: root/application/libraries/Image/Drivers/imagemagick.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-03-02 12:14:58 +0100
committerFlorian Pritz <bluewind@xinu.at>2015-03-02 12:14:58 +0100
commite9e9e635b337dc46111cdf95ccb16b7d28deb849 (patch)
treec69c6e12c48f71306cf54640b24b8c133f793090 /application/libraries/Image/Drivers/imagemagick.php
parentea8471781ee26f79c3835e68b31353601e78d0a8 (diff)
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 <bluewind@xinu.at>
Diffstat (limited to 'application/libraries/Image/Drivers/imagemagick.php')
-rw-r--r--application/libraries/Image/Drivers/imagemagick.php121
1 files changed, 121 insertions, 0 deletions
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 @@
+<?php
+/*
+ * Copyright 2015 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * 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";
+ }
+
+}