根据GEO位置从不同目录中选择随机图像 [英] Picking a random image out of different directories based on GEO locations
问题描述
从目录中挑选随机图像的代码非常简单。
The code for picking an random image out of a directory is pretty straight forward.
例如;我当前的代码是:
For example; my current code is this:
<?php
$imagesDir = 'img/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)]; // See comments
?>
我想使用Cloudflare的GEO IP查找程序,因此,当用户访问网站时,它会在
I want to use Cloudflare's GEO IP finder, so when the user visits the website it feeds back where the users from.
所以我想说
if england use directory > img/en/
if australia use directory > img/au/
if USA use directory > img/usa/
if NZ use directory > img/nz/
if any other country > img/
我知道它的逻辑,但是将其放入代码中是我要做的另一件事
I know the logic to it, but putting it into code is another thing which i've been struggling to do.
有什么想法吗?
推荐答案
创建一个位置目录及其对应名称的数组,然后根据地理位置(在我的示例中为 $ location
)构建图像目录。
Create an array of the location directories and their corresponding names then build your image directory based on the geo location ($location
in my example).
$location = 'australia';
$dirs = array('england' => 'en',
'australia' => 'au',
'USA' => 'usa',
'NZ' => 'nz');
$imagesDir = 'img/' . (isset($dirs[$location]) ? $dirs[$location] . '/' : '');
如果在数组中未找到位置,则 $ imagesDir的设置
变量将默认为 img /
。
If a location is not found in the array the setting of the $imagesDir
variable will default to img/
as it is now.
这篇关于根据GEO位置从不同目录中选择随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!