【问题标题】:Show PHP page as an image content-type like Giphy.com images?将 PHP 页面显示为图像内容类型,如 Giphy.com 图像?
【发布时间】:2018-04-19 18:27:26
【问题描述】:
【问题讨论】:
标签:
php
image
image-processing
giphy
【解决方案1】:
如果您转到图像并查看源代码 (CTRL + U),您将看到它实际上是 HTML。他们使用 .htaccess 或路由将看似图像 URL (https://media.giphy.com/media/l2SqcWByj8h7w0TEk/giphy.gif) 的内容指向他们添加了链接和“元标记”的 HTML 文件。
您可以尝试这样的方法来获得类似的结果:
免责声明:此代码不安全,可能会被注入,因此您需要清理参数,检查文件请求是否实际上是图像文件等。
.htaccess
将所有对 yourdomain.com/media/an-image.gif 的请求定向到 media.php。
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteRule ^media/(.*)\.(gif|jpg|png)$ media.php?image=$1&ext=$2
media.php
编写一个 php 脚本来处理请求,检查图像是否存在等并输出 HTML。
if(is_file('/path/to/media/' . $_GET['image'] . '.' . $_GET['ext'])) {
?>
<html>
<head>
<title>IMAGE TITLE HERE</title>
<!-- other head tags -->
</head>
<body>
<div id="image_container">
<!-- You could load this from a database or from filename -->
<div class="title">IMAGE TITLE</div>
<!-- Quick way to load real image is to store the actual .gif in another file, say '/assets/' so you can load it without it being caught by .htaccess.
<img src="/assets/<?php echo $_GET['image'] . '.' . $_GET['ext'];?>"/>
<!-- Alternative way to to load image and output base64 encoded version (you would have to make a handler for each format -->
<img src="data:image/'<?php echo $_GET['ext'];?>';base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." "/>
<div class="link">
<a href="#linkhere">THIS IS THE IMAGE LINK</a>
</div>
</div>
</body>
</html>
<?php
exit();
}
}
// Redirect or return a 404 here.
header("HTTP/1.0 404 Not Found");
echo 'Not Found!';
exit();