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?
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
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.
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();
?>
Related
i want use php page(random image) in img src(<img src="ads.php"/>)
my ads.php page:
<?php
header('Content-Type: image/png');
$img = array();
$img[] = '<img src="images/1.png" />';
$img[] = '<img src="images/2.png" />';
shuffle($img);
readfile ($img[0]);
?>
The values you put into your $img array need to be pathnames to graphics, not img tags. When you're using readfile() it's trying to find '<img src="images/1.png" />' in the local filesystem.
<?php
header('Content-Type: image/png');
$img = array();
//$img[] = '<img src="images/1.png" />';
//$img[] = '<img src="images/2.png" />';
$img[] = '/path/to/images/1.png';
$img[] = '/path/to/images/2.png';
shuffle($img);
readfile ($img[0]);
?>
Consider how the browser is seeing your code. It's parsing the page and encounters:
<img src="ads.php" />
and goes "ahah, I have to go hit the ads.php script on the site and it'll feed me an image". This is NO different than if you had
<img src="ads.jpg" />
The url you're telling the browser to hit on the server MUST serve up an IMAGE.
But instead of an image, you're serving up more html.
if you want to out the image you have to use
<?php
header('Content-Type: image/png');
$img = array();
$img[] = '/path/to/images/1.png';
$img[] = '/path/to/images/2.png';
shuffle($img);
readfile ($img[0]);
?>
Here's a jpg: http://i.stack.imgur.com/PIFN0.jpg
Let's say I'd like this rendered from /img.php?file_name=PIFN0.jpg
Here's how I'm trying to make this work:
/sample.php
<p>Here's my image:</p>
<img src="/img.php?file_name=PIFN0.jpg">
/img.php
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg($url);
?>
I would expect /sample.php to show the image. But this doesn't work. All I get is a broken image. What am I doing wrong?
Use imagecreatefromjpeg:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg(imagecreatefromjpeg($url));
?>
Reference: http://php.net/manual/en/function.imagecreatefromjpeg.php
Here is an working example:
<?php
function img_create($filename, $mime_type)
{
$content = file_get_contents($filename);
$base64 = base64_encode($content);
return ('data:' . $mime_type . ';base64,' . $base64);
}
?>
<img src="<?php print img_create('http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png','image/png'); ?>" alt="random logo" />
No need to use the GD functions:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
readfile($url);
?>
<?php
header("Content-Type: image/jpeg");
$url = "http://i.stack.imgur.com/PIFN0.jpg";
$imgContents = file_get_contents($url);
$image = #imagecreatefromstring($imgContents);
imagejpeg($image);
?>
header('Content-type: image/jpeg');
readfile($_GET['id']);
but in the new server, only can read the local images, the remote images is error.
I am trying to use header('Content-type: image/jpeg') to edit and display jpeg photos as follows.
header('Content-type: image/jpeg')
$filename = 'aaa.jpg';
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST,50);
imagejpeg($im);
imagedestroy($im);
In the same file,I also have other simple codes like $abc = $_POST['abc'].
After I put the header, code before the header and code after image destroy($im) no longer work. And when I put any code such as $_post['abc'] before the header, both header and code doesn't work. All codes were fine before I included header and code to manipulate and output image. It is my first time using header('Content-type: image/jpeg') and I cannot find the answer after trying for so long. Please help. Thank you.
If you want to output html page, do not send image header. But instead, at first output the transformed image to a file on your server and add the <img> or <a>nchor tag in your html page:
<html><body>
<?php
$output_dir = 'images';
if (!file_exists($output_dir)) {
mkdir($output_dir, 0777);
}
$filename = 'aaa.jpg';
$filename2 = 'aaa2.jpg';
if (!file_exists($filename)) {
echo 'Input image not exists!'; exit;
}
$im = imagecreatefromjpeg($filename);
imagefilter($im, IMG_FILTER_CONTRAST, 50);
imagejpeg($im, $output_dir.'/'.$filename2);
imagedestroy($im);
echo 'Original image:<br/><img src="'.$filename.'" /><br/>';
echo 'Transformed image:<br/><img src="'.$output_dir.'/'.$filename2.'" />';
?>
</body></html>
That image header send in case, you want to output it as standalone image. For more examples have a look at php.net.
I have php code which create pdf thumbnail as follows;
<?php
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
?>
Which is working well. But if I want to display the image in a web page, I have to use <img src=""> tag. Is there any way to remove header("Content-Type: image/jpeg");
from the syntax and echo image using <img src="">..? Or anybody tell me how to use the syntax to display the image inside a web page.
I am running apache with php5 in my Windows Vista PC..
With Imagick, you could use base64 encoding:
echo '<img src="data:image/jpg;base64,'.base64_encode($img->getImageBlob()).'" alt="" />';`
However, this method is kind a slow and therefore I recommend generating and saving the image earlier $img->writeImage($path).
you can try to display the image by this way:
// start buffering
ob_start();
$thumbnail = $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />";
Embedding an image using base64 is a COMPLETELY wrong way to go about the problem esp. with something stateless like a php web script.
You should instead use http parameters to have a single php file which can perform two tasks - the default will send html , and the parameter will instruct the php file to print the image. Below is the "standard" way to do it -
<?php
if (!array_key_exists('display',$_GET))
{
print('<html><head></head><body><img src="'.$_SERVER['PHP_SELF'].'?display=image"></body></html>');
} else
{
// The display key exists which means we want to display an image
$file ="test.pdf";
$im = new imagick(realpath($file).'[0]');
$im->setImageFormat("png");
$im->resizeImage(200,200,1,0);
header("Content-Type: image/jpeg");
$thumbnail = $im->getImageBlob();
echo $thumbnail;
}
?>
You can embed the raw image in you page, see the blog entry below for an example in page syntax.
http://www.sveinbjorn.org/news/2005-11-28-02-39-23
But i think it would be more productive to save the thumbnail on the filesystem and serve it as normal file. Otherwise you will be generating the thumbnail each time the page is accessed. Someone possibly uploaded this PDF file, so you may as well generate the thumbnail on upload time.
As I can see there are too many answers which are not accurate enough, so here goes mine:
This will print the image as you are doing it now(by the time of asking this question). As alternative to answer by #Vasil Dakov you should modify the snippet i gave you like this:
<?php
// ... Image generation goes here
header("Content-Type: image/jpeg");
ob_start();
print $im->getImageBlob();
$the_outputted_image = ob_get_flush();
?>
// Assuming that you use MVC approach and you are storing $the_outputted_image in a object and passing it to the view(ie. index.html or the HTML below the code).
//... Html code of index.html
<img src="data:image/jpg;base64 <?php print $the_outputted_image; ?>" alt="image" title="IMagick Generated Image" />
As another alternative is creating a script to generate the image, save it in some folder ( assuming img/ is the folder) and return only the path+filename+ extension to the file:
<?php
// ... Image generation goes here
header("Content-Type: image/jpeg");
$filename = 'img/' . md5(microtime()) . '.jpg'// Microtime is just as an example, you should use your own method.
$fp = fopen($filename, "x"); //Creating and opening the file for write-only
$im->writeImageFile($fp); //Writing the image to the file pointer (I would recommend writing it using, fwrite(), because it is binary-safe writing method)
fclose($fp);
?>
// Html
<img src="<?php print $filename; ?>" alt="image" title="IMagick Generated Image" />
documentation for Imagick::writeImageFile
In my case I found out a solution like this:
$im = new Imagick("http://www.yourserver.com/upload/file_name.pdf");
$im->setResolution(300, 300); // if higher image will be good to read
$im->setIteratorIndex(0); // read first page
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
ob_start();
print $im->getImageBlob();
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpg;base64,".base64_encode($contents)."' />"; //output as image
good luck.
The only solution would be to convert your image to base64 and include it as an embedded base64 image (data:image/png;base64, ). Further reference.
But this isn't supported in IE 6 and 7.
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header('Content-Type: image/png');
imagepng($im);
//echo '<a href=\'imagepng('.$im.')\'> Download </a>';
echo "-----------------";
} else {
echo 'An error occurred.';
}
It shows the image but does not echo "==============".
You told the browser to expect an image, therefore it's only expecting an image. Everything sent will be considered part of the data for that image. And no, it won't convert text you send into part of the image.
This is because of your
header('Content-Type: image/png');
It prevents you from echoing something else than the picture on this page.
(Well it doens't, but your Browser thinks this still part of the picture)
If you want to echo picture and Text, you need a seperat file, e.g. like this
echo '<img source="./pic.php" alt="pic" height="20" width="20" />';
echo '______________';
wehere pic.php is the path to the file wich echos the picture.
There's a way of displaying both. As a disclaimer, I shall add not to use this in production, though. Not all browsers support this and it ain't purty:
header('Content-Type: text/html');
ob_start();
imagepng($im);
$data = base64_encode(ob_get_clean());
printf('<img src="data:image/png;base64,%s" />', $data);
print('---------');
You are generating an image (hence the header) so if you echo something after that, you are basically echoing "---" after the bytes of the image.
Your browser thinks your .php script is an image now and doesn't display the '---' as plain text and tries to add it to the image.