1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Hylax_Image {
14:
15: var $_data;
16: var $_cmd;
17: var $_pages = array();
18:
19: 20: 21: 22: 23:
24: function Hylax_Image()
25: {
26: $this->_cmd = array('identify' => '/usr/bin/identify',
27: 'convert' => '/usr/bin/convert',
28: 'ps2pdf' => '/usr/bin/ps2pdf14');
29: }
30:
31: function loadData($data)
32: {
33: $this->_data = $data;
34: }
35:
36: function getDimensions()
37: {
38: $tmp_file = Horde_Util::getTempFile('fax', true, '/tmp');
39: Horde::startBuffer();
40: var_dump($tmp_file);
41: Horde::logMessage('Created temp file:' . Horde::endBuffer() . ':', 'DEBUG');
42: $fp = fopen($tmp_file, 'w');
43: fwrite($fp, $this->_data);
44: fclose($fp);
45:
46:
47: $command = sprintf('%s %s', $this->_cmd['identify'], $tmp_file);
48: Horde::logMessage('External command call by Hylax_Image::getDimensions(): :' . $command . ':', 'DEBUG');
49: exec($command, $output, $retval);
50:
51: $init = strlen($tmp_file);
52:
53:
54: Horde::logMessage('External command output by Hylax_Image::getDimensions(): ' . serialize($output), 'DEBUG');
55: foreach ($output as $key => $line) {
56: if (substr($line, 0, $init) != $tmp_file) {
57: continue;
58: }
59: $info = explode(' ', $line);
60: $dims = explode('+', $info[2]);
61: list($width, $height) = explode('x', $dims[0]);
62: $this->_pages[$key]['width'] = $width;
63: $this->_pages[$key]['height'] = $height;
64: }
65: }
66:
67: function getNumPages()
68: {
69: if (empty($this->_pages)) {
70: $this->getDimensions();
71: }
72:
73: return count($this->_pages);
74: }
75:
76: function getImage($page, $preview = false)
77: {
78: $tmp_file = Horde_Util::getTempFile('fax', true, '/tmp');
79: $fp = fopen($tmp_file, 'wb');
80: fwrite($fp, $this->_data);
81: fclose($fp);
82:
83:
84: $resize = ($preview) ? ' -resize 140x200!' : ' -resize 595x842!';
85:
86: $tmp_file_out = Horde_Util::getTempFile('fax_preview', true, '/tmp');
87:
88: $command = sprintf('%s%s %s[%s] png:%s',
89: $this->_cmd['convert'],
90: $resize,
91: $tmp_file,
92: $page,
93: $tmp_file_out);
94: Horde::logMessage('Executing command: ' . $command, 'DEBUG');
95: exec($command);
96: echo file_get_contents($tmp_file_out);
97: }
98:
99: function getPDF()
100: {
101: $tmp_file = Horde_Util::getTempFile('fax', true, '/tmp');
102: $fp = fopen($tmp_file, 'wb');
103: fwrite($fp, $this->_data);
104: fclose($fp);
105:
106:
107: $command = sprintf('%s %s -', $this->_cmd['ps2pdf'], $tmp_file);
108: Horde::logMessage('Executing command: ' . $command, 'DEBUG');
109: passthru($command);
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
124: function &factory($driver, $params = array())
125: {
126: $driver = basename($driver);
127: include_once dirname(__FILE__) . '/Image/' . $driver . '.php';
128: $class = 'Hylax_Image_' . $driver;
129: if (class_exists($class)) {
130: $image = &new $class($params);
131: return $image;
132: }
133:
134: throw new Horde_Exception(sprintf(_("No such backend \"%s\" found"), $driver));
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155:
156: function &singleton($driver, $params = array())
157: {
158: static $instances;
159:
160: if (!isset($instances)) {
161: $instances = array();
162: }
163:
164: $signature = serialize(array($driver, $params));
165: if (!isset($instances[$signature])) {
166: $instances[$signature] = &Hylax_Image::factory($driver, $params);
167: }
168:
169: return $instances[$signature];
170: }
171:
172: }
173: