I'm using http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation to access private files using php. I can get the data of the file by saying $object->body. I actually want to see the image in the browser or play the video in a video player. Is there a way to do that?
I think I need something like readfile. The problem is readfile is I need the path to the file. The path is private so I cannot use that. Is there a way to do a readfile of the binary data?
I put this in the php thinking this would help but it still displays the binary data.
header('Content: image/jpeg');
header('Content-Disposition: inline; filename=IMAG0108.jpg');
echo $object->body;
You just set the content-type header and output the readfile to the browser. What I do is create a new php file, like "showimage.php", that accepts an ID or some such to know what image to display. Then I use it in a browser page: .
In showimage.php, something like:
<?php
header('Content-type: image/png');
readfile('/var/images/' . $_GET['id'] . '.png');
// or
// echo $object->body;
?>
That would read a file from the local system and output it as an image. Off the top of my head, so I might have messed up that code!
header('Content: image/jpeg');
echo $object->body;
Should work fine (for JPEGs), you need know what filetype is in question and then send appropriate content headers.
Related
So there is this webpage where this guy managed to return raw images from the server
just by typing the id as a parameter.
http://photos.iitm.ac.in/byid.php?id=008576
even if you right click the image and open in a new tab, only the php gets opened.
does anyone know how he managed to do this?
I need this kind of functionality for profile pics
It's pretty simple. You just have to send the data with the right Content-type. So if you want to show a gif-image just uste:
Header("Content-Type: image/gif");
and then send out the image data.
You can do like this :
Suppose example URL is :
http://www.example.com/getImage.php?id=20
So, on getImage.php page get user image information from database and provide the image source :
Code should be like this:
$userImageName="testImage.gif"; //FROM DATABASE FOR id=20, Just for example
ob_clean(); //CLEAN THE OUTPUT BUFFER
header("Content-Type: image/gif"); //SET PAGE HEADER FOR IMAGE
echo file_get_contents('imageDirectory/'.$userImageName);
die;
Although it's not a direct answer to the asked question, since it was already answered by other people, I think it's nonetheless worth mentioning.
You can also control the file name that appears when you try to save the picture! To do that, use following header:
header('Content-Disposition', 'inline;filename=my_image_title.jpg');
so that your script looks like this:
header('Content-Disposition: inline;filename=my_image_title.jpg');
header('Content-Type: image/jpeg');
readfile($pathToImage);
//or echo $imageContent
Just make sure that you didn't accidentally output anything before making first call to header(), otherwise you might get "headers already sent" error.
Trying to see what actions can be performed with a PHP script that is being called via an image src like so:
<img src="http://example.com/script.php" />
Now, I have tried to include the PHP header() function in script.php:
<?php
header("Location: http://example.com");
I have also tried to echo an image url expecting the img to display it, which it didn't:
<?php
echo 'http://example.com/image.png';
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
No. A resource that is used as a src for an img tag needs to output image data, nothing else.
There are some exceptions, eg. a header("location: ....") redirect, but the redirect needs to point to another valid image resource, not a web site as you show in your example.
Check out the readfile() as a way to output your image file from your script.php
readfile($file);
Read more about it here in the manual:
http://php.net/manual/en/function.readfile.php
where Example #1 gives an idea of how to set up the headers.
The manual also states that:
readfile() will not present any memory issues, even when sending large
files, on its own.
and
A URL can be used as a filename with this function
ps: This was the way Wordpress Multisite used to open user uploaded (e.g. images) files.
Your script.php should return the output of an image with the correct headers. For instance:
<img src="/html/img/script.php" />
// Script.php
$file = "tiger.jpeg";
$type = "image/jpeg";
header("Content-Type: $type");
header("Content-Length: " . filesize($file));
readfile($file);
You should keep in mind that the src tag should directly point to an image file. However, it is possible to use PHP to create an image, for exmaple by using the GD library:
http://php.net/manual/en/book.image.php
So using:
<img src="http://example.com/script.php" />
is possible, as long as script.php really outputs an image file, for example by using the example as described here:
http://www.php.net/manual/en/image.examples-png.php
I used this kind of processing in the past to overlay texts on JPG images for a broker website (e.g. new, sold, for rent, etc.).
Are there any ways of doing such things with a PHP script that is being called in the img src attribute?
Yes, but the PHP Script has to output image data only, as stated in various other answers.
With that being said, just read the image and output it to the stream with readfile
header('Content-Type: image/png');
readfile($file);
exit();
I know I might be a couple years late to really help you, but the accepted answer just isn't true (anymore).
i want to print a image by using a img tag which src is a php file, the script will also process some action at server scirpt. Anyone have an example code?
here is my test code but no effect
img.html
<img src="visitImg.php" />
visitImg.php
<?
header('Content-Type: image/jpeg');
echo "<img src=\"btn_search_eng.jpg\" />";
?>
Use readfile:
<?php
header('Content-Type: image/jpeg');
readfile('btn_search_eng.jpg');
?>
Directly from the php fpassthru docs:
<?php
// open the file in a binary mode
$name = './img/ok.png';
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
?>
As an explanation, you need to pass the image data as output from the script, not html data. You can use other functions like fopen, readfile, etc etc etc
"<img src=\"btn_search_eng.jpg\" />" is not valid image data. You have to actually read the contents of btn_search_eng.jpg and echo them.
See here for the various ways to pass-through files in PHP.
UPDATE
what you can do without using include as said below in the comments:
try this:
<?
$img="example.gif";
header ('content-type: image/gif');
readfile($img);
?>
The above code is from this site
Original Answer
DON'T try this:
<?
header('Content-Type: image/jpeg');
include 'btn_search_eng.jpg'; // SECURITY issue for uploaded files!
?>
If you are going to use header('Content-Type: image/jpeg'); at the top of your script, then the output of your script had better be a JPEG image! In your current example, you are specifying an image content type and then providing HTML output.
What you're echoing is HTML, not the binary data needed to generate a JPEG image. To get that, you'll need to either read an external file or generate a file using PHP's image manipulation functions.
if you use base64 it would be
<?php
header('Content-Type: image/jpeg');
echo(base64_decode('BASE64ENCODEDCONTENT'));
?>
I really do not know any PHP but I'd love to do one simple thing:
I access a php page from within a <img src="/myhumbleimage.php" /> and I'd like to have an image returned from another URL.
I came up with:
<?php
header('Content-Type: image/png');
readfile('i' . rand(1,3) . '.png');
exit;
And it works:
Avatar selection http://vercas.webuda.com/img.php?.png
(Reload the page a few times!)
Check out readfile().
The basic idea is you send the appropriate MIME type headers (using header()) then deliver the file contents using readfile().
For example
<?php
// myhumbleimage.php
// Do whatever myhumbleimage.php does before the image is delivered
header('Content-Type: image/jpeg');
readfile('path/or/url/of/image/file.jpg');
exit;
Why not just reference the image directly then? If you are trying to hide the fact you are pulling an image from an external source, that external source will still be able to tell you are pulling their images.
Otherwise, pass a Content-Type header with the appropriate mime-type and echo the results of file_get_contents($imageUrl).
Just generate or read, then output the image using PHP.
...get image data from file or dynamically...
header('Content-type: image/png'); //or whatever MIME type
print $imgdata;
Or check out this: http://php.net/manual/en/function.imagepng.php
I've discovered problems if I didn't also include a 'Content-Length: ' header. The problems are crawler, proxy, and browser caching related. In worst cases the browser waits until timeout for more data.
It's in the spec' and solved all issues so I've always included it even if modern browsers may work without it. Who knows, there still may be a slight delay since the browser doesn't know when it has received the last segment.
Another problem I see here is that you are assuming a .png image format.
Better to create a specific function for the purpose so you can re-use it.
function returnImage( $path ) {
header( 'Content-Type: image/' . substr($path, -3) );
header( 'Content-Length: ' . filesize( $path ) );
readfile( $path );
exit;
}
I've made a lot of assumptions here (like the file exists and its extension is 3 characters) but this sequence seems to be the silver bullet in my experience.
Okay excuse the cryptic subject.
I have a system which dynamically fetches image data and does some modification on the fly.
Essentially, missing out the unimportant bits, this is my code:
$File->Filename = "testimage.jpg";
$File->Open();
$FileData = $File->Read();
header('Content-Type: image/jpeg');
echo $FileData;
The only output on the page is $FileData. Okay. When I run the script as is, Firefox presents me with a blank page and Chrome and IE give me a 'missing picture' box.
However oddly enough, when I remove the Content Type declaration, I can see the raw image data just fine. I have tested this with several images, granted all of the JPEG type but it clearly loads up the different pictures just fine, as the raw data changes successfully, and matches the raw content of the image itself.
Anyone have any idea why it would be failing to just display the image at this point?
You need to give more information in order to let the browser handle it correctly (use the correct type and length):
header('Content-Length: '.strlen($FileData),true); // EDIT: inserted the strlen function as suggested by kgb
header('Content-Type: image/jpeg');
Try setting Content-Length appropriately. Remove the trailing ?> to make sure there is no whitespace at the end of the script and ensure that the starting it at the very start of your script.
i'll combine phant0m's and Thariama's answers ;):
header('Content-Length: '.strlen($FileData), true);
header('Content-Type: image/jpeg');
die($FileData);