duanpi7578 2015-07-25 10:10
浏览 41
已采纳

同时打印图像和字符串

This is maybe looks stupid question. But I'm having trouble to print both image and string(s) in web page.

My codes:

<?php
    $file = 'uploadedimages/' . end(end($collectedResult));
    // $file='demo.gif';
    if(file_exists($file)){
        header('Content-Type:image/jpeg');
        header('Content-Transfer-Encoding: base64');
        //  $data=base64_encode(file_get_contents($file));
        //  echo $data;
        $im = imagecreatefromjpeg($file);
        imagejpeg($im);
        imagedestroy($im);
    }
    else
        echo 'No such file';

    header('Content-Type: text/html; charset=utf-8');
    echo '<br><p>' . json_encode($collectedResult, JSON_UNESCAPED_UNICODE) . '</p>';
?>

With these codes image's showing in browser. It's OK. But some json strings aren't showing.

I don't know where I should put the header('Content-Type: text/html; charset=utf-8'); and/or header('Content-Type:image/jpeg'); line(s).

Thanks.

  • 写回答

3条回答 默认 最新

  • doulan1073 2015-07-25 10:17
    关注

    You can't use header('content type') more than one time . if your objetive is download a image with text on it search for PHP image framework , if you just want to print a image and a text renderit using <img src='<?PHP echo $imageurlordata?>'> and then the text you can also do someting like this

    $data = base64_encode(file_get_contents($file));
    //  echo '<img src="image/base64:'.$data.'">';  // incorrect syntax
    echo '<img src="data:image/jpeg;base64,'.$data.'">'; // you can change jpeg as png, gif or smth else...
    

    or just use image url. I'm on phone sorry if there's any typo, hope its help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?