-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageMagick.php
More file actions
139 lines (121 loc) · 3.39 KB
/
ImageMagick.php
File metadata and controls
139 lines (121 loc) · 3.39 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php // vim:ts=3:sts=3:sw=3:et:
/**
* ImageMagic utility
*
* PHP Version 5
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @copyright 2005-2011 Bryan C. Geraghty
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
/**
* ImageMagic utility
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
class MindFrame2_ImageMagick
{
protected $convert = '/usr/bin/convert';
protected $identify = '/usr/bin/identify';
/**
* Creates a thumbnail
*
* @param string $source Source file
* @param string $destination Destination file
* @param int $size Size of thumbnail
*
* @return void
*
* @throws InvalidArgumentException if the source argument is an empty string
* @throws InvalidArgumentException if the destination argument is an empty
* string
*/
public function createThumbnail($source, $destination, $size)
{
if (empty($source))
{
throw new InvalidArgumentException(
'Argument #1 (source) cannot be empty');
}
if (empty($destination))
{
throw new InvalidArgumentException(
'Argument #2 (destination) cannot be empty');
}
$geometry = $this->fetchImageGeometry($source);
list($width, $height) = explode('x', $geometry);
if ($width > $height)
{
$new_width = round($size * ($width / $height));
$resize = sprintf('-resize %d ', $new_width);
}
else
{
$resize = sprintf('-resize %d ', $size);
}
$cmd = sprintf('%s "%s" %s-gravity center -crop %dx%d+0+0 "%s"',
$this->convert, $source, $resize, $size, $size, $destination);
$output = array();
exec($cmd, $output);
}
/**
* Creates a preview
*
* @param string $source Source file
* @param string $destination Destination file
* @param int $size Size of thumbnail
*
* @return void
*/
public function createPreview($source, $destination, $size)
{
if (empty($source))
{
throw new InvalidArgumentException('Argument #1 (source) cannot be empty');
}
if (empty($destination))
{
throw new InvalidArgumentException('Argument #2 (destination) cannot be empty');
}
$resize = sprintf('-resize %d', $size);
$cmd = sprintf('%s "%s" %s "%s"',
$this->convert, $source, $resize, $destination);
$output = array();
exec($cmd, $output);
}
/**
* Builds an associative array of the image meta-data
*
* @param string $image File system path
*
* @return array
*/
public function fetchImageData($image)
{
$output = array();
$cmd = sprintf('%s -verbose "%s"', $this->identify, $image);
exec($cmd, $output);
$output = MindFrame2_Array::reKey($output, ':');
return $output;
}
/**
* Returns the dimensions of the specified image
*
* @param string $image File system path
*
* @return string
*/
public function fetchImageGeometry($image)
{
$output = $this->fetchImageData($image);
$geometry = $output['Geometry'];
return $geometry;
}
}