aland20/image-layer-builder

一个简单的包,用于向背景添加图片和文本

0.1.0 2023-06-30 23:42 UTC

This package is auto-updated.

Last update: 2024-09-30 01:35:50 UTC


README

Latest Version on Packagist Total Downloads

此包允许将小图片合并到背景中,同时还可以在背景中添加文本。在底层,该包使用 Intervention/v2/Image 来执行生成。

安装

您可以通过 composer 安装此包

composer require aland20/image-layer-builder
  • 发布配置文件
php artisan vendor:publish --provider="Aland20\ImageLayerBuilder\ImageLayerBuilderServiceProvider"

用法

  • 注意:如果您不设置自定义字体,您可能无法使用字体大小或其他字体属性。

  • 有关更多信息,请查看 Intervention/v2/Image::text

  • 您可能希望根据需要自定义背景和图像调整大小。

use Illuminate\Http\Request;
use Aland20\ImageLayerBuilder\ImageLayerBuilder;


class ImageController extends Controller
{

    public function generate(Request $request)
    {
        $avatar = $request->file('avatar');
        $logo = $request->file('logo');
        $text = $request->text;

        $output = ImageLayerBuilder::make()
            // Set the background, it must be a valid full file name in the bgDirPath. Also, you may dynamically set it via requests.
            ->setBackground('my-custom-bg.png')
            // You may set the width and height for the background to resize. Default is 1200x750
            ->resizeBackgroundWidth(1000)
            ->resizeBackgroundHeight(750)
            // Add an image for the avatar, you may want to set the width and height resize,  positions, X-axis, Y-axis and many more as well.
            ->addImage($avatar, widthResize: 125, heightResize: 125, border: [3, '#fff'])
            // You may add more than one image on top of the background and change the position
            ->addImage($logo, rounded: false, position: 'bottom-right')
            // Add Text to the background, you may give some offset to get into the range
            ->addText('Welcome', position: 'bottom-center', offsetY: 25, angle: 25)
            // Add Text to the background, position starts from center
            ->addText($text, 'center', offsetY: -150)
            // Generate the instance
            ->generate();
        
        // Save to file
        $filename = $output->saveToFile();

        // Return raw stream data in string
        $rawStream = $output->getRawStream();

        return response($rawStream)->header('Content-type', 'image/png');
    }
}
  • 您可能希望返回所有可用的背景以在前端显示
use Aland20\ImageLayerBuilder;

$backgrounds = ImageLayerBuilder::make()->getBackgrounds();

foreach($backgrounds as $background):
    
    echo $background['path']; // background image file with extension
    echo $background['name']; // background image file without extension

endforeach;

可用公共方法

   /**
     * Make a new instance statically, with default constructor values
     */
    public static function make(
        string $bgDirPath = '',
        string $outputPath = '',
        string $tempPath = ''
    ): static {}

  /**
   * Set background directory path to find all the available backgrounds.
   */
  public function setBgDirPath(string $bgDirPath): static {}

  /**
   * Set output directory path to store the output file.
   */
  public function setOutputPath(string $outputPath): static {}

  /**
   * Set temporary directory path to store temporary files.
   * This will be cleaned up after generation.
   */
  public function setTempPath(string $tempPath): static {}

  /**
   * Set the background width to resize.
   */
  public function resizeBackgroundWidth(int $width): static {}

  /**
   * Set the background heght to resize.
   */
  public function resizeBackgroundHeight(int $height): static {}

  /**
   * return all available background file names in the background types directory
   */
  public function getBackgrounds(): array {}

  /**
   * Set the background. This has to be an image that exists in the bigDirPath dir
   */
  public function setBackground(string $background): static {}

  /**
   * Set custom font path
   */
  public function setCustomFont(string $fontPath): static {}

  /**
   * Add an image on top of the background including X-axis and Y-axis positions from center
   * Available positions are: left, right, center, top, bottom, including top-left combinations
   * This can be called more than once.
   * Accepts the following data for the image image,
   * string - Path of the image in filesystem.
   * string - URL of an image (allow_url_fopen must be enabled).
   * string - Binary image data.
   * string - Data-URL encoded image data.
   * string - Base64 encoded image data.
   * resource - PHP resource of type gd. (when using GD driver)
   * object - Imagick instance (when using Imagick driver)
   * object - Intervention\Image\Image instance
   * object - SplFileInfo instance (To handle Laravel file uploads via Symfony\Component\HttpFoundation\File\UploadedFile)
   * The border is an array of [width, color]
   */
  public function addImage(
    mixed $image,
    int $widthResize = 175,
    int $heightResize = 175,
    bool $rounded = true,
    string $position = 'center',
    int $posX = 0,
    int $posY = 0
  ): static {}

  /**
   * Add given text with the options to the background image.
   * This method can be called more than once.
   */
  public function addText(
    string $text,
    string $position = 'top-center',
    int $offsetX = 0,
    int $offsetY = 0,
    int $fontSize = 32,
    string $color = '#fdf6e3',
    int $angle = 0
  ): static {

  /**
   * Generate background image
   */
  public function generate(): static {}

  /**
   * Exports the generated background image to a file with given format
   */
  public function saveToFile(?string $filename = ''): string

  /**
   * Return the background image stream content.
   */
  public function getOutputStream(): \GdImage {}

  /**
   * Return the background image stream content.
   */
  public function getRawStream(): string {}

变更日志

请参阅 CHANGELOG 以获取有关最近更改的更多信息。

贡献

有关详细信息,请参阅 CONTRIBUTING

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。

Laravel 包模板

此包是使用 Laravel 包模板 生成的。