【问题标题】:PHP PhantomJS unable to make screenshot directory writablePHP PhantomJS 无法使屏幕截图目录可写
【发布时间】:2017-02-09 11:16:27
【问题描述】:

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

致命错误:未捕获 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 之前删除了“/”,我会收到一个没有错误且仍然没有屏幕截图的空白页面。

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

【问题讨论】:

    标签: php phantomjs webpage-screenshot writable php-phantomjs


    【解决方案1】:

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

    有两种路径:绝对相对

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

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

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

    注意开头的 dot 字符。

    【讨论】:

    • 对不起,我也应该澄清一下。我已经在正确的目录中创建了文件夹。
    • OP 似乎不知道绝对路径是什么。
    • 也许 =)。更新了我的答案
    • 啊啊啊我明白你的意思了。 DIR 给出了当前目录之外的路径。
    猜你喜欢
    • 2015-10-30
    • 2014-03-18
    • 2015-05-16
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多