Detect if image is embedded

橙三吉。 提交于 2019-12-19 10:54:06

问题


I started to write my own image host but I have a little problem:

I want to display an HTML page if you directly view the link (eg. Domain.com/img/123) via the browser, and an image if you embed the link via

<img src="Domain.com/img/123">

to faciliate the use.

Is it possible to detect whether the link is directly viewed or the link is embedded with PHP?


回答1:


You can use a htaccess file for this purpose:

When the browser loads an embedded image, he already knows the format to expect, so he will add this information to the HTTP:Accept headers when requesting the file. (or at least reduce it to any image-type)

If the Browser is Accessing a file directly (url in the addressbar) he does not know this, so he will add text/html to the HTTP:Accept Header.

Extract from chrome:

direct: Accept text/html, application/xhtml+xml, */*

embedded: Accept image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5

Use this information to catch the direct access cases: The example bellow will redirect Access on http://localhost/test/myimage.gif to index.php?url=/test/myimage.gif.

RewriteEngine on

RewriteCond %{REQUEST_URI} .*\.gif        # redirect gifs
RewriteCond %{REQUEST_URI} !.*index\.php  # make sure there is no loop
RewriteCond %{HTTP:Accept} .*text/html.*  # redirect direct access
RewriteRule (.*) http://localhost/test/index.php?url=$1 [R,L]  

another file like http://localhost/test/test.php could properly use <img src="http://localhost/test/myimage.gif" /> No redirect will happen, since no Accept: text/html will be send.

Keep in mind, that this is a little bad to test: Once you had the image embedded somewhere, the browser cache will no longer load data when you access the image directly. Hence it will look like direct access is possible. But if you hit F5 to refresh the cached image, the redirect will apply. (Leave debugging tools open to have the cache disabled)


Update

As to your comment. I overlooked that you want to use an artifical url to present the image at any time. This changes the way to design the htaccess ofc.

The following htaccess should behave as you expect:

  • If the Request Uri ends with slash followed by numbers only (i.e. /2537263) it is considered to be eligible for rewrite.
  • If it is diret access (Http-Accept says text/html) it is rewritten to wrapperpage.php
  • If it is embedded access (HTTP-Accept says not *text/html) it is rewritten to image.php

htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI} /\d+$   
RewriteCond %{HTTP:Accept} .*text/html.*  
RewriteRule ^(.*?)$ http://localhost/test/wrapperpage.php?id=$1 [R,L]

RewriteCond %{REQUEST_URI} /\d+$   
RewriteCond %{HTTP:Accept} !.*text/html.*      
RewriteRule ^(.*?)$ http://localhost/test/image.php?id=$1 [R,L]

Note: if you ommit the [R] Option, users will not see the redirect reflected in the url.

Example page code I used:

wrapperpage.php:

THIS IS MY WRAPPER PAGE: 

<br />                   
<img src = "http://localhost/test/<?=$_GET["id"]?>" />
<br />

IMAGE IS WRAPPED.

image.php (Your logic to determine the picture goes there i assume)

<?php

//Load Image
$id = $_GET["id"];

//pseudoloading based on id... 
// loading... 
// done. 
$image = imagecreatefromgif("apache_pb.gif"); 

//output image as png.
header("Content-type: image/png");
imagepng($image);

?>

So:

  • http://localhost/test/1234 in browser -> wrapperpage.php?id=1234
  • http://localhost/test/1234 embedded -> image.php?id=1234
  • http://localhost/test/image.php?id=1234 -> Returns png-image.


来源:https://stackoverflow.com/questions/29413930/detect-if-image-is-embedded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!