PHP how to get image, in direct hex format from MSSQL?

0

I'm doing a system with PHP and I've already been able to put a word image or file directly inside MSSQL , SQLSRV , without needing put them in a folder, but I can not fetch them and display them in the normal format. Does anyone know how I can do this? This data recovery? Bank insertion code:

$dataHex = bin2hex($nome_final);

$imagedata = file_get_contents($nome_final);

$base64 = base64_encode($imagedata);


$sql = "INSERT INTO [RDO].[dbo].[documentosSub] (id_fun, nome_documento, hexa, base64) VALUES (?,?,?,?)";

$params = array($id_fun, $nome_final, $dataHex, $base64);

$stmt = @sqlsrv_query( $conn, $sql, $params);

$pasta = $_UP['pasta'];
$pastaP = explode('/', $pasta);
$pasta = $pastaP[0];


if (move_uploaded_file($_FILES['arquivo']['tmp_name'], $_UP['pasta'] . $nome_final)) {

header("Location: teste.php?pasta=$pasta&id_fun=$id_fun&cc=$cc");
} else {
echo "Não foi possível enviar o arquivo, tente novamente";
}

Display Code:

$sql = "SELECT * FROM [RDO].[dbo].[documentosSub] WHERE id_fun='$id_fun' ";
$stmt = @sqlsrv_query( $conn, $sql);
?>
<table align="center" width="400" border="1">
    <tr>
        <td align="center"><b>Id</b></td>
        <td align="center"><b>Nome</b></td>
        <td align="center"><b>Download</b></td>
        <td align="center"></td>
    </tr>
<?php 
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
            $id_documento = $row['id_documento'];
            $nome_documento = $row['nome_documento'];
            $hexa = $row['hexa'];
            $base = $row['base64'];
            $binary_string = pack("H*" , $hexa);
            $base64 = base64_decode($base);

            $arquivo = $prefixo.'/'.$pasta.'/'.$nome_documento;
            $imgP = explode(".", $arquivo);

?>

    <tr>
        <td align="center"><?php echo $id_documento ?></td>
        <td><?php echo $nome_documento ?></td>
        <td align="center"><a href="<?php echo $arquivo ?>"><?php echo $nome_documento ?></a></td>
        <!--<td><?php echo $base64 ?></td>-->
    </tr>
    <?php
    }
    ?>
</table>
    
asked by anonymous 08.10.2014 / 15:25

2 answers

3

From what I understand of your code, you are reading the image to the database, inserting it into Base64. Then you are doing the output of the image directly in your HTML.

In this way HTML does not result in an accurate image of using the element designated for this purpose, the <img/> .

Example

Tag <img/> with base64 image:

<?php

// gatafunhos que vem da base de dados em Base64
$minhaImagemEmBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';

// output da tag HTML com os gatafunhos em Base64
echo '<img src="data:image/jpeg;base64,'.$minhaImagemEmBase64.'"/>';

?>

See example on JSFiddle

We have a red dot in Base64 to be displayed on the page instead of establishing a link for an image file.

    
08.10.2014 / 22:01
0

You can use 'pack':

$binary_string = pack("H*" , $hex_string);
    
08.10.2014 / 16:19