-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
111 lines (86 loc) · 3.07 KB
/
create.php
File metadata and controls
111 lines (86 loc) · 3.07 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
105
106
107
108
109
110
111
<?php
// Version 1.0
use FileCommitAnimator\GithubRepoExtractor;
use FileCommitAnimator\ScreenshotCreator;
use GifCreator\AnimGif;
require 'vendor/autoload.php';
function readline($prompt = null){
if($prompt){
echo $prompt;
}
$fp = fopen("php://stdin","r");
$line = rtrim(fgets($fp, 1024));
return $line;
}
if (count($argv) != 3) {
exit("Invalid arguments. ");
}
$credentials = base64_encode($argv[1] . ":" . $argv[2]);
echo "--File Details--\n";
$name = readline("Repository Owner Username: ");
$repo = readline("Repository Name: ");
$filePath = readline("File Path: ");
echo "\n";
echo "--Gif Configuration--\n";
$width = readline("Width (px): ");
while (!ctype_digit($width)) {
$width = readline("\rPlease enter an integer! Width (px): ");
}
$width = intval($width);
$height = readline("Height (px): ");
while (!ctype_digit($height)) {
$height = readline("\rPlease enter an integer! Height (px): ");
}
$height = intval($height);
$frameRate = readline("Frame rate (per second): ");
while (!ctype_digit($frameRate)) {
$frameRate = readline("\rPlease enter an integer! Frame rate (per second): ");
}
$frameRate = intval($frameRate);
echo "\n";
$extractor = new GithubRepoExtractor($name, $repo, $credentials);
$ssCreator = new ScreenshotCreator(dirname(__FILE__) . '/bin/phantomjs.exe');
echo "Retrieving commits... ";
try {
$commits = $extractor->getCommits();
} catch (Exception $e) {
exit("\nError: " . $e->getMessage() . " \n");
}
echo "done. \n";
if (!file_exists('images\\')) {
mkdir('images');
} else {
array_map('unlink', glob("images\*") ?: []);
}
$frames = array();
$durations = array();
$counter = 1;
$numOfCommits = count($commits);
foreach ($commits as $commit) {
$htmlFile = fopen("images\\frame" . $counter . ".html", "w") or exit("Unable to write file: images\\frame-" . $counter . ".html");
$content = "<!DOCTYPE html><html><body style='width:100%;height:100%;background-color:white;'>" .
"<div style='font-family:Segoe UI;color:blue;font-size:50px;position:absolute;top:0;left:15px;'>" .
$counter . "</div><div style='display:flex;align-items:center;justify-content:center;'><pre>";
try {
$content .= $extractor->getFileAtCommit($filePath, $commit);
} catch (Exception $e) { }
$content .= "</pre></div></body></html>";
fwrite($htmlFile, $content);
$htmlPath = "file:///" . str_replace('\\', '/', dirname(__FILE__)) . "/images/frame" . $counter . ".html";
$imgPath = "./images/frame". $counter . ".png";
$frames[$counter-1] = $imgPath;
$durations[$counter-1] = 100/$frameRate;
$ssCreator->createScreenshot($htmlPath, $width, $height, $imgPath);
echo "\rProgress: " . $counter . "/" . $numOfCommits . " frames completed. ";
fclose($htmlFile);
$counter += 1;
}
echo "\n";
echo "Creating gif... ";
$anim = new GifCreator\AnimGif();
$anim->create($frames, $durations);
if (!file_exists('gifs\\')) {
mkdir('gifs');
}
$anim->save("./gifs/" . date("Y-m-d h-i-sa") . ".gif");
echo "gifs/" . date("Y-m-d h-i-sa") . ".gif " . "created. \n";