wearejust/laravel-postergenerator

此包已被废弃,不再维护。未建议任何替代包。

dev-master 2016-12-14 15:53 UTC

This package is auto-updated.

Last update: 2023-03-19 22:19:47 UTC


README

注意:该包仍在开发中,请自行决定是否使用

此类集合将使您能够使用 PhantomJS 快速生成 海报。它使开发者能够轻松地将海报实现为 HTML/CSS/JavaScript,并让 PhantomJS 将其捕获为图像或 PDF。

安装

首先,请在您的 composer.json 文件中添加以下内容

  "scripts": {
    "post-install-cmd": [
      "PhantomInstaller\\Installer::installPhantomJS"
    ],
    "post-update-cmd": [
      "PhantomInstaller\\Installer::installPhantomJS"
    ]
  },

然后在项目中引入 postergenerator

composer require wearejust/laravel-postergenerator
// app.php

  ...
  Just\PosterGenerator\Providers\PosterGeneratorServiceProvider::class
  ...

配置

您可以在配置文件(config/poster.php)中调整设置。您还可以使用 artisan publish 命令发布配置文件

用法

首先,创建一个海报对象,其中包含您在海报中需要的属性。这可以是从 textimages 的任何内容。实现 Just\PosterGenerator\PosterInterface 非常重要,例如

<?php

namespace NameSpace\To\Your\Class;

use Just\PosterGenerator\PosterInterface;
use Symfony\Component\HttpFoundation\File\File;

class PosterX implements PosterInterface
{
    /**
     * @var File
     *
     */
    private $background;

    /**
     * @var string
     */
    private $text;

    /**
     * @param $background
     * @param $text
     */
    public function __construct(File $background, $text)
    {
        $this->background = $background;
        $this->text = $text;
    }

    /**
    * Returns a set of variables that are available to the view
    *
    * @return array
    */
    public function getProperties()
    {
        return [
            'background' => $this->getImage(),
            'text' => $this->getText()
        ];
    }

    /**
     * @return string
     */
    private function getText()
    {
        return strtoupper($this->text);
    }
  
    /**
     * @return string
     */
    private function getImage()
    {
        // Could you Croppa (https://github.com/BKWLD/croppa) to crop image and convert it to string (path)
        return Croppa::url($this->background->getFilename(), 595,842, ['resize']);
    }

    /**
     * Returns full path (blade) to your view. The arguments of 
     * getProperties() will be auto inserted 
     * in to this view
     * 
     * @return string
     */
    public function getTemplatePath()
    {
        return 'path.to.your.html.poster';
    }

    /**
     * Returns width/height of the poster
     *
     * @return array
     */
    public function getViewportSize()
    {
        return [595, 842];
    }
}

现在您有了海报对象,通过 Just\PosterGenerator\PosterGenerator 类生成海报就非常简单了

<?php

$poster = new PosterX('Title' new File('test.jpg'));

// We could generate a PDF
$response = $this->generator->generatePDF($poster);

// Or we could generate an Image
$response = $this->generator->generateImage($poster, 'jpg');

// In the $response we have the original response (getResponse()) out of Phantom
// and an getFile() method to get the generated file.
if ($response->getResponse()->getStatus() === 200) {
    return [
        'status' => 'success',
        'url' => $response->getFile()->getFilename())
    ];
}

return [
    'status' => 'error'
];