GrabzIt的PHP库着重于提供可在任何PHP项目中使用的库。 Symfony的 PHP项目以独特的方式组合在一起,需要更多的工作。
Symfony是当前使用的最大的PHP框架之一,它通过提供一组可重用的库和组件来加速Web开发。 感谢的Torben Lundsgaard,现在GrabzIt属于其中 TLA媒体 为Symfony创建了一捆GrabzIt。 此开源软件使用 MIT许可证.
要获取GrabzIt捆绑包,您必须首先使用composer安装它。
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上有详细介绍.