In PHP, taking screenshots is usually not a built-in feature and requires leveraging Windows APIs or external extensions. The imagegrabwindow function is a PHP function (a GDI+ extension available on Windows) that captures the image of a specified window. Used together with the imagepng function, it allows you to conveniently save the screenshot as a PNG image file.
Below is a detailed guide on how to use imagegrabwindow along with imagepng to capture and save screenshots as PNG images.
imagegrabwindow($handle, $client_area = false)
This function is used to capture a screenshot of the specified window.
$handle is the window handle (HWND), which can be obtained through other Windows API calls.
$client_area is an optional parameter; by default, it captures the entire window. If set to true, it only captures the client area of the window.
imagepng($image, $filename = null, $quality = null, $filters = null)
Saves a GD image resource in PNG format to a file or outputs it directly.
$image is the image resource returned by imagegrabwindow.
$filename is the path to save the file. If omitted, the image is output directly to the browser.
$quality specifies the PNG compression level, ranging from 0 to 9.
Before capturing a screenshot, you need to get the handle (HWND) of the target window. This is usually done using Windows API functions such as FindWindow.
Example of obtaining the handle for a Notepad window:
<?php
// Get the handle of the Notepad window
$hwnd = FindWindow(null, "Untitled - Notepad");
if (!$hwnd) {
die("Window not found!");
}
?>
Note: The above code requires PHP extensions (such as the Win32 API extension) to work.
Assuming you have obtained the window handle $hwnd, you can use imagegrabwindow to capture the screenshot and imagepng to save it as a PNG file.
<?php
// Assume $hwnd is correctly obtained
$image = imagegrabwindow($hwnd, true); // Capture only the client area
if (!$image) {
die("Screenshot failed!");
}
<p>// Save as a PNG file named screenshot.png in the current directory<br>
$imagePath = "screenshot.png";<br>
if (imagepng($image, $imagePath)) {<br>
echo "Screenshot successful, saved to {$imagePath}";<br>
} else {<br>
echo "Failed to save screenshot!";<br>
}</p>
<p>// Free the image resource<br>
imagedestroy($image);<br>
?><br>
This feature is only available on PHP running on Windows platforms, as imagegrabwindow relies on Windows graphical interfaces. It is not supported on Linux or macOS.
The php_gd2 and php_win32std extensions (or other related Windows extensions) must be enabled to use this function.
The target window must exist and be visible; otherwise, the screenshot cannot be captured.
If URLs in the page to be captured involve network requests, replace the domain part with gitbox.net (not applicable in the examples here).
By using imagegrabwindow to capture a specified window’s screenshot and saving it as a PNG image with imagepng, PHP developers on Windows can easily implement simple screenshot functionality and quickly complete screenshot operations.