简体   繁体   中英

How to hide image path from source code using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM