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;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\App;
|
||||||
use Spatie\Browsershot\Browsershot;
|
use Spatie\Browsershot\Browsershot;
|
||||||
|
|
||||||
class BrowsershotService
|
class BrowsershotService
|
||||||
@@ -9,29 +10,29 @@ class BrowsershotService
|
|||||||
public function crawlHtml(string $url, array $options = []): string
|
public function crawlHtml(string $url, array $options = []): string
|
||||||
{
|
{
|
||||||
$browsershot = $this->configureBrowsershot($url, $options);
|
$browsershot = $this->configureBrowsershot($url, $options);
|
||||||
|
|
||||||
return $browsershot->bodyHtml();
|
return $browsershot->bodyHtml();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function takeScreenshot(string $url, array $options = []): array
|
public function takeScreenshot(string $url, array $options = []): array
|
||||||
{
|
{
|
||||||
$browsershot = $this->configureBrowsershot($url, $options);
|
$browsershot = $this->configureBrowsershot($url, $options);
|
||||||
|
|
||||||
// Configure viewport for screenshots
|
// Configure viewport for screenshots
|
||||||
$width = $options['viewport_width'] ?? 1920;
|
$width = $options['viewport_width'] ?? 1920;
|
||||||
$height = $options['viewport_height'] ?? 1080;
|
$height = $options['viewport_height'] ?? 1080;
|
||||||
$browsershot->windowSize($width, $height);
|
$browsershot->windowSize($width, $height);
|
||||||
|
|
||||||
// Always use WebP format
|
// Always use WebP format
|
||||||
$quality = $options['quality'] ?? 90;
|
$quality = $options['quality'] ?? 90;
|
||||||
$browsershot->setScreenshotType('webp', $quality);
|
$browsershot->setScreenshotType('webp', $quality);
|
||||||
|
|
||||||
$tempPath = storage_path("temp_screenshot_webp." . time() . '.webp');
|
$tempPath = storage_path("temp_screenshot_webp." . time() . '.webp');
|
||||||
$browsershot->save($tempPath);
|
$browsershot->save($tempPath);
|
||||||
|
|
||||||
$imageData = file_get_contents($tempPath);
|
$imageData = file_get_contents($tempPath);
|
||||||
unlink($tempPath);
|
unlink($tempPath);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'data' => $imageData,
|
'data' => $imageData,
|
||||||
'mime_type' => 'image/webp',
|
'mime_type' => 'image/webp',
|
||||||
@@ -43,26 +44,30 @@ public function takeScreenshot(string $url, array $options = []): array
|
|||||||
private function configureBrowsershot(string $url, array $options = []): Browsershot
|
private function configureBrowsershot(string $url, array $options = []): Browsershot
|
||||||
{
|
{
|
||||||
$browsershot = Browsershot::url($url);
|
$browsershot = Browsershot::url($url);
|
||||||
|
|
||||||
|
if (App::environment('production')) {
|
||||||
|
$browsershot->noSandbox();
|
||||||
|
}
|
||||||
|
|
||||||
// Basic configuration
|
// Basic configuration
|
||||||
if (isset($options['timeout'])) {
|
if (isset($options['timeout'])) {
|
||||||
$browsershot->timeout($options['timeout']);
|
$browsershot->timeout($options['timeout']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['delay'])) {
|
if (isset($options['delay'])) {
|
||||||
$browsershot->setDelay($options['delay']);
|
$browsershot->setDelay($options['delay']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['wait_until_network_idle']) && $options['wait_until_network_idle']) {
|
if (isset($options['wait_until_network_idle']) && $options['wait_until_network_idle']) {
|
||||||
$browsershot->waitUntilNetworkIdle();
|
$browsershot->waitUntilNetworkIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply ad/tracker blocking
|
// Apply ad/tracker blocking
|
||||||
if (($options['block_ads'] ?? true) || ($options['block_trackers'] ?? true)) {
|
if (($options['block_ads'] ?? true) || ($options['block_trackers'] ?? true)) {
|
||||||
$easyListService = new EasyListService();
|
$easyListService = new EasyListService();
|
||||||
$blockedDomains = $easyListService->getBlockedDomains($url);
|
$blockedDomains = $easyListService->getBlockedDomains($url);
|
||||||
$blockedUrls = $easyListService->getBlockedUrls($url);
|
$blockedUrls = $easyListService->getBlockedUrls($url);
|
||||||
|
|
||||||
if (!empty($blockedDomains)) {
|
if (!empty($blockedDomains)) {
|
||||||
$browsershot->blockDomains($blockedDomains);
|
$browsershot->blockDomains($blockedDomains);
|
||||||
}
|
}
|
||||||
@@ -70,7 +75,7 @@ private function configureBrowsershot(string $url, array $options = []): Browser
|
|||||||
$browsershot->blockUrls($blockedUrls);
|
$browsershot->blockUrls($blockedUrls);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $browsershot;
|
return $browsershot;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user