简体   繁体   中英

Displaying an image created with imagecreatefromstring

Let's say I have the code that looks something like:

<?PHP

//
//... stuff here
//

$im = imagecreatefromstring( $imageData );

echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";


//
// more stuff here
//
?>

What do I replace /* what goes here? */ with so my image data will display?

Thank you.

What do I replace /* what goes here? */ with so my image data will display?

The location you highlighted is the so called src attribute of the img HTML-tag Docs . The value is a so called URI Docs .

In your case you want that URI to point to the image data in question. You have not specified which type the image should be output as, so I will assume it's a PNG image in the following example.

You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URI Wikipedia :

<?PHP

//
//... stuff here
//

$im = imagecreatefromstring( $imageData );

ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);

echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";

//
// more stuff here
//
?>

Even this is the most straight forward way, it is not always recommended to do so because the image data will be returned with the HTML to the browser. If the image is large, this is commonly considered an overhead.

Instead of using the data: URI you can place any other URI in there as well, for example a HTTP URI that is pointing to a PHP script on your server that is returning the image. Such a script can be very simple:

<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);

This is comparable to what Marc B suggested, see his answer as well .

<?php

$img = imagecreatefromstring($string);

header('Content-type: image/jpeg');
imagejpeg($img);

should be all you need. Doing it with the image tag as you are, you'd need to output the image to a temporary file and point the image tag at that (which incurs a second HTTP request), or use a data url .

You have to save the resource to a file first or output it using something like imagepng() in a separate request.

See imagecreatefromstring() documentation for more information.

If you want to use a Data URI scheme , you can try this instead:

<?php

// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
           . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
           . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
           . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';

echo '<img src="data:image/png;base64,'. $imageData .'" />';

I think you can do something like this...

$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";

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