12

I'm trying to capture a local web page with imagegrabscreen but I only get a black screenshot. I tried almost every solution from questions here on SO and others sites and nothing works.

I'm using and done the following:

  • Windows 7 64bit
  • Wamp 2.2a 64bit
  • PHP 5.3.8
  • gd2 (version: "bundled 2.0.34 compatible") is installed and enabled.
  • Allowed the apache service to interact with the desktop.
  • I don't have a secondary display or anything.
<?php    
   $im = imagegrabscreen();    
   imagepng($im, "myscreenshot.png");    
   imagedestroy($im);    
?>

And all I get is a black image 1024x768 png.

Danny
  • 986
  • 1
  • 18
  • 42
  • Do you have a graphics server running (XFree86, X11)? – knittl Jan 19 '12 at 00:35
  • @knittl: Hmm nope, it's Windows 7 running normally with nothing else. – Danny Jan 19 '12 at 00:36
  • Whatever your doing, I hope its not going live. Windows SUCKS as a server, and there are several security issues right out of the box. Stick to a lamp server, or something else. – Adam Fowler Jan 21 '12 at 04:29
  • @AdamSack: thanks for your reply, but thankfully this is nothing that is going to a production server or anything, it's just a personal task that I'm trying to achieve, but doing it manually would take me years :P – Danny Jan 22 '12 at 03:52
  • Which version of gd2 lib have you installed? Please add the version to the list in your question- – hakre Jan 24 '12 at 11:45
  • Added the gd2 version to the list. – Danny Jan 24 '12 at 19:53
  • Could you elaborate more on the goal/task you are trying to achieve? – Bas van Ommen Jan 26 '12 at 17:12
  • @PeterVersnee: I just want to capture a screenshot of multiple web pages and save them. Screenshoting them manually would take me a lot of time than using this function with PHP. – Danny Jan 27 '12 at 04:19
  • Perhaps this topic could help you out http://stackoverflow.com/questions/183107/automated-screenshots ? Or Google's #3 http://www.labnol.org/software/automated-screenshots-of-websites-from-command-line/4786/ (http://www.google.nl/search?q=automated+screenshots&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:nl:official&client=firefox-a) – Bas van Ommen Jan 29 '12 at 08:36

3 Answers3

2

You can do it. I did this. I didn't use WAMP. I used everything separate. I have all PHP, MySQL and Apache setup.

Here are the steps.

  1. Stop the Apache server service. You can do this by invoking

    NET STOP Apache2.2
    

    or you can open the services.msc then stop it.

  2. Copy the Apache2.2 folder out of C:\. Put it somewhere where you have full access. Like Documents or in other drive. I put it in K:. To be sure you have full access,

    1. Recursively get ownership of the Apache directory.
    2. Make sure You have Full control marked tick on security tab of the Apache2.2 folder.
    3. This new Apache's configuration file httpd.conf will contain a lot of hardcoded paths. Like C:\apache software foundation\apache2.2. Just replace those with your new path. In my case it was K:\Apache2.2.
  3. At this moment your Apache Server Service should be stopped. So 80 port will not be blocked. And you'll have your own Apache at your own territory (directory).

  4. Open a console window and go to your Apache home where htdocs folder resides along with some other folders using cd

  5. Run bin\httpd.exe. This means you are running Apache. You have full access to your desktop. You can do anything, so do httpd.exe
  6. Open your web page. With following code.

    <?php
    header("Content-type: image/png");
    $im = imagegrabscreen();    
    imagepng($im);
    imagedestroy($im); 
    exit(0);
    ?>
    
  7. You'll see the image.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • I forgot to test if any of the latest answers worked, but I am gonna try tomorrow again and reward with the bounty if it works – Danny Mar 23 '12 at 08:10
1

This is from a comment on the php.net manual page for imagegrabscreen(); try it and see if it fixes the issue, it sounds like what you're running into:

For this to work your Apache service must be set to 'Allow service to interact with desktop' otherwise you will just get a blank image.

To actually make the change:

  • Run the command services.msc as Admin.
  • Find the Apache service in the list, right click and select Properties
  • Click the Log On tab
  • Change the service to use a local system account if it isn't already
  • Check the box that says Allow this service to interact with the desktop.
  • Restart the Apache service.
drew010
  • 68,777
  • 11
  • 134
  • 162
  • This is what I've done before, running services.msc as Admin, then in the Log on Tab (of the 'wampapache' service) selected the first radius and checked the box, apply, OK, and restart service and I still get a black image as screenshot. – Danny Jan 22 '12 at 04:10
0

If you are having trouble with imagegrabscreen() you may want to try a windows command line tool to capture the screen like boxcutter. Then use the PHP exec() function to call it. ex:

<?php

$exec = exec('boxcutter -f image.png'); // -f is full screen option
James L.
  • 4,032
  • 1
  • 15
  • 15