php从目录生成随机映像 [英] php generate random image from a directory

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

问题描述

我必须从目录中生成随机图像。我知道这是简单的,

  $ dire =images /; 
$ images = glob($ dire。'*。{jpg,jpeg,png,gif}',GLOB_BRACE);
$ randomImage = $ images [array_rand($ images)];
< input type =imagesrc =<?= $ randomImage;?> ALT = < = $ randomImage;?> 中/>

但是,我必须确保该目录中的每个图像至少选择一次,然后再生成第二次随机。上述代码只会显示任何随机图像。



我的想法是,我必须将随机图像存储在数组中,并且每次使用新创建的随机图像检查数组。如果新的随机图像不在该阵列中,我需要显示该图像,否则我必须找到另一个图像。



我以上所述创建了以下代码。

  $ allimgs = array(); 
$ dire =images /;
$ images = glob($ dire。'*。{jpg,jpeg,png,gif}',GLOB_BRACE);
$ randomImage = $ images [array_rand($ images)];

if(!in_array($ randomImage,$ allimgs))
{
$ allimgs [] = $ randomImage;
< input type =imagesrc =<?= $ randomImage;?> ALT = < = $ randomImage;?> 中/>
}

但是我仍然坚持使用这段代码。任何人请帮忙改进这个代码?或任何其他想法?



谢谢。

解决方案

array_rand是shuffle:

  $ dire =images /; 
$ images = glob($ dire。'*。{jpg,jpeg,png,gif}',GLOB_BRACE);
shuffle($ images);

然后显示下一个随机图片:

  $ randomImage = array_pop($图像); 

当数组为空时,再次调用初始化代码。所以把它放在一起:

  $ images = array()//在脚本$初始化一次
$ dire = 图片/;
...
if(count($ images)== 0){
$ images = glob($ dire。'*。{jpg,jpeg,png,gif}',GLOB_BRACE );
shuffle($ images);
}
$ randomImage = array_pop($ images);

(我故意重复了glob(),以便在第二次迭代中发现任何新图像。



PS我假设你明白,默认情况下,PHP是无状态的。如果这是每次访问页面(例如旋转横幅广告)时给每个用户一个不同的图像,则代码几乎相同,但移动 $ images 数组到 $ _ SESSION


I have to generate random image from a directory. I know which is simple like,

   $dire="images/";
   $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
   $randomImage = $images[array_rand($images)];
   <input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />

But I have to make sure that each image from that directory picked at least one time before generating second time randomly. The above code only will display only any random image.

My thought is, I have to store the random image in an array and check the array every time with newly created random image. If the new random image is not in that array, I need to display that image,else I have to find another image.

I created the below code with above thought.

  $allimgs=array();
  $dire="images/";
  $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
  $randomImage = $images[array_rand($images)];

   if(!in_array($randomImage,$allimgs))
   {
     $allimgs[]=$randomImage;
     <input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />
   }

But I am still stuck with this code. Anyone please help to improve this code? or any other idea?

Thanks.

解决方案

One alternative to array_rand is shuffle:

$dire="images/";
$images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
shuffle($images);

And then to display the next random image:

$randomImage=array_pop($images);

When the array is empty you call the initialization code again. So putting it together:

$images=array()  //Initialize once at top of script
$dire="images/";
...
if(count($images)==0){
  $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
  shuffle($images);
  }
$randomImage=array_pop($images);

(I deliberately repeated the glob() so that any new images get discovered on the second iteration.)

P.S. I'm assuming you understand that, by default, PHP is stateless. If this is supposed to give each user a different image each time they visit a page (e.g. a rotating banner ad), then the code is almost the same, but move the $images array into $_SESSION.

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

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