tlamedia/grabzit-bundle

GrabzitBundle 将 GrabzIt PHP 库与 Symfony 集成,允许您在 Symfony 项目中与 GrabzIt API 交互。

安装次数: 1,315

依赖者: 0

推荐者: 0

安全性: 0

星标: 3

关注者: 3

分支: 1

开放问题: 0

类型:symfony-bundle

2.1.1 2023-02-07 11:01 UTC

This package is auto-updated.

Last update: 2024-09-07 14:17:08 UTC


README

GrabzitBundleGrabzIt PHP 库与 Symfony 集成。

Latest Stable Version Build Status License

安装

获取捆绑包

composer require tlamedia/grabzit-bundle

然后在您的内核中启用它

// config/bundles.php
return [
    //...
    Tla\GrabzitBundle\TlaGrabzitBundle::class => ['all' => true],
    //...
];

配置

配置要使用的 API 密钥

# config.yml
tla_grabzit:
    key: 'Your_Application_Key' # Replace with your own
    secret: 'Your_Application_Secret' # Replace with your own

使用方法

该捆绑包注册了服务 tla_grabzit.imageoptionstla_grabzit.pdfoptionstla_grabzit.docxoptionstla_grabzit.animationoptionstla_grabzit.tableoptionstla_grabzit.client,这允许您使用 GrabzIt API。

从服务请求缩略图

namespace App\Service;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;

class ThumbnailGenerator
{
    private $container;

    public function __construct(Container $container)
    {
        $this->router = $router;
        $this->container = $container;
    }

    public function generateThumbnail($domain)
    {
        $grabzItHandlerUrl = 'https://www.my-grabzit-thumbnail-site.com/api/thumbmail-ready';

        $options = $this->container->get('tla_grabzit.imageoptions');
        $options->setBrowserWidth(1024);
        $options->setBrowserHeight(768);
        $options->setFormat("png");
        $options->setWidth(320);
        $options->setHeight(240);
        $options->setCustomId($domain);

        $grabzIt = $this->container->get('tla_grabzit.client');
        $grabzIt->URLToImage("http://".$domain, $options);
        $grabzIt->Save($grabzItHandlerUrl);

        try {
            $grabzIt->URLToImage("http://".$domain, $options);
            $grabzIt->Save($grabzItHandlerUrl);
            $result = true;
        } catch (\Throwable $t) {
            $result = false;
        }

        return $result;
    }
}

在控制器中使用 GrabzIt 处理器接收缩略图

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ApiController extends Controller
{
    public function thumbnailReadyAction(Request $request)
    {
        $id = urldecode($request->query->get('id'));
        $customId = $request->query->get('customid');
        $thumbnailFormat = $request->query->get('format');

        if ($id && $customId && $thumbnailFormat) {

            $grabzItApplicationKey = $this->container->getParameter('tla_grabzit.key');

            if (0 === strpos($id, $grabzItApplicationKey)) {

                $grabzIt = $this->container->get('tla_grabzit.client');
                $result = $grabzIt->GetResult($id);

                if ($result) {
                    $rootPath = $this->get('kernel')->getRootDir() . '/../';
                    $thumbnailsPath = $rootPath . 'var/thumbnails/';
                    $fileName = $customId. '.' .$thumbnailFormat;
                    
                    file_put_contents($thumbnailsPath . $fileName, $result);
                } else {
                    throw $this->createNotFoundException('GrabzIt did not return a file');
                }
            } else {
                throw $this->createNotFoundException('Wrong key - Unauthorized access');
            }
        } else {
            throw $this->createNotFoundException('Missing parameters');
        }
        return new Response(null, 200);
    }
}

文档

有关如何访问每个 API 方法的详细文档可以在GrabzIt 网站上的文档中找到

许可证

本软件包可在MIT 许可证下获得。