phpphantomjswebpage-screenshotwritablephp-phantomjs

PHP PhantomJS unable to make screenshot directory writable


I am trying to capture a screen and save it locally. When I run the code it shoots me an error message

Fatal error: Uncaught JonnyW\PhantomJs\Exception\NotWritableException: Output file is not writeable by PhantomJs:

This is my code

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

?>

And this is my form

<!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>

Another interesting thing I noticed was if I removed the '/' before screenshot/TEST.jpg I receive a blank page with no errors and still no screenshot.

I have also created the screenshot folder in the root directory.


Solution

  • Look, you are trying to write screenshot to /screenshot/ folder. And the path is absolute. So make sure you have /screenshot/ folder in the root of your disk and script can write to it.

    There are two kinds of paths: absolute and relative.

    First one, is path that is starts from the root of your disk and it always starts from / character, like this: /usr/local/bin/phantomjs.

    Second one is the path which relative to your current working dirrectory, it can starts with ./ characters, or ../, or just some/folder/. So, in your case, if you have screenshots folder within your current working directory, you should specify output file like this:

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

    Notice the dot char at the beginning.