1 | <?php |
---|
2 | require_once('fpdf.php'); |
---|
3 | |
---|
4 | class VariableStream |
---|
5 | { |
---|
6 | // Stream handler to read from global variables |
---|
7 | var $varname; |
---|
8 | var $position; |
---|
9 | |
---|
10 | function stream_open($path, $mode, $options, &$opened_path) |
---|
11 | { |
---|
12 | $url = parse_url($path); |
---|
13 | $this->varname = $url['host']; |
---|
14 | if(!isset($GLOBALS[$this->varname])) |
---|
15 | { |
---|
16 | trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING); |
---|
17 | return false; |
---|
18 | } |
---|
19 | $this->position = 0; |
---|
20 | return true; |
---|
21 | } |
---|
22 | |
---|
23 | function stream_read($count) |
---|
24 | { |
---|
25 | $ret = substr($GLOBALS[$this->varname], $this->position, $count); |
---|
26 | $this->position += strlen($ret); |
---|
27 | return $ret; |
---|
28 | } |
---|
29 | |
---|
30 | function stream_eof() |
---|
31 | { |
---|
32 | return $this->position >= strlen($GLOBALS[$this->varname]); |
---|
33 | } |
---|
34 | |
---|
35 | function stream_tell() |
---|
36 | { |
---|
37 | return $this->position; |
---|
38 | } |
---|
39 | |
---|
40 | function stream_seek($offset, $whence) |
---|
41 | { |
---|
42 | if($whence==SEEK_SET) |
---|
43 | { |
---|
44 | $this->position = $offset; |
---|
45 | return true; |
---|
46 | } |
---|
47 | return false; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | class MEM_IMAGE extends FPDF |
---|
52 | { |
---|
53 | // (c) Xavier Nicolay |
---|
54 | // V1.01 : 2006-11-19 |
---|
55 | |
---|
56 | // |
---|
57 | // CONSTRUCTOR |
---|
58 | // |
---|
59 | function MEM_IMAGE($orientation='P',$unit='mm',$format='A4') |
---|
60 | { |
---|
61 | $this->FPDF($orientation, $unit, $format); |
---|
62 | //Register var stream protocol (requires PHP>=4.3.2) |
---|
63 | if(function_exists('stream_wrapper_register')) |
---|
64 | stream_wrapper_register('var','VariableStream'); |
---|
65 | } |
---|
66 | |
---|
67 | // |
---|
68 | // PRIVATE FUNCTIONS |
---|
69 | // |
---|
70 | function _readstr($var, &$pos, $n) |
---|
71 | { |
---|
72 | //Read some bytes from string |
---|
73 | $string = substr($var, $pos, $n); |
---|
74 | $pos += $n; |
---|
75 | return $string; |
---|
76 | } |
---|
77 | |
---|
78 | function _readstr_int($var, &$pos) |
---|
79 | { |
---|
80 | //Read a 4-byte integer from string |
---|
81 | $i =ord($this->_readstr($var, $pos, 1))<<24; |
---|
82 | $i+=ord($this->_readstr($var, $pos, 1))<<16; |
---|
83 | $i+=ord($this->_readstr($var, $pos, 1))<<8; |
---|
84 | $i+=ord($this->_readstr($var, $pos, 1)); |
---|
85 | return $i; |
---|
86 | } |
---|
87 | |
---|
88 | function _parsemempng($var) |
---|
89 | { |
---|
90 | $pos=0; |
---|
91 | //Check signature |
---|
92 | $sig = $this->_readstr($var,$pos, 8); |
---|
93 | if($sig != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) |
---|
94 | $this->Error('Not a PNG image'); |
---|
95 | //Read header chunk |
---|
96 | $this->_readstr($var,$pos,4); |
---|
97 | $ihdr = $this->_readstr($var,$pos,4); |
---|
98 | if( $ihdr != 'IHDR') |
---|
99 | $this->Error('Incorrect PNG Image'); |
---|
100 | $w=$this->_readstr_int($var,$pos); |
---|
101 | $h=$this->_readstr_int($var,$pos); |
---|
102 | $bpc=ord($this->_readstr($var,$pos,1)); |
---|
103 | if($bpc>8) |
---|
104 | $this->Error('16-bit depth not supported: '.$file); |
---|
105 | $ct=ord($this->_readstr($var,$pos,1)); |
---|
106 | if($ct==0) |
---|
107 | $colspace='DeviceGray'; |
---|
108 | elseif($ct==2) |
---|
109 | $colspace='DeviceRGB'; |
---|
110 | elseif($ct==3) |
---|
111 | $colspace='Indexed'; |
---|
112 | else |
---|
113 | $this->Error('Alpha channel not supported: '.$file); |
---|
114 | if(ord($this->_readstr($var,$pos,1))!=0) |
---|
115 | $this->Error('Unknown compression method: '.$file); |
---|
116 | if(ord($this->_readstr($var,$pos,1))!=0) |
---|
117 | $this->Error('Unknown filter method: '.$file); |
---|
118 | if(ord($this->_readstr($var,$pos,1))!=0) |
---|
119 | $this->Error('Interlacing not supported: '.$file); |
---|
120 | $this->_readstr($var,$pos,4); |
---|
121 | $parms='/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>'; |
---|
122 | //Scan chunks looking for palette, transparency and image data |
---|
123 | $pal=''; |
---|
124 | $trns=''; |
---|
125 | $data=''; |
---|
126 | do |
---|
127 | { |
---|
128 | $n=$this->_readstr_int($var,$pos); |
---|
129 | $type=$this->_readstr($var,$pos,4); |
---|
130 | if($type=='PLTE') |
---|
131 | { |
---|
132 | //Read palette |
---|
133 | $pal=$this->_readstr($var,$pos,$n); |
---|
134 | $this->_readstr($var,$pos,4); |
---|
135 | } |
---|
136 | elseif($type=='tRNS') |
---|
137 | { |
---|
138 | //Read transparency info |
---|
139 | $t=$this->_readstr($var,$pos,$n); |
---|
140 | if($ct==0) |
---|
141 | $trns=array(ord(substr($t,1,1))); |
---|
142 | elseif($ct==2) |
---|
143 | $trns=array(ord(substr($t,1,1)),ord(substr($t,3,1)),ord(substr($t,5,1))); |
---|
144 | else |
---|
145 | { |
---|
146 | $p=strpos($t,chr(0)); |
---|
147 | if(is_int($p)) |
---|
148 | $trns=array($p); |
---|
149 | } |
---|
150 | $this->_readstr($var,$pos,4); |
---|
151 | } |
---|
152 | elseif($type=='IDAT') |
---|
153 | { |
---|
154 | //Read image data block |
---|
155 | $data.=$this->_readstr($var,$pos,$n); |
---|
156 | $this->_readstr($var,$pos,4); |
---|
157 | } |
---|
158 | elseif($type=='IEND') |
---|
159 | break; |
---|
160 | else |
---|
161 | $this->_readstr($var,$pos,$n+4); |
---|
162 | } |
---|
163 | while($n); |
---|
164 | if($colspace=='Indexed' and empty($pal)) |
---|
165 | $this->Error('Missing palette in '.$file); |
---|
166 | return array('w'=>$w, |
---|
167 | 'h'=>$h, |
---|
168 | 'cs'=>$colspace, |
---|
169 | 'bpc'=>$bpc, |
---|
170 | 'f'=>'FlateDecode', |
---|
171 | 'parms'=>$parms, |
---|
172 | 'pal'=>$pal, |
---|
173 | 'trns'=>$trns, |
---|
174 | 'data'=>$data); |
---|
175 | } |
---|
176 | |
---|
177 | /********************/ |
---|
178 | /* PUBLIC FUNCTIONS */ |
---|
179 | /********************/ |
---|
180 | function MemImage($data, $x, $y, $w=0, $h=0, $link='') |
---|
181 | { |
---|
182 | //Put the PNG image stored in $data |
---|
183 | $id = md5($data); |
---|
184 | if(!isset($this->images[$id])) |
---|
185 | { |
---|
186 | $info = $this->_parsemempng( $data ); |
---|
187 | $info['i'] = count($this->images)+1; |
---|
188 | $this->images[$id]=$info; |
---|
189 | } |
---|
190 | else |
---|
191 | $info=$this->images[$id]; |
---|
192 | |
---|
193 | //Automatic width and height calculation if needed |
---|
194 | if($w==0 and $h==0) |
---|
195 | { |
---|
196 | //Put image at 72 dpi |
---|
197 | $w=$info['w']/$this->k; |
---|
198 | $h=$info['h']/$this->k; |
---|
199 | } |
---|
200 | if($w==0) |
---|
201 | $w=$h*$info['w']/$info['h']; |
---|
202 | if($h==0) |
---|
203 | $h=$w*$info['h']/$info['w']; |
---|
204 | $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i'])); |
---|
205 | if($link) |
---|
206 | $this->Link($x,$y,$w,$h,$link); |
---|
207 | } |
---|
208 | |
---|
209 | function GDImage($im, $x, $y, $w=0, $h=0, $link='') |
---|
210 | { |
---|
211 | //Put the GD image $im |
---|
212 | ob_start(); |
---|
213 | imagepng($im); |
---|
214 | $data = ob_get_contents(); |
---|
215 | ob_end_clean(); |
---|
216 | $this->MemImage($data, $x, $y, $w, $h, $link); |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|
220 | ?> |
---|