0

我正在尝试捕获屏幕并将其保存在本地。当我运行代码时,它会向我发送一条错误消息

致命错误:未捕获 JonnyW\PhantomJs\Exception\NotWritableException:PhantomJs 无法写入输出文件:

这是我的代码

<?php
require 'vendor/autoload.php';

use JonnyW\PhantomJs\Client;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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

$width  = 800;
$height = 600;
$top    = 0;
$left   = 0;

/** 
 * @see JonnyW\PhantomJs\Http\CaptureRequest
 **/
$request = $client->getMessageFactory()->createCaptureRequest('http://jonnyw.me');
$request->setOutputFile('/screenshot/TEST.jpg');
$request->setViewportSize($width, $height);
$request->setCaptureDimensions($width, $height, $top, $left);

/** 
 * @see JonnyW\PhantomJs\Http\Response 
 **/
$response = $client->getMessageFactory()->createResponse();

// Send the request
$client->send($request, $response);

?>

这是我的表格

<!DOCTYPE html>
<html>
<head>
    <title>Upload Files to Crop/Take Screenshot of URL</title>
</head>
<body> 

  <h1>TAKE SCREENSHOT OF URL WITH phantomjs <span style="color:red">[NOT WORKING YET!!!!]</span></h1>
  <form enctype="multipart/form-data" method="post" action="phantomjs.php">
    <div class="row">
      <label for="siteToCapture">Site to Capture</label><br />
      https://www.<input type="input" name="siteToCapture" id="siteToCapture" />.com
    </div>
    <div class="row">
      <input type="submit" value="SCAN" />
    </div>
  </form>

</body>
</html>

我注意到的另一件有趣的事情是,如果我在 screenshot/TEST.jpg 之前删除了“/”,我会收到一个没有错误且仍然没有屏幕截图的空白页面。

我还在根目录中创建了截图文件夹。

4

1 回答 1

1

看,您正在尝试将屏幕截图写入/screenshot/文件夹。并且路径是绝对的。因此,请确保您/screenshot/的磁盘根目录中有文件夹,并且脚本可以写入该文件夹。

有两种路径:绝对路径和相对路径。

第一个是从磁盘根目录开始的路径,它总是/字符开始,如下所示:/usr/local/bin/phantomjs.

第二个是对于您当前工作目录的路径,它可以以./字符开头,或../,或只是some/folder/。因此,在您的情况下,如果您screenshots在当前工作目录中有文件夹,您应该像这样指定输出文件:

$request->setOutputFile('./screenshot/TEST.jpg');

注意开头的字符。

于 2016-09-30T20:32:13.487 回答