<?php
global $orders;
global $ticket_txt;
global $dbconnection;
global $dbuser;
global $dbpassword;
global $CFG_PrintEnable; 

include_once '_ini/cfg.php';
include_once '_function/_funs.php';

$dbuser="sa";
$dbpassword="densen916423330";

 
	try {
		//$filename="$dbdir/$dbdir.db3";
		//$orders = new PDO('sqlite:'.$filename);
		$orders = new PDO($dbconnection,$dbuser,$dbpassword);
		$orders->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  //反射错误码和错误信息。 

	}
	//捕获异常
	catch (Exception $e) {
		//echo 'Message: ' .$e->getMessage();
	}

	    $query_RecordsetCFG = "SELECT  Usercode, [Image] FROM Products where [Image] is not null";
        $values             = $orders->query($query_RecordsetCFG);
     	 $Image=null;
        if (is_iterable($values)) {
            foreach ($values as $value) {
                $data= $value['Image'];
				 $data=preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $data);	
				$imgbin=hex2bin($data);
				$imageData = base64_encode($imgbin);
				$src = 'data:;base64,'.$imageData;
				//$src = 'data: '.mime_content_type($imageUrl).';base64,'.$imageData;
				echo '<img src="', $src, '">';
			print "<hr/>";
				//header('Content-type: image/jpeg');
				//echo $Image;
				//header("Content-Type: image/bmp");
				//remove the ole header
				//$data = substr($data,78);
				//print the pic in the browser
				//$src=data_uri($data,'image/jpeg');
				//print $src;
				file_put_contents("Pho/" . $value['Usercode'] . ".jpg", $imgbin, true);


				//echo '<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO    9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />';
			}//-end-foreach
        } //-end-if (is_iterable($values))


		die;
		$img='[encoded data, 16916 characters]';
 
                 print $img;
				print "<hr/>";

//header('Content-Type: image/bmp',true);
$src=data_uri($img,'image/png');

//print $src;
//file_put_contents("test.png", $src, true);


$h2 = '424D36030000000000003600000028000000100000001000000001001800000000000003000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA0D2FD3BA5FC39A9FC4BABFC77BDFD50ADFC38A5FC37A5FC4DACFC87C6FDE3F2FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDBEEFE39A3FC4CCDFE56D7FE3FC0FD57D8FE61E3FF61E3FF59DAFE3DBDFD289DFBABD7FEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF91CAFD2BAAFC6CEFFF6BEDFF46C4E83AB6E13AB6E140BDE46CEEFF59DBFE209EFCACD7FEFFFFFFFFFFFFFFFFFFEBF6FF30A0FB55D6FE63E5FC28A2D528A2D539B6E139B6E11C94CD1287C565E7FC5ADBFE249AFBE5F3FFFFFFFFFFFFFF93CBFD3ABAFD6CEEFF28A2D555D5F466E8FF66E8FF66E8FF66E8FF49C8EC1C94CD6CEEFF3DBDFD86C5FDFFFFFFFFFFFF52AEFC55D6FE69ECFF66E8FF66E8FF66E8FF66E8FF66E8FF66E8FF66E8FF44C2E869EBFF58DAFE4BABFCFFFFFFFFFFFF37A4FC5FE1FF68EAFF8CEEFFD9F9FFBCF5FF66E8FF66E8FFBCF5FFD9F9FF8CEEFF67E9FF62E3FF35A4FBFFFFFFFFFFFF36A3FC5FE1FF7AEDFFFFFFFF6F6F6F5F5F5F9FF1FF9FF1FF6F6F6F4F4F4FFFFFFF79ECFF61E3FF36A5FBFFFFFFFFFFFF54AFFC54D5FE85F0FFFFFFFF0F0F0F000000A9F2FFA9F2FF0F0F0F000000EFEFEF85EFFF58D9FE4CACFCFFFFFFFFFFFF97CDFD38B8FD6CEEFFF5FEFF9F9F9F9F9F9F82ECFF82ECFF9F9F9F8F8F8FF5FEFF6CEEFF3BBBFD8AC7FDFFFFFFFFFFFFEDF7FF2B9CFB50C9EF6CB5BFACE5EFB2F3FF66E8FF66E8FF95EFFFB2F3FF86F1FF58D9FE269AFBE8F4FFFFFFFFFFFFFFFFFFFFBBDFFE1C7FCD0000000E1E2049A3B068EAFF5BCEE050B2C03D87901029301B80CEB5DCFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBADEFE165A951C5C7F35869F59D4F0429BB015364019526F135286B2DAFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFECF6FF98CDFD57B0FC38A4FC37A4FC55AFFC93CBFDEAF5FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
$s = pack('H*', strtolower($h2));
//echo $s;
//file_put_contents("test.bmp", $s, true);

function data_uri($contents, $mime)
{
 
    $base64   = base64_encode($contents);
    
    return ('data:' . $mime . ';base64,' . $base64);
}
 function hextobin($hexstr) 
{ 
    $n = strlen($hexstr); 
    $sbin="";   
    $i=0; 
    while($i<$n) 
    {       
        $a =substr($hexstr,$i,2);           
        $c = pack("H*",$a); 
        if ($i==0){$sbin=$c;} 
        else {$sbin.=$c;} 
        $i+=2; 
    } 
    return $sbin; 
} 	
 
$imgbin=hex2bin($data);

$imageUrl = 'Photo/85.jpg';
$image = file_get_contents($imageUrl);
$imageData = base64_encode($imgbin);
$src = 'data:;base64,'.$imageData;
//$src = 'data: '.mime_content_type($imageUrl).';base64,'.$imageData;
echo '<img src="', $src, '">';
//echo $fileres;



?> 



