pjam/prig

PHP 随机图像生成器

v1.0.0 2021-10-19 21:46 UTC

This package is auto-updated.

Last update: 2024-09-22 03:34:14 UTC


README

prig

这个项目只是出于纯粹的求知欲,想看看用 PHP 生成带有随机颜色的像素图像会是什么样子。

第一次尝试成功后,我决定把它托管在一个地方,以便可以轻松地将这些图像用作其他项目的占位符图像,只需在图像标签的源属性中设置 URL 即可。

既然我已经在 git 仓库中保存了我的代码,为什么不把它做成一个可下载的包,以便可以在本地使用,而无需外部请求呢?

因此,为了与大家分享,我公开发布了它。我希望它对任何人都有所帮助。

使用方法

使用 HTML 图像 src

您只需将图像标签的 src 属性指向当前托管该图像的网站(https://polar-mountain-75010.herokuapp.com/image.php

<img src="https://polar-mountain-75010.herokuapp.com/image.php">

安装包并在您的 PHP 代码中创建图像

目前,直到包发布到 Packagist 为止,您可以使用 git 安装它。

要求

  • PHP 8.0+(如果您需要与较低版本兼容,我可以扩展代码以允许它,只需请求即可)
<?php

use pjam\prig\ImageGenerator\ImageGenerator;

require_once '../vendor/autoload.php';

$imageGenerator = new ImageGenerator($options);

// returns a GdImage object that you can use for something else
$imageObject = $imageGenerator->createRandomImage(); 

// outputs the image directly to the browser, with the right header already set
$imageGenerator->outputImage($imageObject);

// returns the base64 encoded string of the image that can be used also in the src attribute of an image tag
// or saved to database, for example
$imageGenerator->getEncodedImage($imageObject);

选项

您可以通过在图像标签源属性的查询字符串中添加它们或将它们作为传递给类构造函数的选项数组中的元素来传递您想要的选项。

任何无效值将被忽略,并使用默认值作为回退。

height(int,默认:200,最小:1,最大:2000)

图像的高度,以像素为单位。

示例

<img src="https://polar-mountain-75010.herokuapp.com/image.php?height=600">
$imageGenerator = new ImageGenerator(['height' => 20]);

width(int,默认:200,最小:1,最大:2000)

图像的宽度,以像素为单位。

示例

<img src="https://polar-mountain-75010.herokuapp.com/image.php?width=800">
$imageGenerator = new ImageGenerator(['width' => 1800]);

type(string,默认:'png',接受值:'png','jpg|jpeg','webp')

图像的类型和/或输出格式。

示例

<img src="https://polar-mountain-75010.herokuapp.com/image.php?type=jpeg">
$imageGenerator = new ImageGenerator(['type' => 'webp']);

numColors(?int,默认:null,最小:1,最大:256)

要用于图像的不同颜色的数量。如果没有提供,它将尝试为每个像素设置不同的颜色

示例

<img src="https://polar-mountain-75010.herokuapp.com/image.php?numColors=2">
$imageGenerator = new ImageGenerator(['numColors' => 50]);

许可证

prig 在 MIT 许可证下发布。有关详细信息,请参阅附带许可证文件。

贡献

欢迎任何贡献。只需提交一个拉取请求。