-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagemagick.php
154 lines (124 loc) · 2.73 KB
/
imagemagick.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
// Copyright (C) 2011 Michael Bemmerl
// Released under a CC0 license (http://creativecommons.org/publicdomain/zero/1.0/)
// https://github.com/mibe/misc_stuff/blob/master/imagemagick.php
/*
* This is a simple interface for calling ImageMagick.
*
*
* Example:
*
* $im = new ImageMagick();
* $im->addSequence('rose.jpg');
* $im->addSequence('-resize 50%');
* $im->addSequence('rose.png');
* $result = $im->execute();
*
*
* Another example:
*
* $im = new ImageMagick();
* $im->addSequence('base.gif');
* $im->addSequence('-compose Screen');
*
* foreach($files as $file)
* {
* $im->startSubSequence();
* $im->addSequence('-clone 0');
* $im->addSequence($file);
* $im->addSequence('-composite');
* $im->endSubSequence();
* }
*
* $im->addSequence('-delete 0');
* $im->addSequence('-set delay 100');
* $im->addSequence('-layers optimize');
* $im->addSequence($file);
*
* echo $im->getCommandLine();
*
* $result = $im->execute($output);
*/
class ImageMagick
{
var $path;
var $sequences;
var $isSubSequence;
var $subSequences;
public function __construct()
{
if (!$this->detectExecutable())
throw new Exception("Executable not found");
$this->reset();
}
public function startSubSequence()
{
$this->endSubSequence();
$this->isSubSequence = TRUE;
}
public function endSubSequence()
{
if (!$this->isSubSequence)
return;
$newSequence = '(';
foreach($this->subSequences as $sequence)
$newSequence .= ' ' . $sequence;
$newSequence .= ' )';
$this->isSubSequence = FALSE;
$this->subSequences = array();
$this->addSequence($newSequence);
}
public function addSequence($input)
{
$this->isSubSequence ? $arr = &$this->subSequences : $arr = &$this->sequences;
$arr[] = $input;
}
public function execute(&$output = NULL)
{
$cmd = $this->buildCommandLine();
$cmd = escapeshellcmd($cmd);
$cmd .= " 2>&1";
exec($cmd, $output, $return);
$this->reset();
return $return == 0;
}
public function reset()
{
$this->sequences = array();
$this->subSequences = array();
$this->isSubSequence = FALSE;
}
public function getCommandLine()
{
return $this->buildCommandLine();
}
private function buildCommandLine()
{
$result = '';
foreach($this->sequences as $sequence)
$result .= ' ' . $sequence;
return $this->path . $result;
}
private function detectExecutable()
{
$file = 'convert';
if (PHP_OS == 'WINNT')
$file .= '.exe';
$path = __DIR__ . '/' . $file;
if (file_exists($path))
{
$this->path = $path;
return TRUE;
}
if (PHP_OS == 'WINNT')
$path = getenv('ProgramFiles') . '\\ImageMagick\\' . $file;
else
$path = '/usr/bin/' . $file;
if (file_exists($path))
{
$this->path = $path;
return TRUE;
}
return FALSE;
}
}