根据大小从文件夹中抓取图像

发布于 2024-07-22 17:41:11 字数 396 浏览 1 评论 0原文

好的,我有一个在使用此 answer 之后创建的文件夹,所以

现在我有一个文件夹,里面有很多文件夹 每个都有图像和 .txt 文件,

图像几乎都是 .jpg

所以现在我想使用 php 显示每个文件夹中的图像 我想显示每个文件夹中的第一张图像,如果没有图像,我想显示默认图像,

因此只要知道我想显示图像的文件夹的位置,

有什么想法吗? 可以通过 php 或 javascript,请不要使用其他语言 它将不合时宜地用在 WordPress 主题中,文件夹位置是自定义字段参数

ok so i have a folder that was created after this answer was used

so now i have a folder with with many folders in it
each one having images and a .txt file

the images are almost all .jpg

so now i want to display a image from each folder, using php
i want to display the first image from each folder, and if there is no images i want to display a default image

so by just have the location of the folder i want to display the image

any ideas?
it can be through php or javascript, no other languages please
it will untimely be used in a wordpress theme with the folder location being a custom field parameter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习惯成性 2024-07-29 17:41:11

尝试使用 PHP glob - 它会返回您提供的任何文件夹中的文件名数组。 您可以按 *.jpg 进行过滤,并仅使用数组中的第一项。

例如,我使用:

$imageArray = glob($folder . "/image*.jpg");

它在文件夹 $folder 中创建一个名为 image[something].jpg 的所有文件名的数组。

Try using PHP glob - it returns an array of filenames in any folder you give it. You can filter by *.jpg, and just use the first item in the array.

For example, I use:

$imageArray = glob($folder . "/image*.jpg");

which makes an array of all filenames called image[something].jpg in the folder $folder.

夢归不見 2024-07-29 17:41:11
<?php

$record_ratio = 0;
foreach (glob("*.jpg") as $filename) {
    $info = getimagesize($filename);
    $ratio = $info[0] / $info[1];
    if (abs(1 - $ratio) < abs(1 - $record_ratio)) {
      $record_ratio = $ratio;
      $record_filename = $filename;
    }
    if (record_ratio == 1) break;
}
if ($record_ratio > 0) {
  echo '<img src="'.$record_filename.'" height=150px><br>';
}

?>

<?php
foreach (glob("*.txt") as $filename) {
        echo nl2br(file_get_contents($filename));
        echo "<br></br>";
}
?>
<?php

$record_ratio = 0;
foreach (glob("*.jpg") as $filename) {
    $info = getimagesize($filename);
    $ratio = $info[0] / $info[1];
    if (abs(1 - $ratio) < abs(1 - $record_ratio)) {
      $record_ratio = $ratio;
      $record_filename = $filename;
    }
    if (record_ratio == 1) break;
}
if ($record_ratio > 0) {
  echo '<img src="'.$record_filename.'" height=150px><br>';
}

?>

<?php
foreach (glob("*.txt") as $filename) {
        echo nl2br(file_get_contents($filename));
        echo "<br></br>";
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文