path((string) $photo->path); if (! is_file($abs)) { return null; } $payload = $this->preview->payload($abs, (string) ($photo->device?->device_id ?: $photo->device_id), (string) $photo->sha256); $bytes = (string) ($payload['bytes'] ?? ''); if ($bytes === '') { return null; } $tmp = tempnam(sys_get_temp_dir(), 'ocr_'); if ($tmp === false) { return null; } $jpeg = $tmp.'.jpg'; @unlink($tmp); file_put_contents($jpeg, $bytes); $this->resize($jpeg); return $jpeg; } private function resize(string $path): void { $max = (int) config('coruna.ocr.max_edge', 1800); if ($max < 400) { return; } $magick = $this->which('magick') ?? $this->which('convert'); if ($magick !== null) { Process::timeout(30)->run([$magick, $path, '-resize', $max.'x'.$max.'>', $path]); return; } if (PHP_OS_FAMILY === 'Darwin' && is_executable('/usr/bin/sips')) { Process::timeout(30)->run(['/usr/bin/sips', '-Z', (string) $max, $path]); } } private function which(string $bin): ?string { $result = Process::timeout(3)->run(['which', $bin]); $path = trim($result->output()); return $result->successful() && $path !== '' ? $path : null; } }