简体   繁体   English

从页面上的目录加载随机图像,而没有列出文件名的数组

[英]Load Random Image From Directory on Page Load Without a Listed Array of File Names

I've done some looking around on the site and every time I pull up a solution to this problem, one of the requirements is to have a naming convention and a list of every image to pull from the directory (example: image1.jpg, image2.jpg, etc.) All of the file names are different and there are thousands of them to pick from (so listing each one as a random opportunity in an array is not going to work). 我已经在网站上四处张望,每次提出该问题的解决方案时,要求之一就是要有一个命名约定和要从目录中提取的每个图像的列表(例如:image1.jpg, image2.jpg等)。所有文件名都不相同,并且有数千个文件名可供选择(因此,将每个文件作为随机机会列出在数组中是行不通的)。

I typically use CMS services and I'm writing this webpage from scratch in Notepad in an attempt to better my coding skills... and I'm not sure where to begin. 我通常使用CMS服务,并且正在用记事本从头开始编写此网页,以尝试提高我的编码技能...但我不确定从哪里开始。 I'm decent with HTML and CSS, but j Query and JavaScript are not my friends haha. 我对HTML和CSS很好,但是j Query和JavaScript不是我的朋友哈哈。

Thank you for any help! 感谢您的任何帮助! (Even if it's just pointing me to a tutorial or a solution I could not find!!!) (即使只是将我引向我找不到的教程或解决方案!!!)

Are all file names image1 image2 image3 etc? 是否所有文件名image1 image2 image3等? Then you could try to generate a random number, create a new img element and have it's source pointing to image+randomnumber.jpg and append it to the DOM 然后,您可以尝试生成一个随机数,创建一个新的img元素,并使其源指向image + randomnumber.jpg并将其附加到DOM

One of the main problems your facing here is about your thinking when it comes to how content is delivered, in a standalone static website you do not have access to the file system. 您在此处面临的主要问题之一是在内容传递方式方面的思考,在独立的静态网站中,您无权访问文件系统。 This means that if we want to query things outside of the browsers context we are not allowed, obviously without being able to access directories we can not generate a list of file names which can be loaded. 这意味着,如果我们想在浏览器上下文之外查询内容,显然是不允许访问的,我们无法生成可以加载的文件名列表。

If your wondering why we can't access the file system directly from say the JavaScript it's because of the sandbox that most modern browsers live in, otherwise people could attack your native directories from the front end languages. 如果您想知道为什么我们不能直接从JavaScript访问文件系统,那是因为大多数现代浏览器都位于沙箱中,否则人们可能会从前端语言攻击您的本机目录。 Your question is interesting as electron removes this sandboxing in a sophisticated esk manner, which is necessary as it's used for building desktop apps with chromium. 您的问题很有趣,因为electron以复杂的esk方式消除了此沙箱,这是必需的,因为它用于用铬构建桌面应用程序。

These days the most obvious solution would be to use some form of back end language and to create a web server that has direct access to the native directories around it. 如今,最明显的解决方案是使用某种形式的后端语言并创建可直接访问其周围本机目录的Web服务器。 Node, PhP, GoLang and many other populatr backend languages can parse a directory of files and then interpolate those into the frontend code which is the most common method. Node,PhP,GoLang和许多其他流行的后端语言可以解析文件目录,然后将其插值到最常见的前端代码中。

The other popular method at the moment is to create API's which is just a fancy web server with a queryable end point that then executes code against our web server and provides back a list of such items. 目前,另一种流行的方法是创建API,它只是一个带有可查询端点的精美Web服务器,然后针对我们的Web服务器执行代码并提供此类项目的列表。 You could then for instance take the items and then print those out using javascript . 例如,您可以拿走这些物品,然后使用javascript打印出来。

Reference directories method in php: http://php.net/manual/en/ref.dir.php php中的参考目录方法: http//php.net/manual/en/ref.dir.php

List contents of directory in nodejs: https://code-maven.com/list-content-of-directory-with-nodejs 列出nodejs中目录的内容: https ://code-maven.com/list-content-of-directory-with-nodejs

The best place to really start with the easiest route to understand more would be to start a backend language in either node or php, with php being the simpler of the two. 真正从最简单的途径开始理解的最好的地方是在节点或php中启动后端语言,而php是两者中最简单的一种。

https://www.w3schools.com/php/ https://www.w3schools.com/php/

First you need to get your file list from server side. 首先,您需要从服务器端获取文件列表。 then you can use a code like following: 那么您可以使用如下代码:

  var imageList = //your image list as an array of urls;
  var imageNumber = Math.random() * imageList.length; //gives you a random number in the range of imageList's size
  var imageToLoad = new Image();
  imageToLoad.addEventListener("load", function(){
      console.log( "image is loading" );
      $('#my-container').append(this); //in this case this will return image dom
  });
  imageToLoad.src = imageList[imageNumber];

this will add image to a container with id 'my-container' its just an example you can do anything you want using 'this' 这会将图像添加到ID为“ my-container”的容器中,这只是一个示例,您可以使用“ this”执行任何操作

So after much help and guidance from the community, I have figured out the answer! 因此,在社区的大量帮助和指导下,我已经找到了答案! To clarify my process in extreme detail, here is what I did to achieve the desired outcome: 为了非常详细地阐明我的过程,这是我为实现预期结果所做的工作:

  1. Create the page as a .php file instead of a .html file (in my case, index.php). 将页面创建为.php文件而不是.html文件(在我的情况下为index.php)。 If you are using notepad to create the file, make sure you change the file extension to .php, the encoding to UTF-8, and save file type as "All Files". 如果使用记事本创建文件,请确保将文件扩展名更改为.php,将编码更改为UTF-8,并将文件类型保存为“所有文件”。 As I understand it, PHP can pick the file at random but cannot pass this info to a static HTML page. 据我了解,PHP可以随机选择文件,但无法将此信息传递给静态HTML页面。
  2. Place this block of code into the webpage where the image should show. 将此代码块放入应该显示图像的网页中。 Currently, it is set up to reference a folder named, "images" out of the root directory (aka mysite.com/images/). 当前,它被设置为从根目录(aka mysite.com/images/)引用一个名为“ images”的文件夹。 This can be changed by modifying the text between the apostrophes after $imagesDir. 这可以通过修改$ imagesDir之后的撇号之间的文本来更改。 All other html markup on the page will work correctly if it is outside of the php code block. 如果页面上的所有其他HTML标记不在php代码块之内,则它将正常工作。

Code Block: 代码块:

<?php
   $imagesDir = 'images/';
   $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
   $randomImage = $images[array_rand($images)];
   echo "<img src='$randomImage'>";
?>

Thank you @bardizba for the code! 谢谢@bardizba的代码! Although there may be less resource intensive ways to write this, my situation was a bit different because the file names in the directory did not follow a naming convention and there was a mix of file types (jpg, gif, etc.) 尽管编写这种资源的方式可能较少,但是我的情况还是有些不同,因为目录中的文件名未遵循命名约定,并且文件类型混合在一起(jpg,gif等)。

Thanks again to everyone that helped me out! 再次感谢所有帮助我的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM