Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I can run the phantomjs command just fine from terminal, but from shell_exec, it only partially works. It's running phantomjs and the .js file, but it's returning page rendered without actually rendering the image. It's supposed to save a png to the same directory, but I'm not getting anything. Again, it creates the image just fine from terminal when in that directory. Any ideas? I think it has something to do with a limitation from being run from PHP. I've tried running phantomjs with sudo from shell_exec by giving www-data full access without password and that didn't change anything. I also chmodded the phantomjs, but honestly, that shit didn't make sense any way since it's NOT failing to run the script. It's working partially. I wish phantomjs was throwing an error that I could debug with...

Here's the command:

sudo phantomjs --ignore-ssl-errors=true --cookies-file=cookies.txt screenshot.js 'http://paypal.com' picture.png 600 1200 full

Here's screenshot.js

if(phantom.args.length < 5 || phantom.args.length > 5) {
console.log('Not all params are supplied.');
phantom.exit(1);
}
else{
var page = new WebPage(),
address, filename, delay, vwidth, vheight;
address = phantom.args[0];
filename = phantom.args[1];
delay = phantom.args[2];
vwidth = phantom.args[3];
vheight = phantom.args[4];

if(vwidth == 320 && vheight == 480 || vwidth == 480 && vheight == 320){ 
page.settings.userAgent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us)     AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'; 
}
else if(vwidth == 768 && vheight == 1024 || vwidth == 1024 && vheight == 768){ 
page.settings.userAgent = 'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us)     AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10'; 
}

page.viewportSize = { width: vwidth, height: '1000' }; //full height
page.open(address, function (status) {
if (status !== 'success') {
    console.log('Unable to load the address!');
    phantom.exit('1');
} else {
    window.setTimeout(function () {
        page.evaluate(function() {
            var style = document.createElement('style'),
            text = document.createTextNode('body { background: #fff; }');
            style.setAttribute('type', 'text/css');
            style.appendChild(text);
            document.head.insertBefore(style, document.head.firstChild);
        });
        if(vheight != 'full'){ page.clipRect = { top: 0, left: 0, width: vwidth, height: vheight } } //clip bottom
        page.render(filename);
        console.log('done rendering');
        page.close()
        phantom.exit();
    }, delay);
}
});


}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.