phpjquerycssphantomjsphp-phantomjs

[PHP-]PhantomJS - auto detecting height of page given height


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);
}

Solution

  • 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);