定制 nicoverbruggen/image-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nicoverbruggen/image-generator

Composer 安装命令:

composer require nicoverbruggen/image-generator

包简介

Generate placeholder images with a specific size, color and text (optional).

README 文档

README

This package is intended to be used for quickly generating placeholder images with a specific size, color and text. For more complex use cases, you may want to turn to something like stil/gd-text.

Requirements

  • PHP 8.1 or higher
  • GD extension

Usage

Make sure you require this package in your composer.json:

composer require nicoverbruggen/image-generator

See the example source file that is used to generate and save the sample images. You can generate the following examples:

The barebones example A multiline example An avatar

Please note that for testing purposes, I used Roboto Black as the TrueType font. (This font is not included in this repository.)

Here's a few examples of what you can do with this package:

Save images to a path

use NicoVerbruggen\ImageGenerator\ImageGenerator;

(new ImageGenerator())->generate(output: __DIR__ . "/image_example.png", size: '200x200');

Generate base64 encoded images inline

In addition to saving placeholder images to a particular location, you can also output a base64 encoded string:

use NicoVerbruggen\ImageGenerator\ImageGenerator;

$output = (new ImageGenerator())->generate(output: 'base64', size: '200x200');

echo "<img src='{$output}' alt='Placeholder image'>";

A useful use case may be achieved after declare your own helper, like so:

function placeholder_image(string $size = '500x500'): string {
    return (new ImageGenerator())->generate(output: 'base64', size: $size);
}

This use case can be useful when used in combination with frameworks like Laravel or Symfony:

<div>
    <h3>Item</h3>
    <img src="{{ placeholder_image('200x200') }}" alt="Placeholder">
</div>

Directly output images

You can also check out the other source file. You can point your browser directly at this file (assuming you're running a PHP server, of course) and it will directly return a file since the path is set to null.

Server mode

You can also point your PHP installation's webroot to the server directory, and generate images via URL.

  • The size parameter is used to size the placeholder images.
  • The background_color parameter is used to set the background color.
  • The text_color parameter is used to set the color of the text (of the dimensions).

Please note that you should not use # in the URL for the hexadecimal notation for colors!

You can then link to the domain you're using to host these placeholders.

For example, if it is image-generator.test:

<div>
    <h3>Item</h3>
    <img src="https://image-generator.test/?size=500x500&background_color=005577&text_color=FFF" alt="Placeholder">
</div>

Notes

If you do not supply a TrueType font path:

  • you will be limited in font size options (1 through 5)
  • you will not be able to render multiline text

Upgrade guide

v2 to v3:

In ImageGenerator, makePlaceholderImage() has been removed. You need to replace all usages of it with generate().

v3 to v4:

In ImageGenerator, generate()'s path parameter has been replaced with output. If you use named parameters, you will need to update your usage of this method.

It works the same way, but you have more options for this parameter now, in particular base64 is now a valid value for that parameter.

Tests

./vendor/bin/phpunit tests

Contributions

I am not planning to expand the features of this package at this time. If you've made an improvement or fixed something, you are free to send me a pull request.

License

MIT.

See also: LICENSE.

nicoverbruggen/image-generator 适用场景与选型建议

nicoverbruggen/image-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.06k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2017 年 12 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 nicoverbruggen/image-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nicoverbruggen/image-generator 我们能提供哪些服务?
定制开发 / 二次开发

基于 nicoverbruggen/image-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 35.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 31
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-06