Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Last active January 16, 2018 09:31
Show Gist options
  • Save jakubkulhan/35757f153c9a6ce55f75729e6f0abcf9 to your computer and use it in GitHub Desktop.
Save jakubkulhan/35757f153c9a6ce55f75729e6f0abcf9 to your computer and use it in GitHub Desktop.
Capture screenshots of the webpage loaded into a running Chrome instance using https://github.com/jakubkulhan/chrome-devtools-protocol
<?php
use ChromeDevtoolsProtocol\Context;
use ChromeDevtoolsProtocol\Instance\Instance;
use ChromeDevtoolsProtocol\Model\Page\CaptureScreenshotRequest;
use ChromeDevtoolsProtocol\Model\Page\NavigateRequest;
use ChromeDevtoolsProtocol\Model\Page\SetDeviceMetricsOverrideRequest;
$url = "...";
$ctx = Context::withTimeout(Context::background(), 30);
$instance = new Instance("127.0.0.1", 9222);
$session = $instance->createSession($ctx);
try {
$session->page()->enable($ctx);
$session->page()->setDeviceMetricsOverride(
$ctx,
SetDeviceMetricsOverrideRequest::builder()
->setWidth(1200)
->setHeight(630)
->setDeviceScaleFactor(1.0)
->setMobile(false)
->build()
);
$session->page()->navigate(
$ctx,
NavigateRequest::builder()
->setUrl($url)
->build()
);
$session->page()->awaitLoadEventFired($ctx);
$png = $session->page()->captureScreenshot(
$ctx,
CaptureScreenshotRequest::builder()
->setFormat("png")
->build()
);
$pngBinaryData = base64_decode($png->data);
// ...
} finally {
$session->close();
}
docker run \
-d \
--cap-add SYS_ADMIN \
--name chrome \
-p 9222:9222 \
jakubkulhan/chrome:63.0.3239.132 \
--headless \
--disable-gpu \
--remote-debugging-address=0.0.0.0 \
--remote-debugging-port=9222 \
--lang=cs_CZ \
--hide-scrollbars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment