1

I am trying to take a screenshot of a page using GD library but as a result Screen capture only returns a black image.

<?php
header('Content-type: text/html; charset=windows-1251');
?>

<?php
header("Location:http://10.32.172.30:9080/RTOnline/b/p/115");
$img = imagegrabscreen();
imagepng($img, 'screenshot.png');
?>
  • 2
    `imagegrabscreen()` captures the (desktop) screen of the computer on which PHP itself runs, not the HTML page being served by PHP to a client computer's browser. – KIKO Software Aug 04 '19 at 08:10
  • You can open the web page in a browser on the computer which is running Php and then take a screenshot using GD. Also the imagegrabscreen function only works on windows – Nadir Latif Aug 04 '19 at 09:49
  • Setting HTTP headers suggests you aren't running PHP from the command-line but through a web server. In that case you're most likely using the same credentials as the web server and there's certainly no desktop session open. – Álvaro González Aug 04 '19 at 14:06

1 Answers1

0

Well you can use a command line tool to take a screenshot of a webpage. You can run the tool within Php using shell_exec function. For example the wkhtmltopdf command line tool allows converting from html to pdf and other formats.

If the web page contains complex JavaScript or Ajax, then you can use a headless browser to take the screenshot. For example see: Chrome Headless. Also see the Html2Canvas script. It allows taking screenshots using JavaScript.

Also see this question: Website screenshots

Nadir Latif
  • 3,690
  • 1
  • 15
  • 24