捕獲和轉換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(),
//…

型號

讓您的 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 抓斗ImageOptions
tla_grabzit.pdf選項 GrabzItPDFOptions
tla_grabzit。docxoptions 抓斗DOCXOptions
tla_grabzit。animationoptions 抓斗AnimationOptions
tla_grabzit。tableoptions 抓斗TableOptions

如何產生捕獲

如何在 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 上詳細介紹了此捆綁包的幫助.