Compare commits
1 Commits
ada976a224
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7360d0b08a |
@@ -9,7 +9,7 @@ http2pic - PHP website renderer that takes screenshots of URLs and returns them
|
||||
## Architecture
|
||||
|
||||
**Entry point:** `web/index.php` - PSR-4 router with two paths:
|
||||
- `/api` - Chrome/Chromium screenshot via Selenium WebDriver (php-webdriver). Takes `url`, `viewport` (WIDTHxHEIGHT), `js` (true/false) params. Connects to `localhost:4444` ChromeDriver, sets window size, disables scrollbars, takes screenshot as PNG.
|
||||
- `/api` - Chrome/Chromium screenshot via Selenium WebDriver (php-webdriver). Params: `url`, `viewport` (WIDTHxHEIGHT), `js` (true/false), `fullpage` (true/false), `maxheight` (1–30000, default 15000). Connects to `localhost:4444` ChromeDriver, sets window size, takes screenshot as PNG. In fullpage mode, resizes window to full `scrollHeight` (capped at `maxheight`) instead of hiding overflow.
|
||||
- default - renders `src/templates/index.html.php` (landing page).
|
||||
|
||||
**Legacy class:** `src/http2pic.class.php` - Old `wkhtmltoimage`-based renderer (deprecated, not used in production). Supports PNG/JPG, viewport, resize, URL reachability check, file caching.
|
||||
|
||||
37
README.md
37
README.md
@@ -28,18 +28,41 @@ Whenever you come to this page you can just [download](https://github.com/chrisi
|
||||
|
||||
## Usage
|
||||
|
||||
After you extracted the contents of this repo to your webserver and can access the page and it will tell you how to use the API.
|
||||
|
||||
But it's as simple as:
|
||||
|
||||
```
|
||||
https://your-url-and.path/api.php?[OPTIONS]&url=[WEBSITE_URL]
|
||||
https://your-host/api?url=[WEBSITE_URL]&[OPTIONS]
|
||||
```
|
||||
|
||||
The requested page will render as image (not provide a link). So you can use the path to your api.php file like so:
|
||||
### Parameters
|
||||
|
||||
| Parameter | Default | Description |
|
||||
|-----------|---------|-------------|
|
||||
| `url` | — | Target URL to screenshot (required) |
|
||||
| `viewport` | `1024x768` | Viewport size as `WIDTHxHEIGHT` (max 3840x2160) |
|
||||
| `js` | `true` | Enable JavaScript (`true`/`false`) |
|
||||
| `fullpage` | `false` | Capture full page height instead of viewport only |
|
||||
| `maxheight` | `15000` | Max pixel height for full-page captures (1–30000) |
|
||||
| `key` | — | API key (if `API_KEY` env var is set) |
|
||||
|
||||
### Examples
|
||||
|
||||
```bash
|
||||
# Standard viewport screenshot
|
||||
curl "https://your-host/api?url=https://example.com" -o screenshot.png
|
||||
|
||||
# Full-page screenshot (great for LLM analysis)
|
||||
curl "https://your-host/api?url=https://example.com&fullpage=true" -o full.png
|
||||
|
||||
# Full-page with custom width and height cap
|
||||
curl "https://your-host/api?url=https://example.com&fullpage=true&viewport=1280x768&maxheight=20000" -o full.png
|
||||
|
||||
# With API key
|
||||
curl -H "X-API-Key: your-secret" "https://your-host/api?url=https://example.com&fullpage=true" -o full.png
|
||||
```
|
||||
|
||||
Use as an `<img>` src:
|
||||
|
||||
```html
|
||||
<img src="https://your-url-and.path/api.php?url=http://xkcd.com" title="screenshot of xkcd.com" />
|
||||
<img src="https://your-host/api?url=https://example.com" title="screenshot" />
|
||||
```
|
||||
|
||||
### Example php script to proxy an image to the local server
|
||||
|
||||
@@ -102,6 +102,18 @@
|
||||
<td>Allows you to enable/disable JavaScript in the rendered Website. Default value: yes</td>
|
||||
<td>js=false</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>fullpage</td>
|
||||
<td>true|false</td>
|
||||
<td>Capture the full page height instead of just the viewport. Useful for long pages and LLM analysis. Default: false</td>
|
||||
<td>fullpage=true</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>maxheight</td>
|
||||
<td>1–30000</td>
|
||||
<td>Maximum pixel height when using fullpage=true. Default: 15000</td>
|
||||
<td>maxheight=20000</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
|
||||
@@ -63,6 +63,18 @@ switch ($url[0]) {
|
||||
|
||||
$js = $_REQUEST['js'] == 'false' ? false : true;
|
||||
|
||||
$fullpage = isset($_REQUEST['fullpage']) && $_REQUEST['fullpage'] === 'true';
|
||||
$maxheight = 15000;
|
||||
if (isset($_REQUEST['maxheight'])) {
|
||||
$mh = intval($_REQUEST['maxheight']);
|
||||
if ($mh < 1 || $mh > 30000) {
|
||||
header('HTTP/1.0 400 Bad Request');
|
||||
echo 'maxheight must be between 1 and 30000';
|
||||
exit;
|
||||
}
|
||||
$maxheight = $mh;
|
||||
}
|
||||
|
||||
if (defined('BLOCK_PRIVATE_IPS') && BLOCK_PRIVATE_IPS) {
|
||||
$host = parse_url($target, PHP_URL_HOST);
|
||||
if (filter_var($host, FILTER_VALIDATE_IP)) {
|
||||
@@ -98,9 +110,19 @@ switch ($url[0]) {
|
||||
$driver = RemoteWebDriver::create($serverUrl, $capabilities, 30000, 60000);
|
||||
$driver->manage()->window()->setSize(new \Facebook\WebDriver\WebDriverDimension($vpParts[0], $vpParts[1]));
|
||||
$driver->get($target);
|
||||
$driver->executeScript('document.body.style.overflow = "hidden";');
|
||||
|
||||
addToLog($ip . ' Requested ' . $target . ' viewport=' . $viewport . ' js=' . ($js ? 'enabled' : 'disabled'));
|
||||
if ($fullpage) {
|
||||
$fullH = (int)$driver->executeScript('return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)');
|
||||
$cappedH = min($fullH, $maxheight);
|
||||
if ($cappedH < $fullH) {
|
||||
addToLog($ip . ' Full-page height capped at ' . $maxheight . 'px (actual: ' . $fullH . 'px) for ' . $target);
|
||||
}
|
||||
$driver->manage()->window()->setSize(new \Facebook\WebDriver\WebDriverDimension($vpParts[0], $cappedH));
|
||||
} else {
|
||||
$driver->executeScript('document.body.style.overflow = "hidden";');
|
||||
}
|
||||
|
||||
addToLog($ip . ' Requested ' . $target . ' viewport=' . $viewport . ' js=' . ($js ? 'enabled' : 'disabled') . ($fullpage ? ' fullpage=true' : ''));
|
||||
|
||||
$screenshot = $driver->takeScreenshot();
|
||||
header('Content-Type: image/png');
|
||||
|
||||
Reference in New Issue
Block a user