Terwijl de PHP-bibliotheek van GrabzIt zich richt op het bieden van een bibliotheek die in elk PHP-project kan worden gebruikt. symfony PHP-projecten worden op een unieke manier in elkaar gezet die wat meer werk vergt.
Symfony is een van de grootste PHP-frameworks die momenteel worden gebruikt en versnelt de webontwikkeling door een herbruikbare set bibliotheken en componenten te bieden. Waar GrabzIt nu deel van uitmaakt, dankzij Torben Lundsgaard van TLAMedia die een bundel GrabzIt voor Symfony heeft gemaakt. Deze open source-software maakt gebruik van de MIT-licentie.
Om de GrabzIt-bundel te krijgen, moet u deze eerst met composer installeren.
composer require tlamedia/grabzit-bundle
Voeg het dan toe aan je kernel.
public function registerBundles() { $bundles = array( //... new Tla\GrabzitBundle\TlaGrabzitBundle(), //…
Koop jouw API-sleutel en geheim en voeg ze op deze manier toe aan uw configuratiebestand.
# config.yml tla_grabzit: key: 'Sign in to view your Application Key' secret: 'Sign in to view your Application Secret'
De bundel registreert verschillende services die, wanneer ze worden aangeroepen, de juiste GrabzIt-klasse retourneren.
Service-ID | GrabzIt-klasse |
---|---|
tla_grabzit.client | GrabzItClient |
tla_grabzit.imageoptions | GrabzItImageOptions |
tla_grabzit.pdfoptions | GrabzItPDFOptions |
tla_grabzit.docxoptions | GrabzItDOCXOptions |
tla_grabzit.animationoptions | GrabzItAnimationOptions |
tla_grabzit.tableoptions | GrabzItTableOptions |
Een voorbeeld van hoe u een miniatuur kunt genereren in het Symfony Framework.
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; } }
Een voorbeeld van hoe u opnames van GrabzIt kunt ontvangen met behulp van een handler in het Symfony-framework. Uiteraard moet u dit aanpassen aan uw eigen wensen.
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); } }
Dit Help-artikel is uitgebreid van de hulp voor deze bundel, gedetailleerd op GitHub.