PHP imagegrabwindow GD 库仅返回黑色图像

发布于 2024-08-07 19:12:18 字数 1471 浏览 3 评论 0原文

我希望有人可以帮助我和其他许多人解决这个问题...

我想使用 php 的 imagegrabwindow 函数部分和 GD 库创建一个网站的屏幕截图

在花了一整天的时间实现代码后,我终于可以捕获图像并将其保存到文件中,但是图像是黑色的,而不是我期望的图像...

经过进一步调查,我了解到图像是黑色的原因之一是因为 apache 没有打开与桌面交互 - 因此会创建黑色图像 - 问题是我没有将 Apache 作为服务器运行,我正在运行 IIS 6...

服务中是否有一个设置允许我与桌面交互?如果有人可以指出我正确的方向吗?

<?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();
    ?> 

感谢下面的答案,我已将 imagepng 更改为 imagejpeg - 但仍然得到黑色图像...

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

任何帮助将不胜感激!

谢谢,

我已将代码修改为:社交成瘾者仍然没有成功,图像以黑色显示!

  <?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 - 我已经添加了标题 - 我还不断更改 file1.jpg 名称以确保它没有使用黑色缓存图像...仍然不走运!

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 image 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半衾梦 2024-08-14 19:12:18

我以前遇到过这个问题,通常是当您尝试加载或将图像转换为不正确的类型时引起的,

我认为这可能与此行有关,

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

您将其另存为 png 并将其命名为 jpg 。

正如您所说,尝试以下操作 jpeg

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

尝试

设置在 imagejpeg 调用之前输入标头:

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

然后直接从浏览器访问脚本并查看是否有效?

这也是 PHP.NET 的另一个已知问题

这个函数在以下情况下非常慢:
我正在我的机器上测试它。它
大约需要 2 或 3 秒的时间
返回图像。它也无法工作
如果 Apache 服务没有
访问“与桌面交互”

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"

拿命拼未来 2024-08-14 19:12:18

是否允许 IIS 和 WWW Publishingservice 与桌面交互?

我的电脑 ->管理->服务-> [IIS 和 WWWPub...] ->登录选项卡 ->允许与桌面交互

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

简单爱 2024-08-14 19:12:18

我遇到了同样的问题,尝试将颜色分辨率更改为 32 位,然后重试。

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

oО清风挽发oО 2024-08-14 19:12:18

我在使用 imagegrabwindow() 时遇到问题。最终,在找不到解决问题的帮助后,我决定看看是否可以编写现有的页面捕获插件或扩展的脚本。我发现 Firefox 的 Pearl Crescent Page Saver 可以通过命令行编写脚本。

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

基本信息版本给你的灵活性不如他们的付费版本,但基本版本满足了我的需求。它也可能适用于非 Windows 系统,但我还没有尝试过。

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.

稀香 2024-08-14 19:12:18

我不久前就发现了这个问题:主要问题是我的专用服务器没有安装 32 位显卡。这导致图像显示黑色...

我更新了服务器硬件并安装了 32 位显卡,现在它可以正常工作。

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.

厌味 2024-08-14 19:12:18

我刚刚找到了一个可能最有帮助的答案。摘自 PHP.net 上的评论者:
http://www.php.net/manual/en/function.imagegrabscreen.php

  1. 右键单击​​桌面上的“[我的]计算机”,选择“管理”
  2. 在“服务和应用程序”下,双击“服务”
  3. 选择“属性”
  4. 找到您的 Apache 服务(可能是 IIS),然后右键单击,从“登录”中 ”选项卡,确保启用“允许服务与桌面交互”
  5. 单击“确定”,关闭,然后重新启动 Apache(或 IIS)。

如果没有此选项,则 Apache 无法与您的计算机桌面通信并返回一个空白图像。

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.

暮倦 2024-08-14 19:12:18

“Content-Type”中的“t”必须大写。

之前:

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

之后:

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

现在对我有用..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文