Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image to browser without saving

Tags:

php

image

save

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?

like image 377
OHLÁLÁ Avatar asked Feb 28 '11 10:02

OHLÁLÁ


3 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

like image 53
Adam Lukaszczyk Avatar answered Oct 05 '22 22:10

Adam Lukaszczyk


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.

like image 25
mailo Avatar answered Oct 06 '22 00:10

mailo


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();

?>

like image 40
Jan Zyka Avatar answered Oct 05 '22 22:10

Jan Zyka