[php-src] Issue #23459: imagegrabscreen() is extremely slow

[email protected] (divinity76)
Newsgroups php.bugs
Message-ID <mq89lBt8LrAqqdBGfptAbMqeI28XM6XUlDV4KiIOCoU@main.internal.php.net>
Issue: https://github.com/php/php-src/issues/23459
Author: divinity76

### Description

imagegrabscreen is extremely slow.

The following code:
```php
<?php
$t = microtime(true);
$h = imagegrabscreen();
$grab = microtime(true) - $t;

echo 'Grab: ' . sprintf('%.9f', $grab) . " s\n";
```

Resulted in this output:
```
PS C:\Users\hansh> php test.php
Grab: 7.558355093 s
```

But I expected this output instead:
```
PS C:\Users\hansh> php test.php
Grab: 0.58355093 s
```

Php needs about 7.5 seconds. For comparison, equivalent [Python-mss](https://github.com/BoboTiG/python-mss)+PIL code runs in about 0.26 seconds:
```py
import time
import mss
from PIL import Image

# 1. Grab Screen
t = time.perf_counter()
with mss.MSS() as sct:  # <--- Updated to mss.MSS()
    # monitors[1] is the primary monitor (monitors[0] is all monitors combined)
    monitor = sct.monitors[1] 
    sct_img = sct.grab(monitor)
    
    # Convert raw MSS pixels (BGRA) to a PIL Image (RGB)
    img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
    
grab_time = time.perf_counter() - t
print(f"Grab: {grab_time:.9f} s")
```

```
PS C:\Users\hansh> python3 test.py
Grab: 0.265929600 s
```

### PHP Version

```plain
Observed both windows 11 with
PHP 8.5.1 (cli) (built: Dec 17 2025 10:55:54) (ZTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.5.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.1, Copyright (c), by Zend Technologies

and Windows Server 2025 with
PHP 8.2.12 (cli) (built: Oct 24 2023 21:15:15) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies
```

### Operating System

Observed on both Windows Server 2025, and Windows 11, both x86-64
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.