-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dually8/add-apple-icons
- Loading branch information
Showing
14 changed files
with
1,797 additions
and
145 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import sharp from "sharp"; | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
|
||
const ICON_SIZES = [ | ||
{ size: 180, name: "apple-touch-icon.png" }, // iPhone Retina | ||
{ size: 167, name: "apple-touch-icon-167x167.png" }, // iPad Retina | ||
{ size: 152, name: "apple-touch-icon-152x152.png" }, // iPad | ||
{ size: 120, name: "apple-touch-icon-120x120.png" }, // iPhone | ||
]; | ||
|
||
async function generateImages(imagePath: string, outputDir: string) { | ||
try { | ||
await fs.mkdir(outputDir, { recursive: true }); | ||
const image = sharp(imagePath); | ||
const metadata = await image.metadata(); | ||
const maxSize = Math.max(...ICON_SIZES.map((size) => size.size)); | ||
if (metadata.width! < maxSize || metadata.height! < maxSize) { | ||
throw new Error("Image is too small"); | ||
} | ||
const generatePromises = ICON_SIZES.map(async ({ size, name }) => { | ||
const outputPath = path.join(outputDir, name); | ||
await image | ||
.clone() | ||
.resize(size, size, { | ||
fit: "cover", | ||
position: "center", | ||
}) | ||
.toFormat("png") | ||
.toFile(outputPath); | ||
console.log(`Generated: ${name} (${size}x${size})`); | ||
}); | ||
await Promise.all(generatePromises); | ||
console.log("All images generated successfully"); | ||
const metaTags = ICON_SIZES.map(({ size, name }) => { | ||
return `<link rel="apple-touch-icon" sizes="${size}x${size}" href={new URL('path/to/${name}', Astro.url)} />`; | ||
}).join("\n"); | ||
console.log("-- COPY AND PASTE THESE META TAGS INTO BaseHead.astro! --"); | ||
console.log(metaTags); | ||
console.log("-- BE SURE TO REPLACE \"path/to/\" WITH THE REAL PATH --"); | ||
console.log("-- END --"); | ||
} catch (e: Error | unknown) { | ||
console.error( | ||
"Failed to generate images", | ||
(e as Error)?.message ?? "Unknown error", | ||
); | ||
} | ||
} | ||
// Check if correct arguments are provided | ||
if (process.argv.length !== 4) { | ||
console.log( | ||
"Usage: tsx generate-apple-icons.ts path/to/image.png path/to/outputDir", | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const inputPath = process.argv[2]; | ||
const outputDir = process.argv[3]; | ||
|
||
generateImages(inputPath, outputDir); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.