<?php

require_once('BitStream.php');

class 
FlashJPEG {
    private 
$_data;  
        function 
__construct($data) {
        
$this->_data $data;
    }
    function 
convertFromFlashJPEG($jpegtables '') {
      
$jpegdata $this->_data;
      if (
$jpegtables != '') {
        
// strip SOI, EOI
        
$jpegtables_len strlen($jpegtables);
        
$jpegtables substr($jpegtables2$jpegtables_len 4);
        
// search SOF0
        
$sof0 sprintf("%c%c"0xFF0xC0); // baseline
        
$sof2 sprintf("%c%c"0xFF0xC2); // progressive
        
$pos strpos($jpegdata$sof0);
        if (
$pos === FALSE) {
          
$pos strpos($jpegdata$sof2);
        }
        if (
$pos=== FALSE) {
          
fprintf(STDERR"Can't found SOF[0|2] tag\n");
          return 
'';
        }
        
        
$jpegdata substr($jpegdata0$pos) . $jpegtables 
          
substr($jpegdata$posstrlen($this->_data));
        
      }
      
$soi sprintf("%c%c"0xFF0xD8); // start of image
      
$eoi sprintf("%c%c"0xFF0xD9); // end of image
      
$remove_data $eoi $soi;
      return 
str_replace($remove_data''$jpegdata);
    }
    function 
convertToFlashJPEG() {
      ;
    }
}