GrabzItのPHPライブラリは、任意のPHPプロジェクトで使用できるライブラリの提供に焦点を当てています。 Symfony PHPプロジェクトは、もう少し作業が必要な独自の方法でまとめられます。
symfonyは、現在使用されている最大のPHPフレームワークの1つであり、再利用可能なライブラリとコンポーネントのセットを提供することでWeb開発を高速化します。 どのGrabzItの一部になりました。TorbenLundsgaardのおかげです TLAMedia Symfony用のGrabzItのバンドルを作成した人。 このオープンソースソフトウェアは MITライセンス.
GrabzItバンドルを入手するには、まずコンポーザーでインストールする必要があります。
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クラスを返すいくつかのサービスを登録します。
キャプチャを生成する方法
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(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($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で詳しく説明されているこのバンドルのヘルプ.