Mentre la libreria PHP di GrabzIt si concentra sulla fornitura di una libreria che può essere utilizzata in qualsiasi progetto PHP. symfony I progetti PHP sono riuniti in un modo unico che richiede un po 'più di lavoro.
Symfony è uno dei più grandi framework PHP attualmente utilizzati che accelera lo sviluppo web fornendo un set riutilizzabile di librerie e componenti. Di cui Grabz è ora parte, grazie a Torben Lundsgaard di TLAMedia che ha creato un pacchetto di GrabzIt per Symfony. Questo software open source utilizza il MIT License.
Per ottenere il pacchetto GrabzIt devi prima installarlo con il compositore.
composer require tlamedia/grabzit-bundle
Quindi aggiungilo al tuo kernel.
public function registerBundles()
{
$bundles = array(
//...
new Tla\GrabzitBundle\TlaGrabzitBundle(),
//…
Configurazione
Ottenete il vostro Chiave API e segreto e aggiungili al tuo file di configurazione in questo modo.
# config.yml
tla_grabzit:
key: 'Sign in to view your Application Key'
secret: 'Sign in to view your Application Secret'
Il bundle registra diversi servizi che, quando chiamati, restituiscono la classe GrabzIt appropriata.
Come generare catture
Un esempio di come generare una miniatura in 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;
}
}
Come ricevere acquisizioni con un gestore
Un esempio di come ricevere acquisizioni da GrabzIt usando un gestore nel framework Symfony. Ovviamente dovresti modificarlo per soddisfare le tue esigenze.
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);
}
}
Questo articolo di aiuto è stato ampliato da aiuto per questo pacchetto dettagliato su GitHub.