Update
This commit is contained in:
16
SETUP.md
Normal file
16
SETUP.md
Normal file
@@ -0,0 +1,16 @@
|
||||
# Check if node and npm are installed
|
||||
|
||||
node -v && npm -v
|
||||
|
||||
# Install puppeteer
|
||||
|
||||
sudo npm install -g puppeteer
|
||||
|
||||
# Install chromium
|
||||
|
||||
npx puppeteer browsers install chrome
|
||||
|
||||
# Install dependencies
|
||||
|
||||
sudo apt update
|
||||
sudo apt install libx11-xcb1 libxcomposite1 libasound2t64 libatk1.0-0 libatk-bridge2.0-0 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates install libnss3
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Spatie\Browsershot\Browsershot;
|
||||
|
||||
class BrowsershotService
|
||||
@@ -9,29 +10,29 @@ class BrowsershotService
|
||||
public function crawlHtml(string $url, array $options = []): string
|
||||
{
|
||||
$browsershot = $this->configureBrowsershot($url, $options);
|
||||
|
||||
|
||||
return $browsershot->bodyHtml();
|
||||
}
|
||||
|
||||
public function takeScreenshot(string $url, array $options = []): array
|
||||
{
|
||||
$browsershot = $this->configureBrowsershot($url, $options);
|
||||
|
||||
|
||||
// Configure viewport for screenshots
|
||||
$width = $options['viewport_width'] ?? 1920;
|
||||
$height = $options['viewport_height'] ?? 1080;
|
||||
$browsershot->windowSize($width, $height);
|
||||
|
||||
|
||||
// Always use WebP format
|
||||
$quality = $options['quality'] ?? 90;
|
||||
$browsershot->setScreenshotType('webp', $quality);
|
||||
|
||||
|
||||
$tempPath = storage_path("temp_screenshot_webp." . time() . '.webp');
|
||||
$browsershot->save($tempPath);
|
||||
|
||||
|
||||
$imageData = file_get_contents($tempPath);
|
||||
unlink($tempPath);
|
||||
|
||||
|
||||
return [
|
||||
'data' => $imageData,
|
||||
'mime_type' => 'image/webp',
|
||||
@@ -43,26 +44,30 @@ public function takeScreenshot(string $url, array $options = []): array
|
||||
private function configureBrowsershot(string $url, array $options = []): Browsershot
|
||||
{
|
||||
$browsershot = Browsershot::url($url);
|
||||
|
||||
|
||||
if (App::environment('production')) {
|
||||
$browsershot->noSandbox();
|
||||
}
|
||||
|
||||
// Basic configuration
|
||||
if (isset($options['timeout'])) {
|
||||
$browsershot->timeout($options['timeout']);
|
||||
}
|
||||
|
||||
|
||||
if (isset($options['delay'])) {
|
||||
$browsershot->setDelay($options['delay']);
|
||||
}
|
||||
|
||||
|
||||
if (isset($options['wait_until_network_idle']) && $options['wait_until_network_idle']) {
|
||||
$browsershot->waitUntilNetworkIdle();
|
||||
}
|
||||
|
||||
|
||||
// Apply ad/tracker blocking
|
||||
if (($options['block_ads'] ?? true) || ($options['block_trackers'] ?? true)) {
|
||||
$easyListService = new EasyListService();
|
||||
$blockedDomains = $easyListService->getBlockedDomains($url);
|
||||
$blockedUrls = $easyListService->getBlockedUrls($url);
|
||||
|
||||
|
||||
if (!empty($blockedDomains)) {
|
||||
$browsershot->blockDomains($blockedDomains);
|
||||
}
|
||||
@@ -70,7 +75,7 @@ private function configureBrowsershot(string $url, array $options = []): Browser
|
||||
$browsershot->blockUrls($blockedUrls);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $browsershot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user