forked from sigbertklinke/R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.php
More file actions
executable file
·73 lines (60 loc) · 2.3 KB
/
Convert.php
File metadata and controls
executable file
·73 lines (60 loc) · 2.3 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
<?php
/*
(C) 2006- Sigbert Klinke (sigbert@wiwi.hu-berlin.de),
This file is part of R/Octave Extension for Mediawiki.
The R/Octave Extension is free software: you can
redistribute it and/or modify it under the terms of
the GNU General Public License as published by the
Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
The R/Octave Extension is distributed in the hope that
It will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General
Public License along with the R/Octave Extension.
If not, see http://www.gnu.org/licenses/.
*/
class Convert {
public $cmd;
public $gin;
public $gout;
public $gopt;
private function getCmd ($cmd, $post='') {
$path = array('/usr/bin/', '/usr/local/bin/', '/bin/');
$n = count($path);
for ($i = 0; $i < $n; $i++) {
$cmdf = $path[$i] . $cmd;
if (file_exists($cmdf)) { return (trim($cmdf) . ' ' . $post); }
}
$cmdf = `which $cmd`;
if ($cmdf!='') {
$cmdf = trim($cmdf) . ' ' . $post;
return $cmdf;
}
throw new Exception('Convert.php: ' . $cmd . ' command not found');
}
function __construct($in, $out, $option=' ') {
if (in_array($in, array('pdf')) && in_array($out, array('png', 'jpg'))) {
$this->cmd = $this->getCmd('convert');
$this->gin = $in;
$this->gout = $out;
$this->gopt = $option;
}
if (!isset($this->cmd)) { throw new Exception('Convert.php: Either graphic input or output is not supported: ' . $in . ' -> ' . $out); }
}
public function convert($filename) {
$path = pathinfo($filename);
if (strcmp($path['extension'], $this->gin)) throw new Exception('Convert.php: Can not convert "' . $filename . '"');
$fn = $path['dirname'] . DIRECTORY_SEPARATOR . $path['basename'] . '.' . $this->gout;
$cmd = $this->cmd . ' ' . $filename . ' ' .$this->gopt . ' ' .$fn . ' 2>&1';
$out = null;
$val = -1;
exec ($cmd, $out, $val);
if ($val!=0) { throw new Exception("Convert.php: " . implode("\n", $out)); }
if (!file_exists($fn)) { throw new Exception('Convert.php: File "' . $fn . '" does not exist'); }
return (basename($fn));
}
}
?>