image/jpg hacky support
[pear] / File / Convert / Solution.php
1 <?php
2
3 class File_Convert_Solution
4 {
5     var $type = 0;
6     var $from;  // mimetype
7     var $to; // mimetype
8     var $ext;  // target extension
9     
10     var $debug = 0;
11     var $last = '';
12     var $log = array();
13     var $target = false;
14     
15     function __construct(  $from ,$to)
16     {
17          
18         $this->from = $from;
19         $this->to = $to;
20         
21         require_once 'File/MimeType.php';
22         $mt = new File_MimeType();
23         $this->ext = $mt->toExt($this->to);
24          
25         //$this->last = $this; //?? used where?
26         
27     }
28     function debug($str)
29     {
30         if ($this->debug) {
31             if (is_callable($this->debug)) {
32                 call_user_func($this->debug,$str);
33             } else {
34                 echo $str . "<br/>\n";
35             }
36         }
37         $this->log[] = $str;
38     }
39     
40     
41     function exec($cmd)
42     {
43         $this->debug("EXEC: $cmd");
44         $ret = `$cmd`;
45         $this->debug("RETURNED:  $ret");
46         $this->cmd = $cmd ."\n" . $ret;
47         return $ret;
48     }
49     
50     function count() // ??? why!?
51     {
52         return 1;
53     }
54     function add($in) 
55     {
56         require_once 'File/Convert/SolutionStack.php';
57         $ret = new File_Convert_SolutionStack();
58         $ret->list[] = $this;
59         if ($in->type == 0) {
60             $ret->list[] = $in;
61             return $ret;
62         }
63         $ret->list = array_merge($ret->list, $in->list);
64         return $ret;
65         
66     }
67     
68     // convertion methods
69     function runconvert($fn, $x=0, $y=0, $pg=false)
70     {
71         if (!file_exists($fn)) {
72             $this->cmd = "ERROR:". $fn . " does not exist";
73             return false;
74         }
75         if (!$this->ext) {
76             die("missing mimetype");
77              
78         }
79         
80         $this->debug("runconvert : {$fn}, {$x}, {$y}, {$pg}");
81   
82         
83         return $this->convert($fn, $x, $y, $pg);
84     }
85     
86     function targetName($fn,$x,$y)
87     {
88         return $fn .'.'. $this->ext;
89     }
90     
91     
92     function convertExists($fn, $x, $y)
93     {
94          
95         if (!$this->ext) {
96             return false;
97         }
98         $fn = $this->targetName($fn, $x, $y);
99      
100         return file_exists($fn) ? $fn : false;
101          
102     }
103     
104     function convert($fn,$x,$y,$pg) {
105         die("Convert not implemented for " . get_class($this));
106     }
107    
108    
109    
110     static $deleteOnExit = false;
111     /**
112      * generate a tempory file with an extension (dont forget to delete it)
113      */
114     
115     function deleteOnExitAdd($name)
116     {
117         if (self::$deleteOnExit === false) {
118             self::$deleteOnExit  = array();
119             register_shutdown_function(array('File_Convert_Solution','deleteOnExit'));
120             
121         }
122         self::$deleteOnExit[] = $name;
123     }
124     
125     function tempName($ext, $deleteOnExit=false)
126     {
127         
128         $x = tempnam(ini_get('session.save_path'), HTML_FlexyFramework::get()->appNameShort.'TMP');
129         unlink($x);
130         $ret = $x .'.'. $ext;
131         if ($deleteOnExit) {
132             $this->deleteOnExitAdd($ret);
133         }
134         return $ret;
135     
136     }
137    
138     static function deleteOnExit()
139     {
140         if (count(func_get_args())) {
141             trigger_error("Call deleteOnExitAdd ?!?");
142         }
143         foreach(self::$deleteOnExit as $fn) {
144             if (file_exists($fn)) {
145                 unlink($fn);
146             }
147         }
148     }
149     function which($n)
150     {
151         require_once 'System.php';
152         return System::which($n);
153     }
154      
155     
156 }