वेब पर कब्जा और परिवर्तित करने के लिए उपकरण

GrabzIt के कैप्चर एपीआई के साथ PHP के सिम्फनी फ्रेमवर्क का उपयोग करना

जबकि GrabzIt की PHP लाइब्रेरी एक लाइब्रेरी प्रदान करने पर केंद्रित है जिसका उपयोग किसी भी PHP प्रोजेक्ट में किया जा सकता है। Symfony PHP प्रोजेक्ट्स को एक अनूठे तरीके से एक साथ रखा जाता है जिसके लिए थोड़ा अधिक काम करने की आवश्यकता होती है।

सिम्फनी वर्तमान में उपयोग किए जाने वाले सबसे बड़े PHP फ्रेमवर्क में से एक है, यह पुस्तकालयों और घटकों का पुन: प्रयोज्य सेट प्रदान करके वेब विकास को गति देता है। टोरबेन लुंड्सगार्ड को धन्यवाद, जो GrabzIt अब एक हिस्सा है टीएलएमीडिया जिसने सिम्फनी के लिए GrabzIt का एक बंडल बनाया। यह ओपन सोर्स सॉफ्टवेयर का उपयोग करता है एमआईटी लाइसेंस.

GrabzIt बंडल प्राप्त करने के लिए आपको पहले इसे कंपोज़र के साथ इंस्टॉल करना होगा।

composer require tlamedia/grabzit-bundle

फिर इसे अपने कर्नेल में जोड़ें।

public function registerBundles()
{
$bundles = array(
//...
new Tla\GrabzitBundle\TlaGrabzitBundle(),
//…

विन्यास

जाओ अपने एपीआई कुंजी और गुप्त और उन्हें इस प्रकार अपनी कॉन्फ़िग फ़ाइल में जोड़ें।

# config.yml
tla_grabzit:
    key: 'Sign in to view your Application Key'
    secret: 'Sign in to view your Application Secret'

बंडल कई सेवाओं को पंजीकृत करता है जिन्हें कॉल करने पर उचित GrabzIt क्लास लौटाता है।

सेवा पहचानकर्ता ग्रैब्ज़इट क्लास
tla_grabzit.client GrabzItClient
tla_grabzit.imageoptions GrabzItImageOptions
tla_grabzit.pdfoptions GrabzItPDFOptions
tla_grabzit.docxoptions GrabzItDOCXOptions
tla_grabzit.animationoptions GrabzItAnimationOptions
tla_grabzit.tableoptions GrabzItTableOptions

कैप्चर कैसे जनरेट करें

सिम्फनी फ्रेमवर्क में थंबनेल कैसे उत्पन्न करें इसका एक उदाहरण।

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(1366);
        $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;
    }
}

हैंडलर के साथ कैप्चर कैसे प्राप्त करें

सिम्फनी फ्रेमवर्क में एक हैंडलर का उपयोग करके 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 पर विस्तृत है.