Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display image without showing its file path

Tags:

php

Is there any way to show an image without its path. I mean not using HTML tag, I'd like to use PHP to show image. Because if I use HTML, someone can save or share that image.

Edit: I'm sorry about my question is not clear. I don't mean preventing saving image. I mean not showing the file path "/path/to/image.jpg" on URI or HTML. Because I don't want user copy and share the link which contains that image right on my website. The only way to share that image is to "Save Image As" and share it. Anyway thank you all.

This is my solution:

<?php
$image = 'new.png'; 
$content = file_get_contents($image); 
header('Content-Type: image/jpeg');
echo $content; exit();
like image 976
emeraldhieu Avatar asked Dec 10 '22 02:12

emeraldhieu


2 Answers

If the browser can view it, the user can save it. There's no way around that rule.

The only exception to this is if you recreate the image using 1x1 pixel divs of the colour of each pixel in the image, which is extremely heavy-handed and only usable in the slightest when the image is very small.

like image 83
Niet the Dark Absol Avatar answered Jan 03 '23 15:01

Niet the Dark Absol


Even if you output the image with PHP, it will need to use HTML. There is no way to prevent a user from saving an image.

You could try using the image as a background-image with CSS, which will prevent non-advanced users from saving the image, but anyone who knows how to inspect the DOM or read CSS won't have any issue saving it.

Consider this - by the time a user sees the image on a page, their browser has already downloaded the file to the user's hard drive.

like image 34
dtbarne Avatar answered Jan 03 '23 15:01

dtbarne