Image display problems with GD Lib

Asked

Viewed 381 times

0

in front-end I have the following code

    $separador = explode('ID = ', $data['produto_nome']);
    $produtoSelecionado = $wpdb->get_results( "SELECT * FROM gs_produtos WHERE id = '$separador[1]'" );

    echo '<img src="'.get_bloginfo( 'template_url' ).'/voucher.php">';

In the voucher.php file I have the following code

// Header informando que é uma imagem JPEG
header( 'Content-type: image/jpeg' );

if( $produtoSelecionado ){

    foreach( $produtoSelecionado as $voucher );

        $path = get_bloginfo( 'template_url' ).$voucher->voucher;

        // PEGA URL DA IMAGEM NO BANCO DE DADOS
        $voucherCliente = imagecreatefromjpeg( $path );

        // CORES DA SAIDA DA IMAGEM
        $cor = imagecolorallocate( $voucherCliente, 255, 255, 255 );

        // TEXTO 01 - ESCRITO NA IMAGEM
        $nome = urldecode( ucfirst( $data['userName'] ) );

        // ESCREVENDO NA IMAGEM
        imagestring( $voucherCliente, 5, 15, 515, $nome, $cor );

        // ENVIA IMAGEM PARA O BROWSER OU ARQUIVO
        imagejpeg( $voucherCliente, NULL, 80 );

}

tried several ways, but the image does not appear, just appears that icon of corrupted image, it is the first time I use this lib and I am not able to present the image.

  • 1

    Try to comment on the header( 'Content-type: image/jpeg' ); and see if there are any error messages. In PHP error_log there appears some error related?

  • @Thomas shows no error, but in the browser console it says that the image was transferred as text/html

  • 1

    Try to put ini_set('display_errors', 1); error_reporting(E_ALL); before the header() and comment the header. I believe this will show the possible errors.

  • @Thomas remains error-free

  • he gets to enter the if?

  • @Thomas enters the if tranquil, if I try to make a include() he tries to print something that starts like this, which are many characters that possibly should be the image JFIF ;CREATOR: Gd-jpeg v1.0 (using IJG JPEG V80), quality = 80 C %# , #&')*)-0-(0%()( C (((((((((((((((((((((((((((((((((((((((((((((((((((��%a"�� ��� }! 1AQa"Q2 #B R $3br

  • 1

    adding ob_start(); anted from imagejpeg() and this after it $rawImageBytes = ob_get_clean(); echo "<img src='data:image/jpeg;Base64," . base64_encode( $rawImageBytes ) . " ' />"; I was able to display the image, but without the text I want to write in it

Show 2 more comments

1 answer

-1

SOLVED THAT WAY.

<?php
        // Header informando que é uma imagem JPEG
        header( 'Content-type: image/jpeg' );

        if( $produtoSelecionado ){

            foreach( $produtoSelecionado as $voucher );

                $path = get_bloginfo( 'template_url' ).$voucher->voucher;

                // PEGA URL DA IMAGEM NO BANCO DE DADOS
                $voucherCliente = imagecreatefromjpeg( $path );

                // CORES DA SAIDA DA IMAGEM
                $cor = imagecolorallocate( $voucherCliente, 0, 0, 0 );

                // TEXTO 01 - ESCRITO NA IMAGEM
                $nome = urldecode( ucfirst( $data['userName'] ) );
                $cpf  = urldecode( $data['userCpf'] );

                echo $cpf;

                // ESCREVENDO NA IMAGEM
                imagestring( $voucherCliente, 5, 325, 108, $nome, $cor );

                ob_start();

                // ENVIA IMAGEM PARA O BROWSER OU ARQUIVO
                imagejpeg( $voucherCliente, NULL, 80 );

                $rawImageBytes = ob_get_clean();

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

        }

    ?>
  • @Thomas solved old!

  • 1

    Nice. There was one missing ob_start(), well remembered. In my tests I managed to make it work without the ob_start(), I’ll look for why. Just a tip to make it easier: give an echo on the variable $rawImageBytes, since in front-end you call this script in src of <img> and waits for a return image/jpeg. It feels strange to return a <img>. In fact, it works?

  • 1

    Also, don’t forget to accept this as the valid answer.

  • @Thomas I set that I am no longer using the <img> let the script return the image even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.