Display image from outside the web root ( public_html ) - php

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.

Related

Displaying image from different disk

Hi i want to show images from different disk.
Here is my code.
is it possible to do that ?
$a = "D:/img/1.jpg";
<img src="<?php echo $a; ?>" alt="...">
Im using this on my localhost.This is not for the web.
i add screenshot here.
when i come above the img it shows the link. but not show img. and i use lightbox.
but without lightbox its not show again.
example
The sample you provided/attached is only doable if you load the html file directly to your browser. It is not possible if you load it via localhost. In your case, you will have to do something like this:
<?php
$image = file_get_contents('D:/img/1.jpg');
$image_codes = base64_encode($image);
?>
<image src="data:image/jpg;charset=utf-8;base64,<?php echo $image_codes; ?>" />
Reference: How to retrieve and show images from another drive using src attribute in <img> tag?
I'd suppose to use such URLs with protocol: $a = 'file:///D:/img/1.jpg';

Displaying image from a folder

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.

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.

Load relative path of images instead of <?php echo $img ?>

The timthumb function is not working after the security setting of web server is raised.
Images with "http://...." in URL are blocked.
I have found that <?php echo $img ?> was used to load the image.
Original code:
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?h=100&w=100&zc=1&src=<?php echo $img ?>" alt="<?php the_title_attribute(); ?>" width="100" height="100" />
Would someone please tell me how can I modify the code so that instead of
http://domain.com/wp-content/themes/a/scripts/timthumb.php?h=100&w=100&zc=1&src=http://domain.com/wp-content/uploads/thumbnail.jpg
the image URL will be
http://domain.com/wp-content/themes/a/scripts/timthumb.php?h=100&w=100&zc=1&src=../wp-content/uploads/2013/10/thumbnail.jpg
?
The code inside timthumb.php is super long. I will post the code if it's needed.
I know almost nothing about php code and only use plugins to build Wordpress websites.
You would have save my life. Thank you very very much!
It depends on where it is getting $img value from. If this is just a field on your article (through the extra parameters on the bottom), you could simply alter the url in your post. Alternatively, you could process the path, stripping out the http://domain.com/ and replace it with ../,
a la:
$img = str_replace("http://domain.com/", "../", $img);
just before echoing it out on to the page.

How do I call in an image in php using a href?

I have a bit of code that, using php, I want it to call in an image rather than what it is currently calling in (which is 'echo bloginfo('name');). However, I am sadly PHP illiterate and have no idea how to do this with the 'a href' posted below. Could anyone help me call to /images/logo.png? Many thanks in advance!
<h1><?php echo bloginfo('name'); ?></h1>
The tag for add images is <img src=""> on the srcattribute you need to write the url of the image you want (in your case /images/logo.png) so, replacing the code you pasted
<h1><img src="/images/logo.png"></h1>
Take into account that the path to the image you are using now is a relative one (relative to the document requiring the image) so you probably want to have the absolute url instead.
...place at the beginning of you webpage, near the top with the rest of your PHP/JavaScript:
<?php
$image_name = '/images/logo.png';
?>
...In this are you place allof your HTML...
<img src='<?php echo $image_name; ?>'>
Basically, you can have a variable named 'image_name' that you can have be anything.
If you wanted the HREF link area to be a variable that could be changed as well, just do this:
<?php
$image_name = '/images/logo.png';
$image_url = 'http://www.google.com';
?>
Then...
<a href='<?php echo $image_url; ?>' border='0'><img src='<?php echo $image_name; ?>'></a>
What is the return of get_settings() function? ==> name's Image?
I think this code is OK, but you should check the return value of your function.

Categories