כלים ללכידה ולהמרה של האינטרנט

שימוש במסגרת הסימפוני של PHP עם ממשק ה- API של לכידת GrabzIt

בעוד שספריית ה-PHP של GrabzIt מתמקדת באספקת ספרייה שניתן להשתמש בה בכל פרויקט PHP. סימפוני פרויקטי PHP מורכבים בצורה ייחודית הדורשת קצת יותר עבודה.

Symfony היא אחת ממסגרות ה-PHP הגדולות ביותר בשימוש כיום, והיא מאיצה את פיתוח האינטרנט על ידי אספקת ערכה לשימוש חוזר של ספריות ורכיבים. ש-GrabzIt הוא כעת חלק ממנו, הודות לטורבן לונדסגורד TLAMedia שיצר צרור של GrabzIt עבור סימפוניה. תוכנת קוד פתוח זו משתמשת ב- רישיון MIT.

כדי לקבל את ה-GrabzIt Bundle, תחילה עליך להתקין אותו עם 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.

מזהה שירות שיעור 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

כיצד ליצור לכידות

דוגמה כיצד ליצור תמונה ממוזערת במסגרת 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(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 באמצעות מטפל במסגרת Symfony. כמובן שתצטרך לשנות את זה כדי להתאים לדרישות שלך.

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.