While GrabzIt's PHP Library focuses on providing a library that can be used in any PHP project. Symfony PHP projects are put together in a unique way that requires a bit more work.
Symfony is one of the biggest PHP Frameworks used currently it speeds up web development by providing a reusable set of libraries and components. Which GrabzIt is now a part of, thanks to Torben Lundsgaard of TLAMedia who created a bundle of GrabzIt for Symfony. This open source software uses the MIT License.
To get the GrabzIt Bundle you must first install it with composer.
composer require tlamedia/grabzit-bundle
Then add it to your kernel.
public function registerBundles()
{
$bundles = array(
//...
new Tla\GrabzitBundle\TlaGrabzitBundle(),
//…
Configuration
Get your API Key and Secret and add them to your config file like so.
# config.yml
tla_grabzit:
key: 'Sign in to view your Application Key'
secret: 'Sign in to view your Application Secret'
The bundle registers several services which when called returns the appropriate GrabzIt class.
How to Generate Captures
An example of how to generate a thumbnail in the 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(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;
}
}
How to receive captures with a handler
An example of how to receive captures from GrabzIt using a handler in the Symfony framework. Of course you would need to alter this to match your own requirements.
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);
}
}
This help article has been expanded from the help for this bundle detailed on GitHub.