2

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?

6
  • How are you outputting the image to an "img-tag"? As data URL?
    – deceze
    Jul 4, 2012 at 7:52
  • No, i post the rawdata in the img-tag like shown above Jul 4, 2012 at 7:53
  • Well... that is a data URL. :)
    – deceze
    Jul 4, 2012 at 7:59
  • ok, sorry i didnt know that. i thought you mean an url to an image :-) Jul 4, 2012 at 8:00
  • Note that this is a very inefficient method of outputting images, and it won't work in older versions of Internet Explorer. It would be much preferable to create the image in a separate image resource.
    – Pekka
    Jul 4, 2012 at 8:01

1 Answer 1

0

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).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.