【问题标题】:Redirect to dynamic image hiding its path in php重定向到在php中隐藏其路径的动态图像
【发布时间】:2013-11-19 16:31:16
【问题描述】:

在 PHP 中,我有一个脚本,它接收 GET 变量,在数据库中找到一些值并将用户重定向到动态 (PNG) 图像。 以下是示例步骤:

用户调用地址“http://www.example.com/image/50

“.htaccess”文件中的 RewriteRule 将浏览器重定向到脚本“callImage.php?id=50”

脚本“callImage.php”的内容:

[...]
<?php
$id = $_GET["id"];

$a = pickFromDB("a"); // $a = "Hello"
$b = pickFromDB("b"); // $b = "World"

header("Location: dynamicImage.php?a=".$a."&b=".$b); ?>

结果:用户被重定向到脚本“dynamicImage.php”,该脚本通过 $_GET 数组获取两个变量并生成 PNG 图像。 唯一的问题是,在这些步骤之后,用户将在其浏览器的地址栏中看到“dynamicImage”脚本的完整地址:

http://example.com/dynamicImage.php?a=Hello&b=World

.. 虽然我想隐藏最后一个脚本的地址,但保持显示原来的“友好”地址:

http://www.example.com/image/50

有可能吗? 谢谢!

编辑 我更新了“header()”语句......它缺少关键字“Location:......”:D

更新 2 我也试过,在“callImage.php”脚本中:

[...]

    header('Content-type: image/png');
    $url = "http://example.com/dynamicImage.php?a=Hello&b=World";
    $img = imagecreatefrompng($url);
    imagepng($img);
    imagedestroy($img);

...但浏览器无法显示它,因为“它包含错误”。我确定文件格式是 PNG,因为在“dynamicImage.php”脚本中,我用来生成图像的语句是“imagepng()”。 奇怪的是,如果我把$url的完整地址放在地址栏中按回车,浏览器就可以正确显示图片了!!怎么了? PHP 是在开玩笑!

更新 3

好的,我注意到“UPDATE 2”代码运行良好。它以前不起作用,因为在我的 $url 中有一个空格,所以即使浏览器接受带有空格的 URL,“imagefrompng()”函数也没有,产生错误。

现在“callImage.php”脚本自己生成图像,所以在地址栏中有 http://example.com/image/50

谢谢大家!

【问题讨论】:

标签: php .htaccess redirect


【解决方案1】:

我正在为我的一个网站上的缩略图做类似的事情。基本前提是使用 htaccess 重写 URL,然后 PHP 使用 curl 抓取正确的图像并将其发送到浏览器 - 您需要发送正确的内容类型,在我的情况下始终是 PNG,但您应该能够如果您使用可变图像类型,则检测并发送正确的值。

尝试更新您的页面/变量名称,请原谅任何错误。这应该会导致http://example.com/image/50 保留在地址栏中,并根据动态查询显示正确的图像。只要服务器可以加载 URL,就适用于远程和本地图像。

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  RewriteRule ^image/(\d*)$ /callImage.php?id=$1 [L,QSA]
</IfModule>

callImage.php

// Do your database calls, etc and create the image URL
$image_url = "http://example.com/dynamicImage.php?a={$a}&b={$b}";

// Use curl to fetch and resend the image
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $image_url);     // Set the URL
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);   // Short timeout (local)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   // Return the output (Default on PHP 5.1.3+)
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);   // Fetch the image as binary data

// Fetch the image
$image_string = curl_exec($ch);                // Even though it's binary it's still a "string"

curl_close($ch);                               // Close connection

$image = imagecreatefromstring($image_string); // Create an image object

header("Content-type: image/png");             // Set the content type (or you will get ascii)
imagepng($image);                              // Or imagejpeg(), imagegif(), etc
imagedestroy($image);                          // Free up the memory used by the image
die();                                         // All done

【讨论】:

  • 嗯...我想这是我第一次听说“CURL”。 :|但是我尝试了代码。第一次,浏览器无法显示图像,因为“它包含错误”。第二次,我删除了最后一条语句(用于生成图像输出)并收到错误消息:“警告:imagecreatefromstring():
  • cURL 是一个用于从 URL 传输内容的库 - 基本上是一种命令行 Web 浏览器,PHP 有一个您可以使用的实现。见en.wikipedia.org/wiki/CURLphp.net/manual/en/book.curl.php
  • 对不起,我刚刚按了 ENTER 提交评论时出错。请也看看我对顶帖的最后编辑。
  • 尝试将$img 的内容写入error_log() 以确保它能够正确加载 - 您可能在服务器从自身加载图像时遇到问题。确保您能够在 PHP 中打开远程资源:php.net/manual/en/…
  • 确保dynamicImage.php 也在发送正确的标头。我没有使用您所使用的确切方法,因此很难确定,但如果没有实际链接,很难说出问题所在。
猜你喜欢
  • 2014-09-19
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
相关资源
最近更新 更多