Home » Php » php – Image to browser without saving

php – Image to browser without saving

Posted by: admin July 12, 2020 Leave a comment

Questions:

How can I put image from the memory to the browser, without saving.

For example:

 function getImage()
 {

   $imageFile = imagecreatefromjpeg('Map.jpg');
   $imageObject = imagecreatefrompng('image2.png');
   imagealphablending($imageFile, true);    
   imagecopy(....);
   $ret = array($imageFile, $imageObject) ;  
   return $ret
 }
 <?php $ret = getImage(); ?>
 <img src = <?php $ret[0];? alt=''>

Is this possible, without saving?

How to&Answers:

Yes,

Just try imagejpeg($img);

and put into <img src= path to the PHP script which render the image

See sample at: http://php.net/manual/en/function.imagecreatefromjpeg.php

Answer:

Maybe if you would code your image to base64 and use it like that, it would work:

 <?php
    $img_str = base64_encode($imgbinary);
    echo '<img src="data:image/jpg;base64,'.$img_str.'" />';
    ?>

HTML:

<img src="data:image/jpg;base64,R0lGODlhCgAKAJEAAAAAAP///81Wv81WvyH5BAEAAAMALAAAAAAKAAoAAAIUjIViq+x7QpunwXoZ lXFu/mjIUgAAOw==" alt="image" />

I infered that you want to do this in one request.

Answer:

You should have a script which sends proper headers and then it should be recognized as an image by the browser. Something like:

<?php
ob_start();
// assuming you have image data in $imagedata
$length = strlen($imagedata);
header('Last-Modified: '.date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: '.$length);
header('Content-Type: image/jpeg');
print($imagedata);
ob_end_flush();

?>