检测图像是否嵌入

检测图像是否嵌入

本文介绍了检测图像是否嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始编写自己的图像主机,但是我有一点问题:

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

如果您直接查看链接,我想显示一个HTML页面(例如Domain .com / img / 123)通过浏览器和图像,如果您通过

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">

以方便使用。

是有可能检测链接是直接查看还是链接是用PHP嵌入的?

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

推荐答案

你可以使用 htaccess 用于此目的的文件:

You can use a htaccess file for this purpose:

当浏览器加载嵌入的图像时,他已经知道了期望的格式,所以他会在请求文件时,将此信息添加到 HTTP:Accept 标头。 (或至少将其缩小为任何图像类型)

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)

如果浏览器直接访问文件(地址栏中的网址),他不知道这一点,所以他会将 text / html 添加到 HTTP:接受标题。

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.

从chrome中提取:

Extract from chrome:

direct:接受text / html,application / xhtml + xml,* / *

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

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

使用此信息来捕获直接访问案例:以下示例将重定向上的Access http:// localhost / test / myimage.gif index.php?url = / test / myimage.gif

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]

另一个文件,如 http://localhost/test/test.php 可以正确使用< img src =http://localhost/test/myimage.gif/> 不会发生重定向,因为没有接受:text / html 将被发送。

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.

请记住,这对 test :将图像嵌入某处后,当您直接访问图像时,浏览器缓存将不再加载数据。因此,它看起来像直接访问是可能的。但是,如果您按F5刷新缓存的图像,则将应用重定向。
(保持调试工具打开以禁用缓存)

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)

关于你的评论。我忽略了你想使用一个人工网址随时呈现图像。这改变了设计htaccess ofc的方式。

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.

以下htaccess的行为应符合您的预期:

The following htaccess should behave as you expect:


  • 如果请求Uri以斜线结尾,后跟数字(即 / 2537263 ),它被视为有资格重写。

  • 如果是直接访问(Http-Accept说 text / html )它被重写为 wrapperpage.php

  • 如果是嵌入式访问(HTTP-Accept表示 * text / html)它被重写为 image.php

  • 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]

注意:如果你省略 [R] Opti在,用户将看不到网址中反映的重定向。

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

我使用的示例页面代码:

Example page code I used:

wrapperpage.php:

wrapperpage.php:

THIS IS MY WRAPPER PAGE:

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

IMAGE IS WRAPPED.

image.php(我假设你确定图片的逻辑)

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);

?>

所以:


    浏览器中
  • http:// localhost / test / 1234 - > wrapperpage.php?id = 1234

  • http:// localhost / test / 1234 embedded - > image.php?id = 1234

  • http://localhost/test/image.php?id = 1234 - >返回png-image 。

  • 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.

这篇关于检测图像是否嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:18