Overview

Packages

  • Hylax
  • None

Classes

  • Hylax
  • Hylax_Driver
  • Hylax_Driver_hylafax
  • Hylax_Driver_spandsp
  • Hylax_Image
  • Hylax_SQL_Attributes
  • Hylax_Storage
  • Hylax_Storage_sql
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Hylax_Image Class
  4:  *
  5:  * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (GPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  9:  *
 10:  * @author  Marko Djukic <marko@oblo.com>
 11:  * @package Hylax
 12:  */
 13: class Hylax_Image {
 14: 
 15:     var $_data;
 16:     var $_cmd;
 17:     var $_pages = array();
 18: 
 19:     /**
 20:      * Constructor
 21:      *
 22:      * @param array $params  Any parameters needed for this image driver.
 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:         /* Run a ImageMagick identify command on the file to get the details. */
 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:         /* Figure out the dimensions from the output. */
 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:         /* Set resize based on whether preview or not. */
 84:         $resize = ($preview) ? ' -resize 140x200!' : ' -resize 595x842!';
 85: 
 86:         $tmp_file_out = Horde_Util::getTempFile('fax_preview', true, '/tmp');
 87:         /* Convert the page from the postscript file to PNG. */
 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:         /* Convert the page from the postscript file to PDF. */
107:         $command = sprintf('%s %s -', $this->_cmd['ps2pdf'], $tmp_file);
108:         Horde::logMessage('Executing command: ' . $command, 'DEBUG');
109:         passthru($command);
110:     }
111: 
112:     /**
113:      * Attempts to return a concrete Hylax_Image instance based on $driver.
114:      *
115:      * @param string $driver  The type of concrete Hylax_Image subclass to
116:      *                        return.
117:      * @param array $params   A hash containing any additional configuration or
118:      *                        connection parameters a subclass might need.
119:      *
120:      * @return Hylax_Image  The newly created concrete Hylax_Image instance, or
121:      *                      false on an error.
122:      * @throws Horde_Exception
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:      * Attempts to return a reference to a concrete Hylax_Image instance based
139:      * on $driver.
140:      *
141:      * It will only create a new instance if no Hylax_Image instance with the
142:      * same parameters currently exists.
143:      *
144:      * This should be used if multiple image sources are required.
145:      *
146:      * This method must be invoked as: $var = &Hylax_Image::singleton()
147:      *
148:      * @param string $driver  The type of concrete Hylax_Image subclass to
149:      *                        return.
150:      * @param array $params   A hash containing any additional configuration or
151:      *                        connection parameters a subclass might need.
152:      *
153:      * @return mixed  The created concrete Hylax_Image instance, or false on
154:      *                error.
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: 
API documentation generated by ApiGen