Files
SealShare/tests/Screenshots/Publisher.php
T
Andreas Reinhold / reiniandClaude Opus 5 e49bdf3eb3 Take the website's screenshots with one command
composer screenshots runs Pest browser tests in tests/Screenshots, outside
every test suite: fixed demo data under a frozen clock, desktop (MacBook
14, 2x) and phone (iPhone 15 Pro, 3x) in light and dark, each capture
published at once as WebP at two widths into website/img/screenshots.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V9NnLxnPp8vaaurb3Z1MFy
2026-09-13 12:30:53 +02:00

57 lines
1.9 KiB
PHP

<?php
namespace Tests\Screenshots;
use GdImage;
use RuntimeException;
/**
* Turns a raw browser capture into the website's images: WebP at each width the site's `srcset`
* asks for, under `<device>/<theme>/<name>-<width>.webp`. The capture is read right after it is
* taken, because Pest empties its screenshot directory when a browser run starts.
*/
final class Publisher
{
public function __construct(
private string $capturesPath,
private string $publishPath,
private int $quality = 82,
) {}
/**
* The publisher for this project: Pest's captures into `website/img/screenshots`.
*/
public static function forProject(): self
{
return new self(base_path('tests/Browser/Screenshots'), base_path('website/img/screenshots'));
}
/**
* Write the capture as WebP at each width, keeping its aspect ratio.
*
* @param list<int> $widths
*/
public function publish(string $capture, string $device, string $theme, string $name, array $widths): void
{
$source = $this->capturesPath.'/'.$capture.'.png';
if (! is_file($source) || ! ($image = @imagecreatefrompng($source)) instanceof GdImage) {
throw new RuntimeException("The capture [{$capture}] was not written, so [{$device}/{$theme}/{$name}] cannot be published.");
}
$directory = $this->publishPath.'/'.$device.'/'.$theme;
if (! is_dir($directory)) {
mkdir($directory, 0755, true);
}
foreach ($widths as $width) {
$height = (int) round(imagesy($image) * $width / imagesx($image));
$resized = imagecreatetruecolor($width, $height);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesy($image));
imagewebp($resized, $directory.'/'.$name.'-'.$width.'.webp', $this->quality);
}
}
}