Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
57 lines (44 loc) · 1.64 KB
/
Copy pathindex.php
File metadata and controls
57 lines (44 loc) · 1.64 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
<?php
// ===== ===== ===== ===== ===== ===== ===== ===== ===== ======
//
// Run local:
// git clone https://github.com/michaelstingl/figma-preview.git
// cd figma-preview
// sed -i -e 's/XX-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/replace-with-your-personal-access-token/g' index.php
// php -S localhost:8080
//
// Return image data:
// http://localhost:8080/?file=Ui4YzD3oQcxJSbzBEr80O9eU&node-id=18:646&format=.png
//
// Return $imageUrl as text:
// http://localhost:8080/?file=Ui4YzD3oQcxJSbzBEr80O9eU&node-id=18:646&format=.debug
//
// ===== ===== ===== ===== ===== ===== ===== ===== ===== ======
$figma_personal_access_token = "XX-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
$figma_api_url = "https://api.figma.com/v1/images/";
$figma_file = isset($_GET["file"]) ? $_GET["file"] : null;
$figma_node_id = isset($_GET["node-id"]) ? $_GET["node-id"] : null;
$figma_file_type = isset($_GET["format"])? $_GET["format"] : null;
if ( $figma_file === null || $figma_node_id === null) {
die("missing params");
}
// Create a stream to add headers for HTTP GET request
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Figma-Token: " . $figma_personal_access_token
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$result = file_get_contents( $figma_api_url . $figma_file . "?ids=" . $figma_node_id, false, $context);
$json = json_decode($result, true);
$imageUrl = $json["images"][$figma_node_id];
if ($figma_file_type == ".png") {
// Return image data
$image = imagecreatefromstring(file_get_contents($imageUrl));
header('Content-Type: image/png');
imagepng($image);
} else {
echo $imageUrl;
}