Skip to content

Commit

Permalink
Automatically Resize
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Oct 17, 2024
1 parent 3c15585 commit 07bb1e4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
25 changes: 16 additions & 9 deletions api/lib/sprites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Vinyl from 'vinyl';
import { promisify } from 'node:util'
import { Static } from '@sinclair/typebox';
import { IconResponse } from './types.js';
import Sharp from 'sharp';

const SpriteSmith = promisify(spritesmith.run);

Expand All @@ -12,15 +13,21 @@ type SpriteConfig = {
};

export default async function(icons: Array<Static<typeof IconResponse>>, config: SpriteConfig = {}) {
const doc = await SpriteSmith({
src: icons.map((icon) => {
return new Vinyl({
// @ts-expect-error Deal with indexing issue on icon
path: config.name ? icon[config.name] + '.png' : icon.path.replace(/.*?\//, ''),
contents: Buffer.from(config.useDataAlt && icon.data_alt ? icon.data_alt : icon.data, 'base64'),
})
})
});
const src = [];
for (const icon of icons) {
const contents = await Sharp(Buffer.from(config.useDataAlt && icon.data_alt ? icon.data_alt : icon.data, 'base64'))
.resize(32)
.png()
.toBuffer();

src.push(new Vinyl({
// @ts-expect-error Deal with indexing issue on icon
path: config.name ? icon[config.name] + '.png' : icon.path.replace(/.*?\//, ''),
contents
}))
}

const doc = await SpriteSmith({ src });

const coords: Record<string, any> = {};
for (const key in doc.coordinates) {
Expand Down
18 changes: 15 additions & 3 deletions api/routes/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ export default async function router(schema: Schema, config: Config) {
}),
query: Type.Object({
token: Type.Optional(Type.String()),
alt: Type.Boolean({
default: false,
description: 'Use alternate icon if possible'
})
}),
description: 'Icon Data',
}, async (req, res) => {
Expand All @@ -493,7 +497,11 @@ export default async function router(schema: Schema, config: Config) {
(${req.params.iconset} = iconset AND ${req.params.icon} = name)
`);

res.status(200).send(Buffer.from(icon.data, 'base64'));
if (req.query.alt && icon.data_alt) {
res.status(200).send(Buffer.from(icon.data, 'base64'));
} else {
res.status(200).send(Buffer.from(icon.data, 'base64'));
}
} catch (err) {
Err.respond(err, res);
}
Expand Down Expand Up @@ -535,7 +543,9 @@ export default async function router(schema: Schema, config: Config) {
`
})

const sprites = await Sprites(icons.items);
const sprites = await Sprites(icons.items, {
useDataAlt: true
});

SpriteMap[req.query.iconset] = { image: sprites.image, json: sprites.json };

Expand Down Expand Up @@ -582,7 +592,9 @@ export default async function router(schema: Schema, config: Config) {
AND ${scope}
`
})
const sprites = await Sprites(icons.items);
const sprites = await Sprites(icons.items, {
useDataAlt: true
});

SpriteMap[req.query.iconset] = { image: sprites.image, json: sprites.json };

Expand Down
2 changes: 1 addition & 1 deletion api/web/src/components/CloudTAK/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ export default {
// Eventually make a sprite URL part of the overlay so KMLs can load a sprite package
const iconsets = await std('/api/iconset');
for (const iconset of iconsets.items) {
mapStore.map.addSprite(iconset.uid, String(stdurl(`/api/icon/sprite?token=${localStorage.token}&iconset=${iconset.uid}`)))
mapStore.map.addSprite(iconset.uid, String(stdurl(`/api/icon/sprite?token=${localStorage.token}&iconset=${iconset.uid}&alt=true`)))
}
await mapStore.initOverlays();
Expand Down

0 comments on commit 07bb1e4

Please sign in to comment.