I've seen a few cases of this online but every solution I try yields the same result. I have tried various versions of this same bit of code but I still am unable to display an image, either from the internet or from a folder.
My code looks like this:
<?php
$img = 'anything.jpg';
$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);
?>
And then this in the appropriate spot in the html document:
<?php
echo "<img src ='$img'>";
?>
My output looks like this: (only it is about 100 lines long, covering most of the page)
„•J€m’¥Id„©,ƒð‹"È!dY*
d¨E„Œœ6TêÞ BTç–dáfδ§Ýfΰɾ,ÙÔÌxà5IõcÁ=£TM´® ‡iNt4ÖMYí;ÅU©ª’§ª7’Ê:¡ú§šß”,¾5™ˆ
The image displays after all this.
I've been working at this quite a while now, any help would make a man's day.
The content-type of the image is in the array under mime, not img.
header('Content-type: ' . $getInfo['mime']);
More info is available in the PHP docs.
I ended up solving my problem using the following code:
<?php
$img = 'anything.png';
header("Content-type:image/png");
imagepng($img);
?>
<?php
echo "<img src ='$img'>";
?>
Related
I'm stuck for hours working on this. Few solutions found over the Net but no one seems to help me. I am having problem to display an image on the browser using PHP that takes the image from Postgres DB with column type bytea. I'm sure I'm missing something here. So some guidance are really appreciated. So I have this code below:
$prod = new Product();
$prod->display_latest_product();
if( $prod->exists() ){
$products = $prod->data();
foreach($products as $product){
echo $product->id;
echo $product->binarydata;
/* Solution below: I only get one image with broken link */
header('Content-type: image/png');
echo pg_unescape_bytea($product->binarydata);
/* Solution below: I only get one image with broken link */
header('Content-Type: image/png');
$data=fgets($product->binarydata);
print(pack('H*',$data));
/* bin2hex() expects parameter to be string but resource given */
echo bin2hex($product->binarydata);
/* Solution below: I only get one image with broken link */
$image = stripcslashes($product->binarydata);
header("Content-Type: image/png");
print($image);
}
}
I appreciate some explanation on this because I am still confuse after research and reading.
Finally I found the way to do it. Based on what I found in Handling Binary Data with PDO as I am using PDO to read the column in DB.
So I put these two lines in my foreach loop:
header("Content-Type: ".$product->mimetype);
fpassthru($product->binarydata);
And my file is displayed nicely. As #Musa pointed out that I can only print one image at a time, so I am going to use img with src="thepage.php?img=xx" just to display the image. I am developing e-Commerce app so not sure if this can affect performance because lots of images will be loaded. Any comments or suggestions are welcomed. Anyway I hope this could help people out there who is having same problem.
Documentation on fpassthru is here.
EDIT::SOLUTION FOUND
So I have finally got the real solution that tackles my problem defined in the comment below. And this time it does not involve PHP header at all.
foreach($products as $product){
ob_start(); // Let's start output buffering.
fpassthru($binarydata); //This will normally output the image, but because of ob_start(), it won't.
$contents = ob_get_contents(); //Instead, output above is saved to $contents
ob_end_clean(); //End the output buffer.
$dataUri = "data:image/png;base64," . base64_encode($contents);
echo "<img src='$dataUri' />";
}
I have got the solution here. And based on the comment in that thread, the trick is actually at the buffering. ob_start has to be used in pair with ob_end_clean(). Hope this helps people out there. Thanks.
Assuming that $product->binarydata has contents fetched from the BYTEA column, this method does work:
header('Content-type: image/png');
echo pg_unescape_bytea($product->binarydata);
except when having the client/server version mismatch mentioned in this answer, in which case the escape format for bytea should be used.
The code must not output anything else than the two lines above.
This seems like a SUPER easy question for people who knows PHP but I can't figure out how to make this work (probably because I know VERY little about PHP). Most question related to this asks how to output an image to either browser or disk, but I wanna do both. See example below:
<?php
header('Content-type: image/png');
$filename = "my_written_image.png";
$png_image = imagecreatefrompng('some_fancy_image.png');
imagepng($png_image, $filename);
imagepng($png_image);
?>
This won't work. The image won't show in the browser, but it will work if I remove this line: imagepng($png_image, $filename);
I'm guessing one can only "output" one thing using imagepng but how will I accomplish the above?
You should use the following code:
<?php
header('Content-type: image/png');
$filename = "my_written_image.png";
$png_image = imagecreatefrompng('some_fancy_image.png');
file_put_contents($filename, $image);
imagepng($png_image, $filename);
?>
My server is not turned on so I can't test the code but I think this should work.
Okay it seems my original solution actually did work. The problem was I was using (in my real code): if(readfile("myfile.png")) and this will return false if it's not found, but it will also throw an error if #readfile() is not used (note the #). The docs said:
an error message is printed
I didn't think this would cause the whole thing to fail. Sorry for my stupidness.
The code works fine if i display only image with the help of code.
<?php
// my code processing
header("Content-type: image/png");
imagepng($base_image);
?>
But if i use some other fields like echo some text or i want to put some buttons on my page.
I get error, for code:
<?php
echo "hi";
?>
<?php
// my code processing
header("Content-type: image/png");
imagepng($base_image);
?>
It gives me error : The displayed page contains some errors.
Can someone please help me in this regard.
Any output before the Content-Type header will break your code. The browser does not know you are trying to serve it an image since it will have already defaulted to text/html by the time your image data turns up. If you want an image at a given point in your page, you will need to serve it as a separate object. The easiest way is to wrap it in an <img> tag e.g. <img src="myimage_generator.php" />
This answer is based off of another SO answer. Your problem is that you're trying to send header info after you already sent data to the browser, which is not possible. Even so you can't display an image on a page with it's data alone. You need to base64 encode the image data first. This way you can build a whole HTML page and place this image anywhere on it and position it with CSS.
// Enable output buffering
ob_start();
imagepng($base_image);
// Capture the output
$imagedata = ob_get_contents();
// Clear the output buffer
ob_end_clean();
echo '<img src="data:image/png;base64,'.base64_encode($imagedata).'">';
I have a .php file that's supposed to load an image for display in an img tag(i.e., <img src="the_file.php?which=0"/>). It looks like this:
<?php
ob_clean();
header("Content-type: image/png");
include_once("util.php");
//Do a simple calculation to get $name from (int)$_GET["which"];
$im = imagecreatefrompng("protected_directory/".$name.".png");
imagepng($im,NULL,0,NULL);
imagedestroy($im);
ob_end_flush();
?>
It works correctly, but the image loads substantially slower than just loading it directly(i.e. <img src="protected_directory/the_name.png"/>, where "the_name" was calculated the same way as in the PHP file, but I can't just do this because the protected_directory isn't world readable).
My question is, why is this suddenly so much slower? It's not a large image, but nor is it terribly small.
If you're just displaying an existing file, use readfile() to output it to the browser. There's no need to go through all the overhead of creating an editable GD object for this.
imagepng is known to be slow, if you need to output images with a PHP script, use code like this:
$filename = md5(time() . mk_rand());
imagepng($im, $filename);
echo file_get_contents($filename);
As another answer, I figured out that you can use the third parameter to compress the image (PNG uses zlib). Setting it to 9 works about as well as the other solutions.
/*this code is working(echoing image) on localhost but not displaying on live site */
PLEASE HELP,,,,
<?php echo "<img src=geti/tipb_geti.php?id=$lastid width=200px height=180px >";
/*this below php block is tipb_geti.php file*/
<?php
include("connect.php");
$id=#addslashes($_REQUEST['id']);
$image_query=#mysql_query("select image from tipuranibasti where id=$id");
$image_row=#mysql_fetch_assoc($image_query);
$image=$image_row['image'];
header("content-type: image/jpeg");
echo $image;
?>
What's the memory limit on your live server? You're making TWO copies of the image data here:
$image=$image_row['image'];
which is utterly pointless. You could simply have
echo $image_row['image'];
instead and save yourself the extra wasteful/pointless copy operation.
Check if your characterencodigs. The livedb might have got UTF8 ancoding or the server has, but you not, if not, what is the output? Comment out header("content-type: image/jpeg"); and check if you get the same things live and local.
oh and i would prefer:
<?php print'<img src="geti/tipb_geti.php?id=' . $lastid . '" width="200px" height="180px" />';
makes your html/xhtml a bit more well formatted and print'test'; is slightly faster than print"test"; since "$var" will output the content of $var and '$var' wont, this is a little less overheat. But don't think you gain hyperspeed from that :P You won'T notice the bit more performance, I only think it provides a better code.