PHP screenshot with phantomjs
Ok, so I have been working on this / with this for several days. I am currently working with space / browser ( https://github.com/spatie/browsershot ).
Let me explain what I am trying to do first.
I am trying to get a screenshot of a webpage (it is actually multiple webpages). One is the mobile version and the other is the desktop version. Now it all works and doesn't cause any problems ... When I do it one at a time.
I am also using laravel 5 and their queuing system to process multiple web pages at the same time. This is where my problem begins. When I run about 5 workers to process their turn, they all take screenshots, which doesn't seem to be a problem because the first screenshot they all take always succeeds. But for some reason they just stop at the second screenshot (this doesn't happen when I just do this one at a time).
I think the phantomjs executable is probably crashing or something due to overload, but when I create 2 separate executables and run them separately (one for mobile SS and one for SS on desktop) I still face with this problem.
Does anyone have an idea or maybe a better solution for getting the screenshots I need?
Here every javascript runs
$fileContent= "var page = require('webpage').create();
page.settings.javascriptEnabled = true;
page.viewportSize = { width: " . $this->width . ",height:1000 };
page.open('" . $this->URL . "', function() {
window.setTimeout(function(){
page.render('" . $targetFile . "');
phantom.exit();
}, 5000); // give phantomjs 5 seconds to process all javascript
});";
Also for some reason, when I set the height to 1000, it will always take up the entire height of the page. When I used $ this-> width, it wouldn't do it.
source to share
I have also used the phantomjs project in the past and had many different problems and our users complained about it, it was rather unstable. We did a lot of restarts of our cron job in order to successfully create screenshots.
And for now, the phantomjs project is archived. https://github.com/ariya/phantomjs
And what is your answer to the question of "the best solution to get the screenshots I need?" I would suggest using chrome headless, at the moment I am using it in docker container https://hub.docker.com/r/justinribeiro/chrome-headless/
This is a fast and stable project, you will need to add additional code using puppeter, but it is not difficult https://developers.google.com/web/tools/puppeteer/
At the moment I have no problem with this solution in my application.
source to share