//-.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 $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); } } }