4

I'm using PHP-PhantomJS to screenshot an array of URLs. I can't seem to figure out how to get away with not hardcoding the height of the screenshot being taken.

I'd love to set a width and have the height of each page be auto-detected accordingly. Since I'm working with an array of URLs, each page's height is dynamic.

Has anyone had luck with this? My current version looks like this:

public function takeScreenshots($site, $newDir, $dimensions) {
    $urlHost = parse_url($site)["host"];
    $urlPath = isset(parse_url($site)['path']) ? parse_url($site)['path'] : '';
    $urlPathName = str_replace("/", "",$urlPath);
    $filepath = $newDir . $urlHost . "_" . $urlPathName . ".jpg";

    $client = Client::getInstance();
    $client->getEngine()->setPath(base_path().'/bin/phantomjs');

    $width  = $dimensions["width"];
    $height = $dimensions["height"];
    $top    = 0;
    $left   = 0;

    $request = $client->getMessageFactory()->createCaptureRequest($site, 'GET');
    $request->setOutputFile($filepath);
    $request->setViewportSize($width, $height);
    $request->setCaptureDimensions($width, $height, $top, $left);
    $response = $client->getMessageFactory()->createResponse();
    $client->send($request, $response);
}
John Slegers
  • 45,213
  • 22
  • 199
  • 169
Lauren F
  • 1,282
  • 4
  • 18
  • 29
  • Doesn't PhantomJS make screenshot of the whole page? – Vaviloff Feb 12 '16 at 17:39
  • @Vaviloff If you don't give it dimensions, it'll default to mobile view (which in some instances would be fine), but giving dimensions requires both a height and width... – Lauren F Feb 12 '16 at 18:54

1 Answers1

1

Just set the viewport size, no need to set capture dimensions if you want to make a screenshot of the whole page. PhantomJS will use provided width and will make a screenshot as tall as the page is.

In raw PhantomJS script you would do it by setting viewportSize property of a page:

page.viewportSize = { width: 1280, height: 1024 };

In the "PHP PhantomJs" library it is almost the same:

$request->setViewportSize(1280, 1024);
Vaviloff
  • 16,282
  • 6
  • 48
  • 56