This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageGenerator.php
More file actions
104 lines (81 loc) · 2.55 KB
/
Copy pathImageGenerator.php
File metadata and controls
104 lines (81 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
use Nesk\Puphpeteer\Puppeteer;
require_once 'vendor/autoload.php';
require_once 'ThemeSelection.php';
// TODO: Prettify
class ImageGenerator {
private static $puppeteer = false;
private static function log ($msg) {
if (DEBUG) echo 'ImageGenerator > ' . $msg . PHP_EOL;
}
private static function saveImageFromCarbon ($url, $saveFile) {
self::log('Starting Puppeteer...');
if (!self::$puppeteer)
self::$puppeteer = new Puppeteer();
$browser = self::$puppeteer->launch();
$page = $browser->newPage();
$page->setViewport([
'width' => 1600,
'height' => 1000,
'deviceScaleFactor' => 2,
]);
self::log("Going to url $url...");
$page->goto($url);
$exportContainer = $page->querySelector('#export-container');
try {
$elementBounds = $exportContainer->boundingBox();
self::log("Saving screenshot of #export-container...");
$exportContainer->screenshot([
'path' => $saveFile,
'clip' => [
'x' => round($elementBounds['x']),
'y' => $elementBounds['y'],
'width' => $elementBounds['width'],
'height' => round($elementBounds['height']) - 1,
],
]);
} catch (Error $error) {
self::log("Carbon returned an unexpected error - not saving screenshot...");
}
$browser->close();
}
private static function getCarbonUrl ($options) {
self::log('Getting Carbon URL...');
$defaultOptions = [
't' => 'seti', // Theme
'l' => 'auto', // Language
'bg' => '#fff', // Background
'wt' => 'sharp', // Window Theme
'wc' => false, // Window Controls
'fm' => 'Fira Code', // Font Family
'fs' => '18px', // Font Size
'ln' => false, // Line Numbers
'ds' => false, // Drop Shadow
'dsyoff' => '20px', // Drop Shadow Offset
'dsblur' => '68px', // Drop Shadow Blur
'wa' => true, // Auto Adjust Width
'lh' => '133%', // Line Height
'pv' => '0px', // Padding Vertical
'ph' => '0px', // Padding Horizontal
'si' => false, // Squared Image
'wm' => false, // Watermark
'es' => '2x', // Export Size
'type' => 'png', // Export Type
'code' => 'console.log("Hello World!");'
];
$options = array_merge($defaultOptions, $options);
foreach ($options as $key => $value) {
if (is_bool($value))
$options[$key] = $value === true ? 'true' : 'false';
}
$params = http_build_query($options);
return 'https://carbon.now.sh/?' . $params;
}
public static function generateAndSaveImage($saveFile, $code, $theme) {
$url = self::getCarbonUrl([
't' => $theme,
'code' => $code
]);
self::saveImageFromCarbon($url, $saveFile);
}
}