Outils pour capturer et convertir le Web

Capturez des captures d'écran de sites Web ou convertissez du HTML en images

API PHP

Créez des captures d’images parfaites de sites Web ou convertissez du HTML directement en images en utilisant les fonctionnalités suivantes de API PHP de GrabzIt. Cependant, avant de commencer, rappelez-vous qu'après avoir appelé le URLToImage, HTMLToImage or FileToImage méthodes le Save or SaveTo La méthode doit être appelée pour créer l'image.

Options de base

Un seul paramètre est requis pour prendre une capture d'écran d'une page Web ou convertir HTML into une image comme indiqué dans l'exemple suivant.

$grabzIt->URLToImage("https://www.tesla.com");
//Then call the Save or SaveTo method
$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>");
//Then call the Save or SaveTo method
$grabzIt->FileToImage("example.html");
//Then call the Save or SaveTo method

Formats d'image

L'API PHP de GrabzIt permet de créer des images dans de nombreux formats, notamment JPG, PNG, WEBP, BMP (bits 8, bits 16, bits 24 ou bits 32) et TIFF. Le format par défaut pour les images est JPG. Cependant, la qualité d'une image JPG peut ne pas être suffisante pour certaines applications. Dans ces circonstances, le format PNG est recommandé pour les captures d'écran, car il offre un bon équilibre entre qualité et taille du fichier. L'exemple ci-dessous montre une image en cours de création au format PNG.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setFormat("png");

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.png");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setFormat("png");

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.png");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");
            
$options = new \GrabzIt\GrabzItImageOptions();
$options->setFormat("png");

$grabzIt->FileToImage("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.png");

Taille du navigateur

La taille du navigateur fait référence à la taille de la fenêtre du navigateur qui sera utilisée lors de la capture de la capture d'écran. Dans la plupart des cas, il n'est pas nécessaire de la définir car la taille du navigateur par défaut sera suffisante pour la plupart des tâches. Pour définir la taille du navigateur, il suffit de passer une valeur à la setBrowserWidth et setBrowserHeight méthodes de la GrabzItImageOptions classe.

Changer la taille de l'image

Changer la taille d'une image est facile, le faire sans la déformer est un peu plus difficile. Pour simplifier l’ensemble du processus, nous vous recommandons de l’utiliser. calculateur simple de dimension d'image.

Si vous souhaitez augmenter la largeur et la hauteur de l'image à une taille supérieure à celle du navigateur, qui correspond par défaut à 1366 de 728 pixels, vous devez également augmenter la largeur et la hauteur du navigateur.

Identifiant personnalisé

Vous pouvez transmettre un identifiant personnalisé au image Comme indiqué ci-dessous, cette valeur est ensuite renvoyée à votre gestionnaire PHP GrabzIt. Par exemple, cet identifiant personnalisé pourrait être un identifiant de base de données, permettant d'associer une capture d'écran ou une image à un enregistrement de base de données particulier.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setCustomId(123456);

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setCustomId(123456);

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setCustomId(123456);

$grabzIt->FileToImage("example.html", $options);
//Then call the Save method
$grabzIt->Save("http://www.example.com/handler.php");

Capture d'écran complète

GrabzIt vous permet de prendre une capture d'écran complète d'une page Web entière. Pour ce faire, vous devez passer un -1 au setBrowserHeight méthode. Pour que l’image corresponde à la taille du navigateur, transmettez un -1 au setHeight et setWidth méthodes.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setBrowserHeight(-1);
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setBrowserHeight(-1);
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setBrowserHeight(-1);
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->FileToImage("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");

Vous pouvez également renvoyer des captures d'écran qui ne sont pas recadrées, mais attention, cela peut créer de grandes images. Pour ce faire, passez un -1 au setHeight (facultatif) setWidth méthodes. Toute dimension ayant reçu un -1 ne sera pas recadrée.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->URLToImage("https://www.tesla.com", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->HTMLToImage("<html><body><h1>Hello World!</h1></body></html>", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);

$grabzIt->FileToImage("example.html", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");
Notez qu'il n'y a pas de largeur totale du navigateur!

L'utilisation de ces valeurs spéciales signifie que vous pouvez créer une capture d'écran qui est une version complète de la page Web entière si vous le souhaitez!

Prendre une capture d'écran d'un élément de page

GrabzIt vous permet de prendre une capture d'écran d'un élément HTML, tel qu'un div or span tag, et capturer tout son contenu. Pour ce faire, l'élément HTML que vous souhaitez capturer doit être spécifié comme Sélecteur CSS.

...
<div id="features">
	<img src="http://www.example.com/peace.jpg"/><h3>World Peace Announced</h3>
</div>
...

Pour l'exemple ci-dessous, nous allons sélectionner la div avec l'id "features" et la sortir sous forme d'image JPEG 250 x 250px.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

// The 250 parameters indicates that image should be sized to 250 x 250 px
$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(250);
$options->setHeight(250);
$options->setFormat("jpg");
$options->setTargetElement("#features");

$grabzIt->URLToImage("http://www.bbc.co.uk/news", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");

L'exemple suivant prend une autre capture d'écran de la fonction "features" mais cette fois-ci génère une image JPEG de la taille exacte de la div.

$grabzIt = new \GrabzIt\GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

// The -1 indicates that image should not be cropped
$options = new \GrabzIt\GrabzItImageOptions();
$options->setWidth(-1);
$options->setHeight(-1);
$options->setBrowserHeight(-1);
$options->setFormat("jpg");
$options->setTargetElement("#features");

$grabzIt->URLToImage("http://www.bbc.co.uk/news", $options);
//Then call the Save or SaveTo method
$grabzIt->SaveTo("result.jpg");