summaryrefslogtreecommitdiffstats
path: root/application/libraries/Image/Drivers/imagemagick.php
blob: fb4e4d88fdb7f0f03a4bdfc8a88d3d11686f87bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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)
	{
		$mimetype = $mimetype;
		$base = explode("/", $mimetype)[0];

		if ($base == "image"
			|| in_array($mimetype, array("application/pdf"))) {
			return 100;
		}

		return -1;
	}

	/**
	 * 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();
	}

	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]";

		switch ($target_type) {
			case IMAGETYPE_GIF:
				$command[] = "gif:-";
				break;
			case IMAGETYPE_JPEG:
				$command[] = "jpeg:-";
				break;
			case IMAGETYPE_PNG:
				$command[] = "png:-";
				break;
			default:
				assert(0);
		}

		try {
			$ret = (new \libraries\ProcRunner($command))->execSafe();
		} catch (\exceptions\ApiException $e) {
			throw new \exceptions\ApiException("libraries/Image/thumbnail-creation-failed", "Failed to create thumbnail", null, $e);
		}

		return $ret["stdout"];
	}

	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";
	}

}