Displaying image from a folder - php

image displays in the browser whrn the following code is used.But when the code is used inside html,the image does not appear instead a small box appear.
<?php
header('Content-Type: image/jpeg');
readfile('http://localhost/picture013.jpg');
?>
for example when i use no image is displayed.
<html>
<body>
<?php
header('Content-Type: image/jpeg');
readfile('http://localhost/picture013.jpg');
?>
</body>
</html>
in short i wanted to display all the images from a folder.

Maybe you can try this one
<?php
$dir = "localhost/image"; // your folder name ex: image
$imgs = glob($dir ."/*.jpg"); // get your image files with .jpg
foreach ($imgs AS $i) {
echo "<img src='$i'>"; //
}
?>

Think about what this is doing for a moment. That file already exists, you're not inventing image data from nothing, so why are you not just creating an element and link to that image?
Put an <img src="some/location/picture013.jsp"> on the page with an echo/print instead, and whatever page needs to show that image will work just fine. In this case it's already a legal URL, so you can just add <img src="http://localhost/picture013.jpg"> to whatever page needs it.
If it wasn't, you could make the server simply resolve the location you're pointing to with an .htaccess rule, or simply have the file live in an already web-visible directory. There is no reason to make PHP proxy the image data when it's already a normal web-visible resource; in fact, you're just over-complicating your code with unnecessary steps.

Related

How to load images into a page using a php file but wait for all images load before displaying

This is the minimum code in order to demonstrate the effect.
https://replit.com/#computinginschools/imageload#index.php
This is a simple php file which loads image using an external script, image.php, and displays them on the page. The reason I have done this on my live site is because the images are stored outside the www folder and I need the image.php script to access them. To make it easier I have just put everything under the webroot.
In this example, all the images are located in a folder called images next to index.php.
<html>
<head>
<title>Image Load</title>
</head>
<body>
<?php
$images = glob('images/*.png');
foreach($images as $image){
echo('<img src="image.php?i='.urlencode($image).'" loading="lazy">');
}
?>
</body>
</html>
This is the image.php file which loads the images.
<?php
$path = urldecode($_GET['i']);
if (file_exists($path)) {
$extension = pathinfo($path)['extension'];
header('Content-type:'.$extension);
header('Cache-control:public, max-age=604800');
header('Accept-ranges:bytes');
header('Content-length:'.filesize($path));
header('Last-Modified: '.date(DATE_RFC2822, filemtime($path)));
header_remove('pragma');
echo(file_get_contents($path));
}
else {
echo("File not found: ".$file);
}
?>
The script works fine to display the images however, the loading is often prolonged with all the images loading in a 'wave' as the image.php file renders them on the screen after index.php has loaded. The REPL is fast so this is not really noticeable, however, on my production site, this effect is quite pronounced.
I would like to know if there is a way of delaying the loading of the images in index.php until all the images rendered by images.php have loaded. My intention is to set the display attribute of the images to none by default and then, when the index.php document has loaded, apply an appear animation to all the images on the page to make them fade into view.
It would be even better if I could embed some kind of loading animation in the container for each image.
Please help?
This can't really be done in PHP; the slowdown is from the browser going through the HTML and loading all of the images. If you would like to hide the page until everything is loaded, then you can set the HTML body to display: none, and the use JavaScript to remove that once all of the images are loaded. This has to be done from JavaScript because there isn't really any way to check on the browser's status from PHP.
If you must do this from PHP, then you can have all of the images inline using base64 encoding, although this is just about the least efficient way of serving images, and will probably cause major slowdowns. The format to do so is like this:
<img src="data:image/jpeg;base64,<?php echo base64_encode(file_get_contents($image)); ?>">

Display image from outside the web root ( public_html )

Background:
I have a folder with images called uploads or images located outside the website root. ( outside of public_html )
I am trying to print the image inside let's say image.php.
Rules for doing it:
I am trying to do it without using alias mod_rewrite in .htaccess.
Without showing a black background and image in the middle ( I don't want it like when browsing domain.com/image.png. like the example of the picture I mentioned below.
Without using another page and pass it as get.
What I tried:
I checked many questions, one of them is this and another is
this.
From following the current questions asked above and other tutorials, I came up with
this:
<?php
$location = dirname($_SERVER['DOCUMENT_ROOT']);
$image = $location . '/public_html/images/banned.png';
header('Content-Type:image/png');
header('Content-Length: ' . filesize($image));
echo file_get_contents($image);
?>
<img src=" <? echo $image; ?> "/>
It works fine, and here is an example:
Gyazo
However, this is not what I am looking for, or trying to do. Again, I am trying to view it as a normal image src as it will be used for a profile picture or other usages.
Any help will be much appreciated. Thanks in advance!
Change your image.php to
<?php
function image() {
$location = dirname($_SERVER['DOCUMENT_ROOT']);
$image = $location . '/public_html/images/banned.png';
return base64_encode(file_get_contents($image));
}
?>
In another file where you want to display that image, let's say test.php:
<?php
include("image.php");
?>
<img src='data:image/png;base64,<?= image(); ?>' >
This:
echo file_get_contents($image);
?>
<img src=" <? echo $image; ?> "/>
is wrong for a few reasons. First, you cannot mix raw image content with HTML markup that way. Also src specifies the URL of the image not the raw content.
You can either move your echo file_get_contents($image); and related code to separate file, i.e. image.php, then reference it in your src, passing image name as argument (i.e. image.php?img=foo.jpg) - note that if you do this wrong, you will be open to directory traversal attack). Alternatively you can try to use rectory traversal attackdata URI scheme as src argument and pass raw content directly that way.

Change PHP GD image output file url name/link

I have this problem, i have this script in php that creates a image on the fly, the problem is that the outputted image on the browser is allright, but i need to change it's name.
Ex: Index.php
<?php $url = "http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33" ?>
<img src="<?php echo $url ?>" />
The image_scrc.php is the file that creates the image, and as you can see i have several data that is passed by the get method.
In the image_scrc.php i have tryed
header('Content-type: image/jpg');
header('Content-Disposition:inline; filename="'.$random_name_jpeg.'"');
but the html link is is always appearing like this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
if i select the image on browser and then select copy image link it copies just like this also.
however, when I save the image it assumes the random_name.jpg, but only on save!
i've tried everything, even htaccess rules but nothing seems to work !!
it's this possible to acomplish? transform this
http://www.somesite.com/cls/image_scrc?_aaa=qJ7VgSxWBLG3FfNQVB%2BKI58kfHQulPHZLuLYaZAG6Tk%3D&_ab=3ctGTf547wdsAGjY%2F5FASE%2BpBnbQEAcrhbJzCHQ7mGs%3D&_acb=89e62acf3b4d254abf0c3ab30d6ebb33
to this
http://www.somesite.com/cls/random_name.jpg
i cant have the image on the server side! and must be displayed on the fly
Thanks in advance.

Generating QR Code using PHP QrCode

After using the PHP QrCode lib I discovered that for some reason when using a dynamic page with scripts and dialog boxes (JQuery) that when trying to output a QR code in a .png format I get weird symbols instead of the actual generated .png file.
Heres what I have tried:
Created a seperate file with just:
<?php
//include only that one, rest required files will be included from it
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>
Works great,
Attempt 2:
File opened within a dialog box type using JQuery UI:
<?php
header stuff...
include "phpqrcode/qrlib.php";
...
?>
<html>
...
<?php
QRcode::png('barrda554');
?>
..
</html>
In this attempt I get multiple funky symbols for some reason:
�PNG IHDRWWKK/PLTE���U��~�IDAT8��ѱ � P# �c��n :V�L�#�k
y��)�|F��5`ڸzHF|l���
%Z"e�Ы�D{\�ގ����p`�f�eh�������k�[BJeJ�c����,�^�gu�m|Q��o��W����g�
�#�s�<�y��k�m��!v�.��(+�u���$s�-�n$߫>�gR�`IEND�B`�
This has stumped me and I am unsure on how I should approach this to fix it.
Let me know your ideas,
David
UPDATE:
After putting header('Content-Type: image/png'); within the file that JQuery opens, no cigar.
Here is the actual file:
http://jsfiddle.net/T4nEP/
The problem is here:
<html>
<?php
QRcode::png('barrda554');
?>
</html>
To understand what this is doing, imagine that you open a regular PNG file in a text editor, and just copy/paste the contents directly into your HTML file. It's not going to show an image - it'll just be garbage, like you're seeing.
To include an image in an HTML file, you need to use the <img> tag, and point to the URL of the image. In this case, the URL of the image would be a PHP script that outputs nothing except the PNG contents - like this:
<img src="qrcode.php">
And then in qrcode.php, generate the image:
<?php
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>
If you need some information from the HTML page in order to generate the image, you can include it as query parameters in the URL, like this:
<img src="qrcode.php?product=1&format=2">
And then get those values in your PHP like this:
<?php
include "phpqrcode/qrlib.php";
$product = $_GET['product'];
$format = $_GET['format']
// ...
// whatever you need to do to generate the proper code
// ...
QRcode::png('barrda554');
?>
And finally - there are ways to include the image data directly into the HTML page, but it's not supported by all browsers, and is not recommended because it makes the page size much larger and prevents the browser from being able to cache the image separately.
You can see more about base64 data URLs here and here.
Make sure you have the following php code to generate the correct Content-Type header so the browser knows how to render the data its receiving.
header("Content-Type: image/png");

How to retrieve image from server and display using json parsing?

I have created a form which takes in images into a folder. I have only one image per folder and I want to display the image from the specified folder.
Say I've uploaded one picture to folder name uploads. Now I want to retrieve that image from the folder and display it.
How do I do that?
When a file is uploaded with your form, it becomes a file on your server, immediately. The web server puts it in a temporary directory and PHP tells you where it was put through the $_FILES array. You can immediately access this file, you can move it somewhere within your website's documents, and you can immediately print out an <img> tag pointing to where the file was put.
Stop writing "an img tag won't work" as that is exactly how you display the image.
Read the PHP manual page "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
The PHP manual should always be the first place you go when you're trying to do something you haven't done before.
<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>
or:
echo '<img src="/path/to/the/upload/folder/'.$filename.'"/>";
First you should have the image and path (or user) names in PHP variables. Then use an IMG tag to display them like :
<img src="some_path/uploads/<?php echo $username . '/' . $imagename; ?>">
you can't do it without knowing the name of the image file.
<?php
header('Content-Type:image/jpeg');
readfile('path_to_your/image.jpg');

Categories