Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from euglena1215/euglena1215/image-optimize2
Browse files Browse the repository at this point in the history
pre resize images
  • Loading branch information
euglena1215 authored Nov 27, 2022
2 parents d1aefb9 + c78e0b3 commit 732c22c
Show file tree
Hide file tree
Showing 64 changed files with 241 additions and 58 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"axios": "^0.25.0",
"bezier-easing": "^2.1.0",
"fastify": "^3.25.3",
"fastify-modern-images": "^0.1.2",
"fastify-sensible": "^3.1.2",
"fastify-static": "^4.5.0",
"framer-motion": "^6.2.8",
Expand Down
Binary file added public/assets/images/players/001_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/002_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/003_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/004_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/005_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/006_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/007_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/008_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/009_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/010_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/011_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/012_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/013_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/014_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/015_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/016_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/017_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/018_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/019_100x100.avif
Binary file not shown.
Binary file added public/assets/images/players/020_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/001_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/001_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/002_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/002_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/003_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/003_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/004_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/004_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/005_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/005_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/006_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/006_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/007_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/007_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/008_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/008_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/009_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/009_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/010_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/010_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/011_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/011_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/012_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/012_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/013_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/013_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/014_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/014_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/015_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/015_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/016_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/016_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/017_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/017_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/018_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/018_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/019_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/019_400x225.avif
Binary file not shown.
Binary file added public/assets/images/races/020_100x100.avif
Binary file not shown.
Binary file added public/assets/images/races/020_400x225.avif
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
import React, { useEffect, useState } from "react";
import React from "react";

/**
* アスペクト比を維持したまま、要素のコンテンツボックス全体を埋めるように拡大縮小したサイズを返す
*/

/**
* @typedef Size
* @property {number} width
* @property {number} height
*/

/** @type {(cv: Size, img: Size) => Size} */
const calcImageSize = (cv, img) => {
const constrainedHeight = cv.width * (img.height / img.width);

if (constrainedHeight >= cv.height) {
return {
height: constrainedHeight,
width: cv.width,
};
}

const constrainedWidth = cv.height * (img.width / img.height);

return {
height: cv.height,
width: constrainedWidth,
};
};

/**
* @typedef Props
* @property {string} src
Expand All @@ -38,32 +13,15 @@ const calcImageSize = (cv, img) => {

/** @type {React.VFC<Props>} */
export const TrimmedImage = ({ height, src, width }) => {
const [dataUrl, setDataUrl] = useState(null);

useEffect(() => {
const img = new Image();
img.src = src;
img.onload = () => {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;

const size = calcImageSize(
{ height: canvas.height, width: canvas.width },
{ height: img.height, width: img.width },
);

const ctx = canvas.getContext("2d");
ctx.drawImage(
img,
-(size.width - canvas.width) / 2,
-(size.height - canvas.height) / 2,
size.width,
size.height,
);
setDataUrl(canvas.toDataURL());
};
}, [height, src, width]);

return <img src={dataUrl} />;
if (width === 100 & height === 100) {
const url = src.replace('.jpg', '_100x100.avif')
return <img src={url} />;
}
if (width === 400 & height === 225) {
const url = src.replace('.jpg', '_400x225.avif')
return <img src={url} />;
}

return <img src={src} />;
};
2 changes: 2 additions & 0 deletions src/server/routes/spa.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from "path";

import compression from "@fastify/compress";
// import modernImages from "fastify-modern-images";
import fastifyStatic from "fastify-static";

/**
Expand All @@ -12,6 +13,7 @@ export const spaRoute = async (fastify) => {
root: join(__dirname, "public"),
wildcard: false,
});
// fastify.register(modernImages, { quality: 1 });

fastify.get("/favicon.ico", () => {
throw fastify.httpErrors.notFound();
Expand Down
Loading

0 comments on commit 732c22c

Please sign in to comment.