4 // Script to create a new blogpost, with a cover image created at the right size
5 // and attribution ready set.
7 $siteDir = '/home/dan/web/bs-site';
9 $key = getenv('UNSPLASH_ACCESS_KEY');
11 error("Unsplash access key needs to be set on [UNSPLASH_ACCESS_KEY] environment variable to run this script");
14 $unsplashId = read("Enter the unsplash image ID:");
15 $imageDesc = read("Enter a short one/two word description of the image:");
16 $version = read("Enter the BookStack version (eg. v21.03 or v21.12.3):");
18 // Fetch and write out image
19 $image = unsplashGet($unsplashId, $key);
20 $imageContent = getImageContent($image, 2100, 800, 60);
21 $imageOutName = str_replace(' ', '-', strtolower($imageDesc)) . "-" . $image->user->username . '.jpg';
22 $imageOutPath = implode(DIRECTORY_SEPARATOR, [$siteDir, "static/images/blog-cover-images", $imageOutName]);
23 file_put_contents($imageOutPath, $imageContent);
26 $hyphenVersion = str_replace('.', '-', trim($version));
27 $postMdName = "bookstack-release-{$hyphenVersion}.md";
28 $postMdOutPath = implode(DIRECTORY_SEPARATOR, [$siteDir, "content/blog", $postMdName]);
29 $postMdContent = getPostMarkdown();
30 file_put_contents($postMdOutPath, $postMdContent);
32 output("Done, Post created at {$postMdOutPath}");
34 function unsplashGet(string $id, string $unsplashKey) {
35 $json = file_get_contents("https://api.unsplash.com/photos/{$id}?client_id={$unsplashKey}");
36 return json_decode($json);
39 function getImageContent($unsplashImage, int $width, int $height, int $quality) {
40 $url = $unsplashImage->urls->raw;
41 $url .= "&fm=jpg&w={$width}&h={$height}&fit=crop&q={$quality}&crop=focalpoint,center";
42 return file_get_contents($url);
45 function getPostMarkdown() {
46 global $image, $imageContent, $version, $hyphenVersion, $imageOutName;
47 $date = str_replace('+00:00', 'Z', date('c'));
48 $content = <<<POSTCONTENT
50 categories = ["Releases"]
52 title = "BookStack Release $version"
55 image = "/images/blog-cover-images/$imageOutName"
56 slug = "bookstack-release-$hyphenVersion"
62 * [Update instructions](https://www.bookstackapp.com/docs/admin/updates)
63 * [GitHub release page](https://github.com/BookStackApp/BookStack/releases/tag/$version)
81 - Username - *Language*
87 ### Full List of Changes
89 **Released in $version**
95 <span style="font-size: 0.8em;opacity:0.9;">Header Image Credits: <span>Photo by <a href="https://unsplash.com/@{$image->user->username}?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">{$image->user->name}</a> on <a href="https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a></span></span>
100 function error($text) {
105 function output($text) {
109 function read($question): string {
110 $response = readline($question);
112 error("Failed to respond to question (" . $question . ")");