【问题标题】:Making php generated image be seen as a file使php生成的图像被视为文件
【发布时间】:2014-07-30 09:42:23
【问题描述】:

我有这个代码来显示存储在公共网络文件夹之外的图像。

$result2 = mysqli_query($iddb, "SELECT url, extension FROM imagenes WHERE id=$id");
if ($row2=mysqli_fetch_array($result2, MYSQLI_BOTH))
    {   
        $ruta_archivo = $row2['url'];
        $extension =$row2['extension'];
        $filePath=$ruta_outside.$ruta_archivo;
        {
            $image = file_get_contents($filePath);
        }   
    }
header('Content-Type: image/.$extension.');
echo $image;

我可以在 html 标记中调用它,如下所示,它可以工作。

<img src='http://my_domain.com/content_image.php?id=11' height='40' width='40'/>

但是,当我在 facebook 上分享我的页面时,它无法定位为图像。我使用了 og: 标签,但该地址未被识别为图像。

您是否有将信息提供给客户的想法,以便客户将其理解为以文件名结尾的 url?喜欢

http://my_domain.com/imagenid11.jpg

谢谢

【问题讨论】:

  • 您的标题实际上是“Content-Type: image/.$extension.”,它不是有效的内容类型。您使用单引号字符串和/或字符串连接错误。
  • 试试header('Content-Type: image/'.$extension);
  • 这不是问题,图像正在被 标签识别,但感谢您的推荐。解析器实际上理解这种格式,即使它不是标准的。 php.net/manual/en/language.types.string.php

标签: php image file


【解决方案1】:

你想要做的是 url 重写。如果您使用的是 Apache,请使用 apache 扩展 mod_rewrite。如果您使用的是 nginx,请使用 rewrite。

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

您应该将header('Content-Type: image/.$extension.'); 更改为header('Content-Type: image/'.$extension);

【讨论】:

  • 谢谢,我不知道是否可以更改托管服务器上的 apache 文件,但我会找到的。顺便说一句,php parse 接受我写的字符串 header('Content-Type: image/.$extension.');图片显示在 标签中。见php.net/manual/en/language.types.string.php
  • 单引号中的变量不会被解析。
  • 是的,他们会这样做,请不要大喊大叫。自己测试代码。
  • 阅读php手册中的注释。 Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
  • 你实际上告诉浏览器的是Content-Type: image/.$extension.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 2014-01-10
  • 2011-01-03
  • 2013-12-14
  • 1970-01-01
  • 2017-12-26
相关资源
最近更新 更多