fix: optimize thumbnail image urls
This commit is contained in:
1 parent
62044ce3b7
commit
8b82a782f5
4 files changed
+21
-9
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
import Link, { parseURL } from '$lib/components/input/Link.svelte'
|
import Link, { parseURL } from '$lib/components/input/Link.svelte'
|
||||||
import { Material } from 'mono-svelte'
|
import { Material } from 'mono-svelte'
|
||||||
import { Icon, Link as LinkIcon } from 'svelte-hero-icons'
|
import { Icon, Link as LinkIcon } from 'svelte-hero-icons'
|
||||||
|
import { optimizeImageURL } from './helpers'
|
||||||
|
|
||||||
export let url: string
|
export let url: string
|
||||||
export let thumbnail_url: string | undefined = undefined
|
export let thumbnail_url: string | undefined = undefined
|
||||||
@@ -39,10 +40,10 @@
|
|||||||
{#if thumbnail_url}
|
{#if thumbnail_url}
|
||||||
<a
|
<a
|
||||||
href={url}
|
href={url}
|
||||||
class="ml-auto w-full thumbnail rounded-r-lg overflow-hidden flex-shrink -m-4"
|
class="ml-auto w-full thumbnail rounded-r-lg overflow-hidden flex-shrink -m-4 max-h-48"
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={thumbnail_url}
|
src={optimizeImageURL(thumbnail_url, 256)}
|
||||||
class="w-full h-full object-cover bg-slate-200 dark:bg-zinc-800"
|
class="w-full h-full object-cover bg-slate-200 dark:bg-zinc-800"
|
||||||
width={600}
|
width={600}
|
||||||
height={400}
|
height={400}
|
||||||
@@ -57,7 +58,7 @@
|
|||||||
{#if thumbnail_url && !compact}
|
{#if thumbnail_url && !compact}
|
||||||
<a href={url}>
|
<a href={url}>
|
||||||
<img
|
<img
|
||||||
src={thumbnail_url}
|
src={optimizeImageURL(thumbnail_url, 256)}
|
||||||
class="rounded-md max-w-[300px] w-full h-auto aspect-video object-cover bg-slate-200 dark:bg-zinc-800"
|
class="rounded-md max-w-[300px] w-full h-auto aspect-video object-cover bg-slate-200 dark:bg-zinc-800"
|
||||||
width={600}
|
width={600}
|
||||||
height={400}
|
height={400}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { getInstance } from '$lib/lemmy.js'
|
import { getInstance } from '$lib/lemmy.js'
|
||||||
import type { View } from '$lib/settings'
|
import type { View } from '$lib/settings'
|
||||||
import { isImage, isVideo } from '$lib/ui/image'
|
import { isImage, isVideo } from '$lib/ui/image'
|
||||||
|
import { findClosestNumber } from '$lib/util'
|
||||||
import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client'
|
import type { CommentView, PersonView, Post, PostView } from 'lemmy-js-client'
|
||||||
|
|
||||||
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
|
export const isCommentMutable = (comment: CommentView, me: PersonView) =>
|
||||||
@@ -27,6 +28,20 @@ export const bestImageURL = (
|
|||||||
return post.url ?? ''
|
return post.url ?? ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const optimizeImageURL = (
|
||||||
|
urlStr: string,
|
||||||
|
width: number = 1024
|
||||||
|
): string => {
|
||||||
|
try {
|
||||||
|
const url = new URL(urlStr)
|
||||||
|
url.searchParams.append('format', 'webp')
|
||||||
|
url.searchParams.append('thumbnail', findClosestNumber([128, 256, 512, 1024], width).toString())
|
||||||
|
return url.toString()
|
||||||
|
} catch (e) {
|
||||||
|
return urlStr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const YOUTUBE_REGEX = /^(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
|
const YOUTUBE_REGEX = /^(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
|
||||||
|
|
||||||
export const isYoutubeLink = (url?: string): RegExpMatchArray | null => {
|
export const isYoutubeLink = (url?: string): RegExpMatchArray | null => {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
VideoCamera,
|
VideoCamera,
|
||||||
type IconSource,
|
type IconSource,
|
||||||
} from 'svelte-hero-icons'
|
} from 'svelte-hero-icons'
|
||||||
import type { IframeType, MediaType } from '../helpers'
|
import { optimizeImageURL, type IframeType, type MediaType } from '../helpers'
|
||||||
import { userSettings } from '$lib/settings'
|
import { userSettings } from '$lib/settings'
|
||||||
|
|
||||||
export let type: IframeType = 'none'
|
export let type: IframeType = 'none'
|
||||||
@@ -102,7 +102,7 @@
|
|||||||
>
|
>
|
||||||
{#if thumbnail}
|
{#if thumbnail}
|
||||||
<img
|
<img
|
||||||
src={thumbnail}
|
src={optimizeImageURL(thumbnail, 512)}
|
||||||
class="absolute top-0 left-0 -z-10 brightness-[25%] w-full object-cover h-full"
|
class="absolute top-0 left-0 -z-10 brightness-[25%] w-full object-cover h-full"
|
||||||
alt=""
|
alt=""
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -11,17 +11,13 @@
|
|||||||
showImage,
|
showImage,
|
||||||
} from '$lib/components/ui/ExpandableImage.svelte'
|
} from '$lib/components/ui/ExpandableImage.svelte'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
import { isImage, isVideo } from '$lib/ui/image'
|
|
||||||
import type { Post } from 'lemmy-js-client'
|
import type { Post } from 'lemmy-js-client'
|
||||||
import { Icon, VideoCamera } from 'svelte-hero-icons'
|
|
||||||
import PostIframe from './PostIframe.svelte'
|
import PostIframe from './PostIframe.svelte'
|
||||||
|
|
||||||
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
|
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
|
||||||
export let post: Post
|
export let post: Post
|
||||||
export let type: MediaType = 'none'
|
export let type: MediaType = 'none'
|
||||||
export let opened: boolean | undefined = undefined
|
export let opened: boolean | undefined = undefined
|
||||||
|
|
||||||
console.log
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if type == 'image'}
|
{#if type == 'image'}
|
||||||
|
|||||||
Reference in new issue
Block a user