Image conversion with PHP

0

It's my first day with php and I'm doing a select that returns me an image, how do I render this image. Here is my select and the image return:

class CooperMaisController {

public function getItens(){
    $connect = OpenConnection();
    $query = ibase_query($connect, " select * from CAD_PROD_PONTUACAO_RESGATE PP left join CAD_PRODUTOS CP on CP.EMPRESA = PP.EMPRESA and  CP.CODIGO = PP.PRODUTO ");

    if ($query){                
        $pagamentos = array();          
        while ($row = ibase_fetch_object($query)){              
            $pagamento = new coopermais();
            $pagamento->setCodigo($row->CODIGO);
            $pagamento->setPontuacao($row->PONTUACAO);
            $pagamento->setNome($row->NOME);
            $pagamento->setFoto($row->FOTO);                
            $pagamentos[] = $pagamento;         
        }           
        return $pagamentos;
    } else {
        return "Erro: ".ibase_errmsg();
    }

    unset($row);
    unset($query);
    ibase_close($connect);
   }    
}

When I give a var_dump in the image I visualize this:

I'mgettingusedtoJavaScriptandnodewhereIsavedtheconvertedimagetothedatabaseasbase64(itwasahugestring),nowwithphpwhensavingtheimageappearsthis:

When I look at the XML the image is rendered.

    
asked by anonymous 13.12.2017 / 14:22

1 answer

0

I'm not quite sure, but I think I have to convert to VARCHAR

SELECT *, CAST(SUBSTRING(blob1 FROM 1 FOR 80) AS VARCHAR(80)) AS IMAGECHAR

And it would look like this:

$pagamento->setFoto($row->IMAGECHAR);

However, if it is to be displayed in an HTML, you may have to use the data URI scheme , ie the image will have to be converted to base64, to use the URI date it is necessary to know the mimetype of the image you need to enable the file_info extension in PHP on your server, the code should look like this:

A generic example just to understand:

function mimeType($data)
{
    $mimetype = false;

    if (class_exists('finfo')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_buffer($finfo, $data);
        finfo_close($finfo);
    }

    return $mimetype ? $mimetype : 'imagem/jpeg'; //jpeg será um fallback pra facilitar
}

...

while ($row = ibase_fetch_object($query)){              
    $imagem = base64_encode($row->IMAGECHAR);

    echo '<img src="data:image/jpeg;base64,', base64_encode($imagem),'">';
}

...
    
13.12.2017 / 14:38