开发者

PHP imagegrabwindow GD Library returns only black images

I am hoping that there is someone that can help me and many others with this problem...

I want to create a screenshot of a website using the imagegrabwindow function part of php and the GD library

After spending the whole day implementing the code I finally can capture the image and save it to a file, but the image is black and not the image that I expected...

On further investigation I have learned that one of the reasons why the image is black is because apache is not turned on to interact with the desktop - and because of this creates a black image - The issue is I am not running Apache as a server I am running IIS 6...

Is there a setting in Services that I should allow to interact with the desktop? and if there is can somebody point me in the right direction?

<?php
    $browser = new COM("InternetExplorer.Application");
    $handle = $browser->HWND;
    $browser->Visible = true;
    $browser->Fullscreen = true;
    $browser->Navigate("http://localhost/site/advertise/index.asp");

    /* Still working? */
    while ($browser->Busy) {
        com_message_pump(4000);
    }
    $im = imagegrabwindow($handle, 0);
    imagepng($im, "iesnap6.jpg");
    $browser->Quit();
    ?> 

Thanks to the answers below I have changed imagepng to imagejpeg - But still get a black image...

imagejpeg($im, "iesnap7.jpg");

Any help would be much appreciated!

Thanks

I have ammended the code to this: Social Addict still no success with the i开发者_开发百科mage showing up in Black!

  <?php
 header('Content-type: image/jpeg');
$browser = new COM("InternetExplorer.Application");
$handle = $browser->HWND;
$browser->Visible = true;
$browser->Navigate("http://localhost/advertise/index.asp");

/* Still working? */
while ($browser->Busy) {
    com_message_pump(4000);
}
$im = imagegrabwindow($handle, 0);
$browser->Quit();
imagejpeg($im," file2.jpg");
imagedestroy($im);
?> 

Social Addict - I have added the header - I am also constantly changing the file1.jpg name to make sure it is not using a black cached images... still no luck!


I've had this problem before and is often caused when you try and load or convert an image to an incorrect type

I think it may be something to do with this line

imagepng($im, "iesnap6.jpg");

You are saving it as a png and naming it as a jpg here.

Try the following as you have stated you want jpeg

imagejpeg($im, "iesnap6.jpg"); 

instead

Try setting the header type before the imagejpeg call:

header('Content-type: image/jpeg');

then accessing the script from the browser directly and see if that works?

Also this is another known issue from PHP.NET

This function was painfully slow when I was testing it on my machine. It took about 2 or 3 seconds for it to return an image. It also fails to work if the Apache service doesn't have access to "Interact with the desktop"


Are the IIS and WWW Publishingservice allowed to interact with the desktop?

My computer -> manage -> services -> [IIS and WWWPub...] -> LoginTab -> Allow to interact with desktop


i had the same problem, try change color resolution to 32 bits and try again.


I was having problems with imagegrabwindow(). Eventually, after finding no help with the problem, I decided to see if I could script an existing page-capture plugin or extention. I found that Pearl Crescent Page Saver for Firefox is scriptable from the commandline.

http://pearlcrescent.com/products/pagesaver/doc/#commandline

The Basic version gives you less flexibility than their paid version, but the basic version met my needs. It may also work on non-Windows systems, but I haven't tried it.


I have figured out the issue a while back: The main problem were that my dedicated server did not have a 32 bit Graphics Card Installed. This caused the images to display black...

I updated my server hardware and installed a 32 bit graphics card and now it is working with no issues.


I just found an answer that probably will help most. Taken from a commentor on PHP.net: http://www.php.net/manual/en/function.imagegrabscreen.php

  1. Right click '[My] Computer' on your desktop, select 'Manage'
  2. Under the 'Services and Applications', double click Services
  3. Find your Apache service (possibly IIS) and right click, select Properties
  4. From the 'Log on' tab, make sure that 'Allow service to interact with desktop' is enabled
  5. Click 'Okay', close out, and then restart Apache (or IIS)

If you don't have this, then Apache can't communicate with your computer desktop and returns a blank image.


The 't' in 'Content-Type' needs to be capital.

Before:

header('Content-type: image/jpeg');

After:

header('Content-Type: image/jpeg');

It works for me now..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜