【问题标题】:[PHP-]PhantomJS - auto detecting height of page given height[PHP-]PhantomJS - 自动检测给定高度的页面高度
【发布时间】:2016-05-23 22:18:06
【问题描述】:

我正在使用PHP-PhantomJS 截取一组 URL。我似乎无法弄清楚如何不硬编码正在拍摄的屏幕截图的高度。

我很想设置一个宽度并相应地自动检测每个页面的高度。由于我使用的是一组 URL,因此每个页面的高度都是动态的。

有人遇到过这个吗?我当前的版本如下所示:

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

【问题讨论】:

  • PhantomJS 不截图整个页面吗?
  • @Vaviloff 如果你不给它尺寸,它会默认为移动视图(在某些情况下会很好),但给尺寸需要高度和宽度......跨度>

标签: php jquery css phantomjs php-phantomjs


【解决方案1】:

只需设置视口大小,如果要对整个页面进行截图,则无需设置捕获尺寸。 PhantomJS 将使用提供的宽度,并将屏幕截图与页面一样高。

在原始 PhantomJS 脚本中,您可以通过设置页面的 viewportSize 属性来实现:

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

在“PHP PhantomJs”库中几乎是一样的:

$request->setViewportSize(1280, 1024);

【讨论】:

  • 哇,没想到这么简单。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 2012-09-04
  • 1970-01-01
  • 2013-07-28
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多