Die PHP-Bibliothek von GrabzIt konzentriert sich auf die Bereitstellung einer Bibliothek, die in jedem PHP-Projekt verwendet werden kann. Symfonie PHP-Projekte werden auf einzigartige Weise zusammengestellt, was etwas mehr Arbeit erfordert.
Symfony ist eines der größten PHP-Frameworks, das derzeit verwendet wird. Es beschleunigt die Webentwicklung, indem es einen wiederverwendbaren Satz von Bibliotheken und Komponenten bereitstellt. Zu welchem GrabzIt gehört nun, dank Torben Lundsgaard von TLAMedia wer hat ein Bundle von GrabzIt für Symfony erstellt. Diese Open Source Software verwendet die MIT-Lizenz.
Um das GrabzIt-Bundle zu erhalten, müssen Sie es zuerst mit composer installieren.
composer require tlamedia/grabzit-bundle
Dann füge es deinem Kernel hinzu.
public function registerBundles()
{
$bundles = array(
//...
new Tla\GrabzitBundle\TlaGrabzitBundle(),
//…
Konfiguration
Ihre API-Schlüssel und Geheimnis und füge sie wie folgt zu deiner Konfigurationsdatei hinzu.
# config.yml
tla_grabzit:
key: 'Sign in to view your Application Key'
secret: 'Sign in to view your Application Secret'
Das Bundle registriert mehrere Dienste, die beim Aufruf die entsprechende GrabzIt-Klasse zurückgeben.
So generieren Sie Captures
Ein Beispiel für das Generieren einer Miniaturansicht 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;
}
}
So empfangen Sie Captures mit einem Handler
Ein Beispiel für das Empfangen von Captures von GrabzIt mithilfe eines Handlers im Symfony-Framework. Natürlich müssten Sie dies ändern, um es Ihren eigenen Anforderungen anzupassen.
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);
}
}
Dieser Hilfeartikel wurde aus dem erweitert Hilfe zu diesem Bundle finden Sie auf GitHub.