<legend id='JJgQC'><style id='JJgQC'><dir id='JJgQC'><q id='JJgQC'></q></dir></style></legend>

    <small id='JJgQC'></small><noframes id='JJgQC'>

  • <tfoot id='JJgQC'></tfoot>

      1. <i id='JJgQC'><tr id='JJgQC'><dt id='JJgQC'><q id='JJgQC'><span id='JJgQC'><b id='JJgQC'><form id='JJgQC'><ins id='JJgQC'></ins><ul id='JJgQC'></ul><sub id='JJgQC'></sub></form><legend id='JJgQC'></legend><bdo id='JJgQC'><pre id='JJgQC'><center id='JJgQC'></center></pre></bdo></b><th id='JJgQC'></th></span></q></dt></tr></i><div id='JJgQC'><tfoot id='JJgQC'></tfoot><dl id='JJgQC'><fieldset id='JJgQC'></fieldset></dl></div>
          <bdo id='JJgQC'></bdo><ul id='JJgQC'></ul>
      2. php从目录中生成随机图像

        php generate random image from a directory(php从目录中生成随机图像)

        <small id='tPx1I'></small><noframes id='tPx1I'>

        • <bdo id='tPx1I'></bdo><ul id='tPx1I'></ul>
          1. <tfoot id='tPx1I'></tfoot>

                <tbody id='tPx1I'></tbody>

            • <i id='tPx1I'><tr id='tPx1I'><dt id='tPx1I'><q id='tPx1I'><span id='tPx1I'><b id='tPx1I'><form id='tPx1I'><ins id='tPx1I'></ins><ul id='tPx1I'></ul><sub id='tPx1I'></sub></form><legend id='tPx1I'></legend><bdo id='tPx1I'><pre id='tPx1I'><center id='tPx1I'></center></pre></bdo></b><th id='tPx1I'></th></span></q></dt></tr></i><div id='tPx1I'><tfoot id='tPx1I'></tfoot><dl id='tPx1I'><fieldset id='tPx1I'></fieldset></dl></div>
                  <legend id='tPx1I'><style id='tPx1I'><dir id='tPx1I'><q id='tPx1I'></q></dir></style></legend>
                  本文介绍了php从目录中生成随机图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我必须从目录中生成随机图像.我知道哪个很简单,

                  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?

                  谢谢.

                  推荐答案

                  array_rand 的一个替代方案是 shuffle:

                  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);
                  

                  (我故意重复 glob() 以便在第二次迭代时发现任何新图像.)

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

                  附言我假设您明白,默认情况下,PHP 是无状态的.如果这应该在每次访问页面时为每个用户提供不同的图像(例如旋转横幅广告),那么代码几乎相同,但将 $images 数组移动到 $_SESSION.

                  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从目录中生成随机图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <bdo id='wbZa2'></bdo><ul id='wbZa2'></ul>

                    • <small id='wbZa2'></small><noframes id='wbZa2'>

                        <tbody id='wbZa2'></tbody>
                      • <tfoot id='wbZa2'></tfoot>
                            <legend id='wbZa2'><style id='wbZa2'><dir id='wbZa2'><q id='wbZa2'></q></dir></style></legend>

                          1. <i id='wbZa2'><tr id='wbZa2'><dt id='wbZa2'><q id='wbZa2'><span id='wbZa2'><b id='wbZa2'><form id='wbZa2'><ins id='wbZa2'></ins><ul id='wbZa2'></ul><sub id='wbZa2'></sub></form><legend id='wbZa2'></legend><bdo id='wbZa2'><pre id='wbZa2'><center id='wbZa2'></center></pre></bdo></b><th id='wbZa2'></th></span></q></dt></tr></i><div id='wbZa2'><tfoot id='wbZa2'></tfoot><dl id='wbZa2'><fieldset id='wbZa2'></fieldset></dl></div>