2

Attempting to print out an image to the browser using code I copied from http://php.net/manual/en/function.imagecopyresampled.php.

It prints out a box of random characters.

CODE:

    public function printSummaryArticle($article, $copy, $thumb) {
    $src_image = Config::getAbsPath() . '/images/articles/' . $article['image'];
    echo
        '<div class="summary_article"><a href="/'
        . BreadCrumbs::getCrumb(1)
        . '/'
        . BreadCrumbs::getCrumb(2)
        . '/article/'
        . $article['id']
        . '"><h4>'
        . $article['title']
        . '</h4></a> ('
        . $article['date']
        . ')'
        . '<img src="data:image/jpeg;base64,'. imagejpeg($thumb->generateThumb($src_image, 300, 200)) . '"'
        . '<p>'
        . strip_tags($copy->truncateString($article['body'], 250, " "))
        . '</p><p><a href="/' . BreadCrumbs::getCrumb(1)
        . '/'
        . BreadCrumbs::getCrumb(2)
        . '/article/'
        . $article['id']
        . '"> Read more</a></p></div>';
}

Also tried:

    public function printSummaryArticle($article, $copy, $thumb) {
    $src_image = Config::getAbsPath() . '/images/articles/' . $article['image'];
    echo
        '<div class="summary_article"><a href="/'
        . BreadCrumbs::getCrumb(1)
        . '/'
        . BreadCrumbs::getCrumb(2)
        . '/article/'
        . $article['id']
        . '"><h4>'
        . $article['title']
        . '</h4></a> ('
        . $article['date']
        . ')';
        header('Content-type: image/jpeg');
        imagejpeg($thumb->generateThumb($src_image, 300, 200));
    echo
        '<p>'
        . strip_tags($copy->truncateString($article['body'], 250, " "))
        . '</p><p><a href="/' . BreadCrumbs::getCrumb(1)
        . '/'
        . BreadCrumbs::getCrumb(2)
        . '/article/'
        . $article['id']
        . '"> Read more</a></p></div>';
}

Same result. except with an added error claiming headers have already been sent.

How can I fix this?

obmon
  • 347
  • 2
  • 12

2 Answers2

3

imagejpeg() neither returns a string nor performs Base64 encoding. To work around this, capture its output in a PHP output buffer, and then Base64 encode the captured output:

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

Note that data: URLs are limited to 32 KB in Internet Explorer 8 and do not work in earlier versions of IE (source). If you need to support IE 8 and below, you may want to instead save the image as a separate file on the server. This is left as an exercise for the reader :)

(For an explanation of the "Headers already sent" warning, see How to fix "Headers already sent" error in PHP.)

Community
  • 1
  • 1
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • Thanks PleaseStand. I'm starting to think saving the thumb before outputting it might be the best way to do this.. – obmon Jan 06 '13 at 09:59
0

You haven't closed the image tag, but more importantly you haven't base64_encoded the image data

Before you echo that string

ob_start();
imagejpeg($thumb->generateThumb($src_image, 300, 200));
$imagejpg=ob_get_clean();

then this

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

You can't do what you're trying to do in the 2nd example

Popnoodles
  • 28,090
  • 2
  • 45
  • 53