tempManager = $tempManager; $this->logger = $logger; $this->config = $config; } /** * @param Bookmark|null $bookmark * * @return Image|null */ public function getImage($bookmark): ?IImage { if (!isset($bookmark)) { return null; } $serverPath = self::getPageresPath(); if ($serverPath === null) { return null; } $url = $bookmark->getUrl(); // Fetch image from remote server return $this->fetchImage($serverPath, $url); } /** * @param string $serverPath * @param string $url * * @return Image * * @throws Exception */ protected function fetchImage(string $serverPath, string $url): Image { $tempPath = $this->tempManager->getTemporaryFile('.png'); $tempDir = dirname($tempPath); $tempFile = basename($tempPath, '.png'); $command = $serverPath; $escapedUrl = escapeshellarg($url); $env = $this->config->getAppValue('bookmarks', 'previews.pageres.env'); $cmd = "cd {$tempDir} && {$env} {$command} {$escapedUrl} 1024x768" . ' --delay=4 --filename=' . escapeshellarg($tempFile) . ' --crop --overwrite 2>&1'; $retries = 0; $output = []; while ($retries < self::CAPTURE_MAX_RETRIES) { $output = []; @exec($cmd, $output, $returnCode); if ($returnCode === 0 && is_file($tempPath)) { $content = file_get_contents($tempPath); unlink($tempPath); return new Image('image/png', $content); } else { $this->logger->debug('Executing pageres failed'); $this->logger->debug(implode("\n", $output)); } $retries++; } throw new Exception("Pageres Error\nCommand: {$cmd}\nOutput: " . implode(' ' . PHP_EOL, $output) . PHP_EOL); } /** * @return null|string */ public static function getPageresPath(): ?string { $serverPath = @exec('which pageres'); if (!empty($serverPath) && is_readable($serverPath)) { return $serverPath; } return null; } }