How to hide image path from source code using PHP - php

I want to hide image path from website. To do that first I did this:
<?php
// get image path
$path = 'images/profiles/uploads/' . $list['thumbnail'];
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
?>
But this is very slow, So I try to implement like this, but code is not working:
download.php:
function setImgDownload($imagePath) {
$image = imagecreatefromjpeg($imagePath);
header('Content-Type: image/jpeg');
imagejpeg($image);
}
mypage.php:
<?php include('download.php'); ?>
<img src="<?php setImgDownload('images/Chrysanthemum.jpg') ?>" width="300"/>
If I put function and call function at the same page it's working:
function setImgDownload($imagePath) {
$image = imagecreatefromjpeg($imagePath);
header('Content-Type: image/jpeg');
imagejpeg($image);
}
setImgDownload('images/Chrysanthemum.jpg');
How to keep function in separate page?

Try changing your mypage.php to
<?php
include('download.php');
echo "<img src='".setImgDownload('images/Chrysanthemum.jpg')."' width='300'/>"; // calling function inside php
?>
Here, instead creating the HTML element outside php, put it inside php using echo and just escape while calling the php function. This should give you what you need.

Related

Displaying images stored in the server using 'path/filename' stored in the database

I just wanna ask if someone can explain to me why I cant display my images to the website that I'm creating. I stored the 'path/filename' in one of the columns in the database. While the image image is stored in the server.
<?php foreach $data as $val{?>
<img src="<?php echo base_url();?><?php echo $val->file;?>">
<?php }?>
Controller
$path = $_FILES["addFile"]["name"];
$extension = pathinfo($path, PATHINFO_EXTENSION);
$file_sc = 'uploads/screenshots/'.date("Ymdhms.").$extension;
$new_name = date("Ymdhms").".".$extension;
$target_dir = "./uploads/screenshots/";
$target_file = $target_dir.$new_name;
$type = $_FILES["addFile"]["type"];
$allowed = array('image/jpeg', 'image/png', 'image/gif');
if($_FILES["addFile"]["name"]){
if(!in_array($type,$allowed)){
echo 'File type is not allowed.';
} else {
move_uploaded_file($_FILES["addFile"]["tmp_name"],$target_file);
$this->MyModel->add($file_sc);
}
Model
function add_kudos($file_sc)
{
$query = $this->db->query("insert into tbl_kudos(file) values('$file_sc');
}
This is how it looks like in the database -> 'uploads/screenshots/20170224040231.jpg'
The file name in the server ->20170224040231.jpg
The image is not showing. Even if my url is correct.
Can someone explain to me why it's like this? I am new to this.
create a php file like this named showimg.php:
<?php
$img = $_REQUEST['img'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime=finfo_file($finfo, $img) ;
finfo_close($finfo);
header("Content-type: $mime");
$fp = fopen($img, 'rb');
fpassthru($fp);
?>
use your code to get the path of the image that you want to show from the DB in a var named $myimgpath and then use something like this:
<img src="<?php echo base_url()."showimg.php?img=".$myimgpath;?>">
First filename is different to second filename!
You can use urlencode() then print your image URL
Try this
<?php foreach ( $data as $val ) { ?>
<img src="<?php echo base_url( $val->file ); ?>">
<?php } ?>

Increasing security on imageUpload, issues with img src="image.php"

Cant seem to get the php script to execute and fetch the image script, the image placeholder.jpg is sored outside of the public_html webroot, here is what i do
Show image .php file
<img src="image.php?image=<?php echo urlencode('placeholder.jpg') ?>"/>
image.php
<?php
$file = basename(urldecode($_GET['image']));
$fileDir = '/noaccess/avatars/';
if (file_exists($fileDir . $file))
{
$contents = file_get_contents($fileDir . $file);
header('Content-type: image/jpg');
echo $contents;
}
?>
It is EXTREMELY unsecured to run that kind of script, because you are giving hackers access to your file system remember they can use ../ to navigate up the folders,
but anyway try this:
<?php
$file = basename(urldecode($_GET['image']));
$fileDir = $_SERVER['DOCUMENT_ROOT'] . 'noaccess/avatars/';
if (file_exists($fileDir . $file))
{
$imageRes = imagecreatefromjpeg($fileDir . $file);
header('Content-Type: image/jpeg');
// Output the image
#imagepng($imageRes);
// Free up memory
#imagedestroy($imageRes);
die();
}
?>

How can I render a remote image with php?

Here's a jpg: http://i.stack.imgur.com/PIFN0.jpg
Let's say I'd like this rendered from /img.php?file_name=PIFN0.jpg
Here's how I'm trying to make this work:
/sample.php
<p>Here's my image:</p>
<img src="/img.php?file_name=PIFN0.jpg">
/img.php
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg($url);
?>
I would expect /sample.php to show the image. But this doesn't work. All I get is a broken image. What am I doing wrong?
Use imagecreatefromjpeg:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
imagejpeg(imagecreatefromjpeg($url));
?>
Reference: http://php.net/manual/en/function.imagecreatefromjpeg.php
Here is an working example:
<?php
function img_create($filename, $mime_type)
{
$content = file_get_contents($filename);
$base64 = base64_encode($content);
return ('data:' . $mime_type . ';base64,' . $base64);
}
?>
<img src="<?php print img_create('http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png','image/png'); ?>" alt="random logo" />
No need to use the GD functions:
<?php
$url = 'http://i.stack.imgur.com/' . $_GET['file_name'];
header('Content-type: image/jpeg');
readfile($url);
?>
<?php
header("Content-Type: image/jpeg");
$url = "http://i.stack.imgur.com/PIFN0.jpg";
$imgContents = file_get_contents($url);
$image = #imagecreatefromstring($imgContents);
imagejpeg($image);
?>
header('Content-type: image/jpeg');
readfile($_GET['id']);
but in the new server, only can read the local images, the remote images is error.

Displaying an image created with imagecreatefromstring

Let's say I have the code that looks something like:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";
//
// more stuff here
//
?>
What do I replace /* what goes here? */ with so my image data will display?
Thank you.
What do I replace /* what goes here? */ with so my image data will display?
The location you highlighted is the so called src attribute of the img HTML-tagDocs. The value is a so called URIDocs.
In your case you want that URI to point to the image data in question. You have not specified which type the image should be output as, so I will assume it's a PNG image in the following example.
You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URIWikipedia:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);
echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";
//
// more stuff here
//
?>
Even this is the most straight forward way, it is not always recommended to do so because the image data will be returned with the HTML to the browser. If the image is large, this is commonly considered an overhead.
Instead of using the data: URI you can place any other URI in there as well, for example a HTTP URI that is pointing to a PHP script on your server that is returning the image. Such a script can be very simple:
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);
This is comparable to what Marc B suggested, see his answer as well.
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/jpeg');
imagejpeg($img);
should be all you need. Doing it with the image tag as you are, you'd need to output the image to a temporary file and point the image tag at that (which incurs a second HTTP request), or use a data url.
I think you can do something like this...
$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";
You have to save the resource to a file first or output it using something like imagepng() in a separate request.
See imagecreatefromstring() documentation for more information.
If you want to use a Data URI scheme, you can try this instead:
<?php
// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
echo '<img src="data:image/png;base64,'. $imageData .'" />';

PHP: How to convert an image from URL to Base64?

I want to convert image from its url to base64.
Do you want to create a data url? You need a MIME-Type and some other additional information then (see Wikipedia). If this is not the case, this would be a simple base64 representation of the image:
$b64image = base64_encode(file_get_contents('path/to/image.png'));
Relevant docs: base64_encode()-function, file_get_contents()-function.
I got to this question searching for a similar solution, actually, I understood that this was the original question.
I wanted to do the same, but the file was in a remote server, so this is what I did:
$url = 'http://yoursite.com/image.jpg';
$image = file_get_contents($url);
if ($image !== false){
return 'data:image/jpg;base64,'.base64_encode($image);
}
So, this code is from a function that returns a string, and you can output the return value inside the src parameter of an img tag in html. I'm using smarty as my templating library. It could go like this:
<img src="<string_returned_by_function>">
Note the explicit call to:
if ($image !== false)
This is necessary because file_get_contents can return 0 and be casted to false in some cases, even if the file fetch was successful. Actually in this case it shouldn't happen, but its a good practice when fetching file content.
Try this:-
Example One:-
<?php
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
?>
used as so
<style type="text/css">
.logo {
background: url("<?php echo base64_encode_image ('img/logo.png','png'); ?>") no-repeat right 5px;
}
</style>
or
<img src="<?php echo base64_encode_image ('img/logo.png','png'); ?>"/>
Example Two:-
$path= 'myfolder/myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
I'm not sure, but check this example http://www.php.net/manual/es/function.base64-encode.php#99842
Regards!
base64_encode

Categories