随机图像的URL [英] URL to a random image

查看:364
本文介绍了随机图像的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些我在网络开发中没有看过的东西,但似乎有可能。 我想要一个可以放入任意HTML < IMG /> 标记或CSS background-image 属性,当访问时,随机图像。我不希望页面指向或重定向到随机图像,我不希望它显示或者嵌入随机图像,因为这些东西不能满足上述要求。我已经看到了使用Javascript交换属性或隐藏元素在网页中显示随机图像的解决方案,但这必须使整个页面成为图像,没有前面或后面的二进制或ASCII数据。我希望那些对编程一无所知的人可以使用它。我的目标是这样的:

I want to make something that I've not seen done in web development, but it seems possible. I want a URL that can be put into any arbitrary HTML <IMG/> tag or CSS background-image property that, when accessed, is a random image. I don't want the page to point or redirect to a random image, and I don't want it to display or embed a random image, as these things won't work with the above-stated requirements. I've seen solutions for displaying a random image in a webpage using Javascript to swap a property or hide an element, but this has to make the entire page the image, with no preceding or succeeding binary or ASCII data. I want it to be usable by people who don't know anything about programming. What I'm aiming for is something like this:

http://example.com/randomImage/

理论上,每次在CSS或HTML中使用此URL加载页面时,随机图像都会显示在引用元素。它应该有效,如果你有几个< IMG /> 元素或几个元素及其 background-image 引用这个页面时,它们都应该随机显示不同的图像。

In theory, every time a page is loaded with this URL in the CSS or HTML, a random image will be displayed in the referenced element. It should work so that if you have several <IMG/> elements or several elements with their background-image referencing this page, they should all show randomly different images.

我的第一个想法是使用PHP(在下面描述的结构中)查看特定的目录,选择一个随机的图像文件,并回显其二进制数据。 如果可以,那我该怎么做呢?如果没有,那么有没有人有任何其他想法?

My first thought is to use PHP (in the structure described below) to look into a particular directory, pick an image file at random, and echo its binary data. If this is possible, then how would I go about doing it? If not, then does anyone have any other ideas?

我的主要想法是伪代码:

My primary idea as pseudocode:

<?PHP
$chosenImage = getRandomFileFrom("./rand/");
$bytes = readFileAsBinary($chosenImage);
foreach($bytes as $byte){
    echo $byte;
}
?>

我不关心真或伪随机。它可以每次循环我所关心的一切;我在这方面所需要的只是多个图像可以作为单个URL引用

推荐答案

你当然可以做这与PHP。

You certainly could do this with PHP.

首先,选择一个随机文件(参见从目录中选择随机文件

First, choose a random file (see Select random file from directory )

function random_pic($dir = 'imagedir') {
    $files = glob($dir . '/*.jpg');
    if (!$files) return false;

    $file = array_rand($files);
    return $files[$file];
}

然后你可以使用PHP readfile 将其发送给用户的功能。您可以在此处阅读: http://www.php.net/readfile ,请参阅示例1 ,我在下面稍微修改过:

Then you can use the PHP readfile function to send it to the user. You can read up on it here: http://www.php.net/readfile see Example 1, which I've modified slightly below:

function outputImage( $filename ) {
    header('Content-Description: File Transfer');
    header("Content-type: image/jpeg");
    header('Content-Disposition: attachment; filename='.basename($filename ));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename ));
    ob_clean();
    flush();
    readfile($filename );
    exit;
}

然后将它们全部拉出来:

Then pull it all together:

// Get a filename
$filename = random_pic();

// Check that a file was found
if (!$filename) {
    header('HTTP/1.0 404 Not Found');
    die();
}

// Output the image
outputImage( $filename );

您还可以更改 $ files = glob($ dir。'/ * .jpg'); 查找不同的图像格式,但您还必须更改 outputImage 以检测格式并设置内容类型键入标题为 image / jpeg image / png 等等......

You could also change the $files = glob($dir . '/*.jpg'); to look for different image formats, but you would also have to change outputImage to detect the format and set the Content-type type header to image/jpeg or image/png etc...

如果您只提供一种类型的图像,您可以随时设置Apache来处理对'randomImage.jpg'的请求到PHP脚本,以及那么客户端浏览器绝对不会更聪明。有关详细信息,请查看如何将所有请求重定向到文件,除了使用Apache的图像?(虽然你当然会做相反的事情)同时查看Apache重写规则: http://httpd.apache.org/docs/current/rewrite/flags.html

If you're only serving one type of image, you could always setup Apache to handle requests to 'randomImage.jpg' to a PHP script, and then the client browser would be absolutely none the wiser. For more info check out How to redirect all requests to a file, except images using Apache? (although you would of course be doing the opposite) Also check out Apache rewrite rules: http://httpd.apache.org/docs/current/rewrite/flags.html

I尚未测试此代码,但它应该没有太多的努力。希望有所帮助!

I haven't tested this code, but it should work without too much effort. Hope that helps!

这篇关于随机图像的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆