简体   繁体   English

从img标签获取标题

[英]Getting header from img tag

Is there any way to retrieve headers from <img> tag using PHP? 有什么方法可以使用PHP从<img>标记中检索标头? The <img> has src attribute set to a randomly and dynamic generated PHP image, which sends header that I want to retrieve containing dynamic content based on content of the image. <img>的src属性设置为随机动态生成的PHP图像,该图像发送要检索的标头,其中包含基于图像内容的动态内容。

The HTTP headers are determined by the web server when they serve the image. HTTP标头由Web服务器提供图像时确定。 You can override the headers by serving the content directly from PHP. 您可以通过直接从PHP提供内容来覆盖标头。 However, this is much, much slower than the web server serving the image directly. 但是,这比直接提供图像的Web服务器要慢得多。 Most web servers allow for configuration to determine the HTTP headers for a given file extension. 大多数Web服务器都允许进行配置以确定给定文件扩展名的HTTP标头。

UPDATE: Add code to respond to comment. 更新:添加代码以响应评论。

HTML 的HTML

<a href="/images/my-image.php?type=png" />

In my-image.php: 在my-image.php中:

<?php
   if( $_REQUEST['type'] == 'png' ){
        header( 'Content-Type: image/png' );
        echo file_get_contents( '/websites/mywebsite.com/web/images/my-image.jpg' );
            exit();
   }

More checks and code is available here: 此处提供更多检查和代码:
http://www.php.net/manual/en/function.header.php#102175 http://www.php.net/manual/zh/function.header.php#102175

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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