switch lib
This commit is contained in:
@ -9,8 +9,7 @@ import NodeCache from "node-cache";
|
||||
import ScoreSaberLeaderboardToken from "@ssr/common/types/token/scoresaber/score-saber-leaderboard-token";
|
||||
import ScoreSaberPlayer, { getScoreSaberPlayerFromToken } from "@ssr/common/types/player/impl/scoresaber-player";
|
||||
import { Config } from "../common/config";
|
||||
import ky from "ky";
|
||||
import { createCanvas, loadImage } from "canvas";
|
||||
import { Jimp } from "jimp";
|
||||
import { extractColors } from "extract-colors";
|
||||
|
||||
const cache = new NodeCache({ stdTTL: 60 * 60, checkperiod: 120 });
|
||||
@ -29,33 +28,27 @@ export class ImageService {
|
||||
|
||||
return await this.fetchWithCache<{ color: string }>(`average_color-${src}`, async () => {
|
||||
try {
|
||||
const response = await ky.get(src);
|
||||
if (response.status !== 200) {
|
||||
throw new Error(`Failed to fetch image: ${src}`);
|
||||
const image = await Jimp.read(src); // Load image using Jimp
|
||||
const { width, height, data } = image.bitmap; // Access image dimensions and pixel data
|
||||
|
||||
// Convert the Buffer data to Uint8ClampedArray
|
||||
const uint8ClampedArray = new Uint8ClampedArray(data);
|
||||
|
||||
// Extract the colors using extract-colors
|
||||
const colors = await extractColors({ data: uint8ClampedArray, width, height });
|
||||
|
||||
// Return the most dominant color, or fallback if none found
|
||||
if (colors && colors.length > 0) {
|
||||
return { color: colors[2].hex }; // Returning the third most dominant color
|
||||
}
|
||||
|
||||
const imageBuffer = await response.arrayBuffer();
|
||||
|
||||
// Create an image from the buffer using canvas
|
||||
const img = await loadImage(Buffer.from(imageBuffer));
|
||||
const canvas = createCanvas(img.width, img.height);
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
// Draw the image onto the canvas
|
||||
ctx.drawImage(img, 0, 0);
|
||||
|
||||
// Get the pixel data from the canvas
|
||||
const imageData = ctx.getImageData(0, 0, img.width, img.height);
|
||||
const { data, width, height } = imageData;
|
||||
|
||||
// Extract the colors
|
||||
const color = await extractColors({ data, width, height });
|
||||
return {
|
||||
color: color[2].hex,
|
||||
color: "#fff", // Fallback color in case no colors are found
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching image or extracting colors:", error);
|
||||
return {
|
||||
color: "#fff",
|
||||
color: "#fff", // Fallback color in case of an error
|
||||
};
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user