捕获和转换Web的工具

将PHP的Symfony框架与GrabzIt的Capture API结合使用

而 GrabzIt 的 PHP 库专注于提供一个可以在任何 PHP 项目中使用的库。 Symfony的 PHP 项目以一种独特的方式组合在一起,需要做更多的工作。

Symfony 是目前使用的最大的 PHP 框架之一,它通过提供一组可重用的库和组件来加速 Web 开发。 GrabzIt 现在是其中的一部分,感谢 Torben Lundsgaard 特拉媒体 他为 Symfony 创建了 GrabzIt 包。 该开源软件使用 MIT许可证.

要获取 GrabzIt Bundle,您必须首先使用 Composer 安装它。

composer require tlamedia/grabzit-bundle

然后将其添加到您的内核中。

public function registerBundles()
{
$bundles = array(
//...
new Tla\GrabzitBundle\TlaGrabzitBundle(),
//…

配置

UCpay API密钥和秘密 并将它们添加到您的配置文件中,如下所示。

# config.yml
tla_grabzit:
    key: 'Sign in to view your Application Key'
    secret: 'Sign in to view your Application Secret'

该捆绑包注册了多个服务,这些服务在调用时返回适当的 GrabzIt 类。

服务标识符 抢夺类
tla_grabzit.client GrabzItClient
tla_grabzit。imageoptions GrabzItImageOptions
tla_grabzit.pdf选项 GrabzItPDFOptions
tla_grabzit。docxoptions GrabzItDOCXOptions
tla_grabzit。animationoptions GrabzItAnimationOptions
tla_grabzit。tableoptions GrabzItTableOptions

如何生成捕获

如何在 Symfony 框架中生成缩略图的示例。

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($url)
    {
        $grabzItHandlerUrl = 'https://www.my-grabzit-thumbnail-site.com/api/thumbmail-ready';

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

        $grabzIt = $this->container->get('tla_grabzit.client');
        $grabzIt->URLToImage($url, $options);
        $grabzIt->Save($grabzItHandlerUrl);

        try {
            $grabzIt->URLToImage($url, $options);
            $grabzIt->Save($grabzItHandlerUrl);
            $result = true;
        } catch (\Throwable $t) {
            $result = false;
        }

        return $result;
    }
}

如何使用处理程序接收捕获

如何使用 Symfony 框架中的处理程序从 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);
    }
}

本帮助文章已从 GitHub 上详细介绍了此捆绑包的帮助.