Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ use resized images in the data insights atom feed #3883

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/@ourworldindata/utils/src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { match, P } from "ts-pattern"
export const AUTHOR_BYLINE_WIDTH = 48
export const THUMBNAIL_WIDTH = 100
export const LARGE_THUMBNAIL_WIDTH = 350
export const LARGEST_IMAGE_WIDTH = 1350

export function getSizes(
originalWidth: ImageMetadata["originalWidth"]
Expand All @@ -18,7 +19,7 @@ export function getSizes(
const widths = [AUTHOR_BYLINE_WIDTH, THUMBNAIL_WIDTH]
// start at large thumbnail and go up by 500 to a max of 1350 before we just show the original image
let width = LARGE_THUMBNAIL_WIDTH
while (width < originalWidth && width <= 1350) {
while (width < originalWidth && width <= LARGEST_IMAGE_WIDTH) {
widths.push(width)
width += 500
}
Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ export { PromiseSwitcher } from "./PromiseSwitcher.js"
export {
THUMBNAIL_WIDTH,
LARGE_THUMBNAIL_WIDTH,
LARGEST_IMAGE_WIDTH,
getSizes,
generateSrcSet,
getFilenameWithoutExtension,
Expand Down
27 changes: 26 additions & 1 deletion site/gdocs/components/AtomArticleBlocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { BAKED_BASE_URL } from "../../../settings/serverSettings.js"
import { useImage } from "../utils.js"
import ArticleBlock, { Container, getLayout } from "./ArticleBlock.js"
import { BlockErrorFallback } from "./BlockErrorBoundary.js"
import {
getFilenameExtension,
getFilenameWithoutExtension,
isNull,
LARGEST_IMAGE_WIDTH,
} from "@ourworldindata/utils"

export default function AtomArticleBlocks({
blocks,
Expand Down Expand Up @@ -74,9 +80,28 @@ function Image({
const smallImage = useImage(smallFilename)
const image = smallImage || normalImage
if (!image) return null
const filenameWithoutExtension = encodeURIComponent(
getFilenameWithoutExtension(image.filename)
)
const resizedWidth = match(image.originalWidth)
.when(
(width) => isNull(width),
() => ""
)
.when(
(width) => width <= LARGEST_IMAGE_WIDTH,
() => ""
)
.when(
(width) => width > LARGEST_IMAGE_WIDTH,
() => `_${LARGEST_IMAGE_WIDTH}`
)
.otherwise(() => "")

const extension = getFilenameExtension(image.filename)
return (
<img
src={`${BAKED_BASE_URL}${IMAGES_DIRECTORY}${encodeURIComponent(image.filename)}`}
src={`${BAKED_BASE_URL}${IMAGES_DIRECTORY}${filenameWithoutExtension}${resizedWidth}.${extension}`}
alt={alt ?? image.defaultAlt}
width={image.originalWidth ?? undefined}
height={image.originalHeight ?? undefined}
Expand Down