bradietilley/faker-imagez

提供假图像的基础系统

v1.1.0 2024-04-13 10:07 UTC

This package is auto-updated.

Last update: 2024-09-13 11:22:42 UTC


README

Static Analysis Tests

介绍

FakerImagez 为其他假图像生成器提供接口。

仓库

Imagez 可用

文档

使用起来非常简单。

定义您的 Imagez 类,或使用上面之一 ^

class Something extends Imagez
{
    public static function basePath(): string
    {
        return '/path/to/images';
    }
}

如果您正在使用上述提到的软件包,只需在下面的示例中将 imagez() 替换为软件包名称,例如 foodz()catz()

获取随机路径

每次调用都会返回一个新的随机路径。

imagez()->path();                             // string: /path/to/pics/image_0037.jpg
imagez()->path();                             // string: /path/to/pics/image_0101.jpg

一旦所有图像都已耗尽,它将重新加载并再次以另一个随机顺序继续。

获取随机内容

每次调用都会返回一个新的随机文件的全部内容。

imagez()->contents();                         // string: <contents of /path/to/pics/image_0087.jpg>
imagez()->contents();                         // string: <contents of /path/to/pics/image_0120.jpg>

获取 SplFileInfo 对象

每次调用都会返回一个新随机文件,作为 SplFileInfo 的实例。

imagez()->fileinfo();                         // \SplInfo: <fileinfo of /path/to/pics/image_0042.jpg>
imagez()->fileinfo();                         // \SplInfo: <fileinfo of /path/to/pics/image_0099.jpg>

获取精确图像

如果您有喜欢的图像,请使用 get 方法获取特定的图像。这将 不会 从池中删除图像,它只会检索给定的图像。

imagez()->get(24);                            // string: /path/to/pics/image_0024.jpg
imagez()->get(43);                            // string: /path/to/pics/image_0043.jpg

获取图像数量

如果您想确定可用图像的数量,请使用 count 方法。这将 不会 统计池中的图像,而是所有图像。

$min = 1;
$max = catz()->count();

$image = catz()->get(mt_rand($min, $max));  // string: /path/to/pics/image_XXXX.jpg

停止迭代以进行重复交互

有时您可能想手动迭代池并执行多个查询以获取路径、内容或 SplFileInfo,而不自动迭代。您可以使用以下方法实现此目的。

imagez()->iterate();                          // Iterates to the next image

imagez()->getCurrentImagePath();              // string: /path/to/pics/image_0046.jpeg                 (won't iterate)
imagez()->getCurrentImagePath();              // string: /path/to/pics/image_0046.jpeg                 (won't iterate)
imagez()->getCurrentImageContents();          // string: <contents of /path/to/pics/image_0046.jpeg>   (won't iterate)
imagez()->getCurrentImageFileinfo();          // \SplFileInfo: /path/to/pics/image_0046.jpeg           (won't iterate)

获取所有图像

也许您想获取所有样本图像并执行一些自定义操作。很容易。 all 方法将返回所有图像(路径),pool 方法将返回池中剩余的内容(路径)。

imagez()->all();                              // array: <path1, path2, ..., path118, path119, path120>

imagez()->path();                             // this iterates the pool, popping the last path from the pool
imagez()->pool();                             // array: <path1, path2, ..., path118, path119>         (pool contains one less now)

imagez()->path();                             // this iterates the pool, popping the last path from the pool
imagez()->pool();                             // array: <path1, path2, ..., path118>                  (pool contains one less now)

池重新加载

这些是内部函数,但也是公开的。如果您正在执行自定义操作,它们可能很有用。

foreach (range(1, 100) as $i) {
    imagez()->path();                         // iterates 100 images
}

imagez()->loadWhenEmpty();                    // Won't do anything here as there's still images in the pool.
imagez()->load();                             // Will reload the pool of images to be the full collection of image images. 

替换所有假图像

您还可以使用 replaceAll() 方法替换假图像中的所有图像。

$all = imagez()->all();
unset($all[23452]);                           // Say you don't like this one

imagez()->replaceAll($all);

imagez()->path();                             // Will never include image #23452

组合生成器

也许您喜欢猫、狗、食物,并希望您的占位符/头像包含多个来源。这很容易实现。

$generator = catz()->combine(dogz(), foodz());
// or
$generator = CombineImagez::make(catz(), dogz(), foodz());

// Then interact with it as you otherwise would:
$generator->path();                           // path to a dog image
$generator->path();                           // path to a cat image
$generator->path();                           // path to a food image

路线图

  • 可能添加自定义过滤,例如 imagez()->red()->path()imagez()->red()->iterate()->getCurrentImagePath()
  • 可能添加图像干预作为可选依赖项以进行缩放: imagez()->resize(128)->path()
  • 添加更多图像和软件包

作者