Facebook将参数传递给JPG文件和自定义图像显示 [英] Facebook Passing Argument to JPG Files and Custom Image Display
问题描述
我在 Facebook 的源代码中注意到,该图像是指向 PHP 文件的链接, safe_image.php
(或 rsrc.php
;它会随时更改),并将选定文件的名称追加到最后,例如:
https://external-lax3-2.xx.fbcdn.net/safe_image.php?imagename1234
或有时它们是通常的 JPEG 文件,并在结尾添加了一个随机标记:
我想知道他们是如何在自己的网站上获得自定义图片查看器的。通常,它只是一个白色背景,所选图像位于左上角。然而,它们的背景是灰色背景。
不仅如此,链接的图像还是直接返回到查看的 PHP
strong>文件;这怎么可能,他们是怎么做到的?干杯。
编辑:我也注意到,如果您将 img
src
更改为无效链接,则会显示错误到页面:
图片插入图片链接无法显示,因为它包含错误。
Jpeg文件,没有人惊讶,不需要参数。但是,PHP呢。所以,Facebook最可能做的是使用 rewrite
规则将其< .jpg
文件扩展名,从PHP加载数据,并使图像在浏览器中正常显示。
这可以通过 PHP
和 .htaccess
来实现。首先,让我们在我们想要的 / img文件夹内设置
里面: .htaccess
。 jpg?=
选项+ FollowSymLinks
RewriteEngine On
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^ img.jpg(。*)$ imageBackground.php $ 1 [NC]
img.jpg?image = bill
映射到 imageBackground.php?image = bill
。
imageBackground。 PHP
。我写了一个非常简单的,但基本上你所做的一切就是设置标题(你正在使用的图像格式)以及显示图像数据。显然,在实际应用中,这可能会更复杂一些,比如你可能会从数据库中动态获取图像数据(比如 Facebook )。 <$ p $
<?php
if($ _GET ['image'] ==bill){
header(Content-type:image / pjpeg );
echo file_get_contents(bill.jpg);
}
?>
I've noticed in Facebook's source code, that images are links to a PHP file, safe_image.php
(or rsrc.php
; it changes every now and then), with the name of the selected file appended to the end, such as:
https://external-lax3-2.xx.fbcdn.net/safe_image.php?imagename1234
Or sometimes they're the usual JPEG files with a random token appended to the end:
What, I'm wondering is how they get a custom image viewer on their website. Usually, it's just a white background, with the selected image in the top left corner. However, they have it set in the middle with a grey-ish background.
Not only that, the linked image is direct back to the viewed PHP file; how is this possible, and how do they do it?
Cheers.
EDIT: I've also noticed if you change the img
src
to an invalid link, it will print an error to the page:
The image " Insert image link here " cannot be displayed because it contains errors.
Jpeg files, to no ones surprise, do not take in arguments. However, PHP does. So, what is most likely Facebook did is use a rewrite
rule to 'map' their .jpg?=
URL to a PHP file, which can process the arguments. That PHP file then fetches the image data from a MYSQL (like) table. If you're wondering, yes you can have the .jpg
file extension display in the URL, load data from PHP, and have the image display properly in browser.
This can be achieved via PHP
and .htaccess
.
Firstly, let's setup our .htaccess
inside whatever folder we want to have our /img.jpg?=
inside of:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^img.jpg(.*)$ imageBackground.php$1 [NC]
Yay, now we are mapping our img.jpg?image=bill
to imageBackground.php?image=bill
.
And then we are going to write our imageBackground.php
. I wrote a very simple one, but basically all your doing is setting up headers (for which image format you're using) as well as display the image data. Obviously in practical application this would be more complicated, like maybe you're dynamically grabbing image data from a database (like Facebook).
<?php
if ($_GET['image'] == "bill") {
header("Content-type: image/pjpeg");
echo file_get_contents("bill.jpg");
}
?>
这篇关于Facebook将参数传递给JPG文件和自定义图像显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!