tlamedia / grabzit-bundle
GrabzitBundle 将 GrabzIt PHP 库与 Symfony 集成,允许您在 Symfony 项目中与 GrabzIt API 交互。
2.1.1
2023-02-07 11:01 UTC
Requires
- php: ^7.1 || ^8.0
- grabzit/grabzit: ^3.4
- symfony/config: ^3.4 || ^4.4 || ^5.4
- symfony/dependency-injection: ^3.4.26 || ^4.4 || ^5.4
- symfony/http-kernel: ^3.4 || ^4.4.50 || ^5.4
Requires (Dev)
- symfony/phpunit-bridge: ^4.4 || ^5.4
README
GrabzitBundle 将 GrabzIt PHP 库与 Symfony 集成。
安装
获取捆绑包
composer require tlamedia/grabzit-bundle
然后在您的内核中启用它
// config/bundles.php return [ //... Tla\GrabzitBundle\TlaGrabzitBundle::class => ['all' => true], //... ];
配置
配置要使用的 API 密钥
# config.yml tla_grabzit: key: 'Your_Application_Key' # Replace with your own secret: 'Your_Application_Secret' # Replace with your own
使用方法
该捆绑包注册了服务 tla_grabzit.imageoptions、tla_grabzit.pdfoptions、tla_grabzit.docxoptions、tla_grabzit.animationoptions、tla_grabzit.tableoptions 和 tla_grabzit.client,这允许您使用 GrabzIt API。
从服务请求缩略图
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($domain) { $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("http://".$domain, $options); $grabzIt->Save($grabzItHandlerUrl); try { $grabzIt->URLToImage("http://".$domain, $options); $grabzIt->Save($grabzItHandlerUrl); $result = true; } catch (\Throwable $t) { $result = false; } return $result; } }
在控制器中使用 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); } }
文档
有关如何访问每个 API 方法的详细文档可以在GrabzIt 网站上的文档中找到
许可证
本软件包可在MIT 许可证下获得。