This question already has answers here:
Return a PHP page as an image
(6 answers)
Closed 8 years ago.
I have an image in a directiory, lets say: test.jpg.
I also have 2 other PHP file in this directory called: new.php and load.php
Now, what I want to do is, when people visit the website new.php, they should need to see the test.jpg image.
However! I would also need that if I place the new.php into an <img> tag, it should also display the image correctly.
I have tried to call the image in an img tag, but it doesn't load correctly, when I insert the new.php into img tags.
<img src="test.jpg">
How is that even possible to make a php work as an image - if you know what I mean?
using:
header('Content-Type: image/jpeg');
readfile('path/to/image/screenshot.jpg');
other variants:
#readfile('path/to/image/screenshot.jpg');
won't have any errors if file does not exist.
AND
readfile('path/to/image/screenshot.jpg') or die('Image file not found');
will kill your script with a predefined error message if file does not exist.
The most useful alternative, as suggested by #hd:
$file = 'path/to/image/screenshot.jpg';
readfile(file_exists($file) ? $file : 'path/to/image/image_not_found.jpg');
Which will choose $file or if it does not exist, the default image_not_found.jpg
You may need to change the mime-type in the header() for other image types.
Note: The other answer is very much as functional as this one, but this one uses no memory and in general is faster and more flexible than the other answer.
Try this:
new.php
header("Content-Type: image/jpeg");
$im = imagecreatefromjpeg("test.jpg");
imagejpeg($im);
imagedestroy($im);
and use it like this
<img src="new.php"/>
Related
I am trying to display img_a.png or img_b.png based on the access level of the user (e.g. signed in or not). Of course the content (in this case img_a and img_b) should not be available to the general public.
I tried a few solutions, none of them helps me and that's why I look for help here. What I tried so far is:
Based on the user checks I tried adding the resources folder to "open_basedir" which is a lame option but was looking the easiest solution. All ended up by raising a warning that resource is not in the basedir folder through it's obviously there.
Attempted to put the resources in the public folder and restrict them via .htaccess. But in this case they got restricted not only for the unwanted audience but for everyone.
Final and closest attempt was to put back the images outside the webroot and write a class that validates the access and serves the image like:
class Image {
...
public function getImage() {
...
header('Content-Type: '.$this->type);
readfile($this->item);
which afterwards is displayed in the initial script through:
echo "<img src=".$image->getImage($file).">";
The problem above is that the headers were already sent so I could either stream the image or the html output of the .php page but not both. Do I have a way out there?
Create a script that checks whatever user attribute you want, determines what image to serve, and read/send that image. Use the script URL as the <img src=... attribute, ie
<img src='/scripts/user_image.php'>
Something like this will work for PNG images. Similar functions exist for GIF, JPG, etc.
<?php
// do stuff here to determine file name of image to send
if($_SESSION['userlevel']=="ADMIN"){
$imageFilename="admin_image.png";
}
// Create Image From Existing File
$image = imagecreatefrompng($imageFilename);
//Set the Content Type
header('Content-type: image/png');
// Send Image to Browser
imagepng($image);
// Clear Memory
imagedestroy($image);
exit;
?>
OK, per your comment, I think you are referencing things wrong.
My working script is exact as above, only the if-then is commented out. I'm just assigning the filename to the variable. I've named the script user_image.php.
A simple index.html file to reference the image -
<html>
<head><title>test</title></head>
<body>
Hello World!<br />
<img src="user_image.php">
</body>
</html>
And it Just Works. See it in action at https://itsjustcrap.com/img or grab source at https://itsjustcrap.com/img/src.zip
I'll leave it up for a few days, but other than a png image and a commented out if-then, the source is exact same as what is in this post.
Actually I think I found a solution to the headers confict I had.
Displaying the image as data like:
<img src="data:image/png;base64, <?=base64_encode(file_get_contents($file))?>" alt="img">
instead of feeding it inside the source attribute with readfile() php script like:
<img src="image.php?<?=$file?>" alt="my_image">
where the last one was giving me a headers conflict when inside html page.
Discovered the solution over here How to display Base64 images in HTML?
This question already has answers here:
How to serve documents from outside the web root using PHP?
(2 answers)
Closed 8 years ago.
Okay, so I'm not so experienced with PHP, and I've been searching for hours for a way to access an image file outside of the document root. I know there are many answers to this question, sort of, but none that actually helped me.
So what I have so far is a folder structure like this (ignore the odd file names):
-img
-imagez.php
-logo.php
-public_html
-files.php
I put this code inside of files.php:
<?php include('/home/byonexco/img/imagez.php'); ?>
If I access files.php from my browser, I see the content of imagez.php, as is expected.
My problem is, I want to be able to do the same thing with the file logo.png. The folder img is not publicly accessible, so I know I have to call the image with PHP.
How can I get logo.png to show on the page when someone accesses the file files.php?
You could write a very simple script like
<?php
header('Content-Type: image/png');
if (strpos($_GET['img'], '..') === false) // check for quackery
readfile('../img/' . $_GET['img']);
and access it like
/img.php?img=logo.png
However there are a couple disadvantages to this solution:
Relaying the image through PHP costs time and performance
The script is possibly subjectible to exploits, letting an evil person retrieve any file on the server
You're far better off with hosting images directly accessible.
As the image isn't publicly accessible, you'd need to get the image via PHP then output the image with the correct header
$image = file_get_contents('path/to/image.png';
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
This question already has answers here:
A PHP script to let users download a file from my website without revealing the actual file link in my website?
(4 answers)
Closed 9 years ago.
I want to make images shopping site in which I want people buy images then they can download them.
My problem is how to create hidden path to image that people download the image and don't know the real path of the image.
You can call a php file to download the image and not the real image/path.
Like this you can call the real path inside your php file with something like:
$path = "/public_html/yourPath/";
if (! isset($_GET['img'])) {
die("Invalid URL");
}
$imageName = filter_var($_GET['img'], FILTER_SANITIZE_STRING);
$finalPath = $path.$imageName;
header('Content-type: octet/stream');
header('Content-Type: image/jpg');
header("Content-Disposition: attachment; filename=$finalPath;");
readfile($finalPath);
You can read more about it here.
Store the images in an offline location (not www) and retreive them with PHP, so they can access the image for example like this: http://yoursite.com/index.php?file=filename and then PHP will go and return that file from the offline location. You just need to set the correct headers so the content is not treated like a web page but an image instead. Now obviously, such link is still public so you need to add some more information to it to authenticate the downloader.
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).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
rendering html to png (server-side)
I have a DIV element with some images positioned within the DIV element. I want to make a JPG or a PNG file out of that, so people can save it for their Facebook timeline cover image. I found a LOT of scripts and tutorials, but nothing really fills my needs. This can't really be a very hard thing to do?
I already got my host to install iMagick.
You should get the positions via JavaScript (I guess you are using js to set the positions of the images in the div) and send them to a PHP-Script. With help of the PHP GD manual you can easily generate a png oder jpeg image.
You can try
WKHTMLTOIMAGE
I had to achieve the same thing, only for something else than Facebook and I used webkit2png
I save the HTML code on a temporary HTML file, then I run the python webkit2png command on that temporary HTML file which converts it to PNG. It requires xvfb (apt-get install xvfb)
I then use the following command in PHP:
exec('xvfb-run --server-args="-screen 0, 1024x768x24" python webkit2png.py --log=/tmp/webkit2pnglog.log --output=' .$fullUploadPath . ' ' . $fullPath);
Where $fullUploadPath is the location of the target file and $fullPath is the location of the temporary HTML file.