jonnnnyw / php-phantomjs

Execute PhantomJS commands through PHP
MIT License
1.44k stars 432 forks source link

Output file is not writeable by PhantomJs #258

Open Gummet opened 6 years ago

Gummet commented 6 years ago

I'm getting the following when trying to generate a screenshot. The script runs as bitnami, also bitnami is the owner for everything under /htdocs. I've even tested setting all files/folders to 777 just to give it a quick test and see if the file will generate but no luck.

Could I be doing something wrong here that I am not relising?

[Thu Jul 26 16:51:50.456619 2018] [proxy_fcgi:error] [pid 1808:tid 140458139199232] [client 141.101.98.151:22227] AH01071: Got error 'PHP message: PHP Fatal error:  Uncaught JonnyW\\PhantomJs\\Exception\\NotWritableException: Output file is not writeable by PhantomJs: /opt/bitnami/apache2/htdocs/test.jpg in /opt/bitnami/apache2/htdocs/vendor/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Http/CaptureRequest.php:199

Stack trace:
#0 /opt/bitnami/apache2/htdocs/myClasses/MakeChart.php(35): JonnyW\\PhantomJs\\Http\\CaptureRequest->setOutputFile('/opt/bitnami/ap...')
#1 /opt/bitnami/apache2/htdocs/Commands/ChartCommand.php(23): MakeChart->takeScreenshot()
#2 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Commands/Command.php(173): Longman\\TelegramBot\\Commands\\UserCommands\\ChartCommand->execute()
#3 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Telegram.php(537): Longman\\TelegramBot\\Commands\\Command->preExecute()
#4 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Telegram.php(505): Longman\\TelegramBot\\Telegram->executeCommand('chart')
#5 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Telegram.php(423):...'

Here is the class/function that is called to generate the screenshot if it helps at all:

<?php

require __DIR__ . '/../vendor/autoload.php';
use JonnyW\PhantomJs\Client;

class MakeChart
{
    public function takeScreenshot()
    {
        $client = Client::getInstance();

        // isLazy() is useful when we want to wait for all resources on the page to load.
        $client->isLazy();

        // Some options
        $client->getEngine()->addOption('--load-images=true');
        $client->getEngine()->addOption('--ignore-ssl-errors=true');

        $vWidth  = 2560;
        $vHeight = 1440;

        $width  = 1910;
        $height = 670;
        $top    = 95;
        $left   = 15;

        // Here specify the page to take the screenshot
        $request  = $client->getMessageFactory()->createCaptureRequest('https://google.com');

        $request->setCaptureDimensions($width, $height, $top, $left);
        $request->setViewportSize($vWidth, $vHeight);
        $request->setBodyStyles(['backgroundColor' => '#ffffff']);

        $file = getcwd() . '/test.jpg';
        $request->setOutputFile($file);

        $response = $client->getMessageFactory()->createResponse();

        // Send the request
        $client->send($request, $response);
    }
}
cdknight commented 6 years ago

Getting the same issue as well. I'm not sure how to fix it currently.

nicklike commented 6 years ago

/vendor/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Http/CaptureRequest.php

/**

50l3r commented 5 years ago

Same problem. +1

    private function screenshot($url, $output, $type = "pc"){
        $client = Client::getInstance();
        $client->getEngine()->setPath(APPPATH."../../bin/phantomjs.exe");
        $client->getEngine()->addOption('--ignore-ssl-errors=true');
        $client->isLazy();
        $client->getEngine()->debug(true);

        $width  = 1920;
        $height = 1080;
        $top    = 0;
        $left   = 0;

        switch($type){
            case "mobile":
                $width  = 375;
                $height = 667;
            break;

            default:
                $width  = 1920;
                $height = 1080;
            break;
        }

        $request  = $client->getMessageFactory()->createCaptureRequest($url);
        $request->setOutputFile(APPPATH.'tmp'.DIRECTORY_SEPARATOR.$output);
        $request->setTimeout(5000);
        $request->setViewportSize($width, $height);
        $request->setCaptureDimensions($width, $height, $top, $left);

        $response = $client->getMessageFactory()->createResponse();

        $client->send($request, $response)->getContent();
    }
Koredeoluwafemi commented 5 years ago

I know this issue is an old one but I solved the problem by first creating an empty file named file.pdf(in my own case) and it worked fine.

pablosouza10 commented 4 years ago

I solved disable the sellinux on Centos 7.