简体   繁体   中英

Show image created by imagejpeg

I create an image with an external function.

The function that returns the raw data of the image:

function create_image()
{
    ...
    ImageJPEG($myimg,NULL,85);
    $imgdata = ob_get_contents(); 
    ob_end_clean();          
    return $imgdata;
}

My script which should show what the image looks like:

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

Now the image is not complete in the <img> tag. If I make the quality 50 (with ImageJPEG($myimg,NULL,50); ) the image will be displayed completely. If I catch the rawdata and write it to the disk, the image will be complete in every quality.

$rawdata = create_image();
$im = imagecreatefromstring($rawdata);
ImageJPEG($im,"./test.jpg",90);

Only in the <img> tag it doesn't work.

Does anybody have an idea why it doesn't work?

I personally have never used this technique, I usually use a simple image tag with my image.jpg.php (for example) as an image. For this I put a header("Content-type: image/jpeg") in the image file and simply echo the data.

I really never have tried it with raw data, but I believe this would be preferrable, especially for maintenance (and compatibility).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM