2022-01-12 00:53:23 +00:00
|
|
|
import classNames from "classnames";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { Component } from "inferno";
|
2020-10-14 19:36:37 +00:00
|
|
|
|
|
|
|
const iconThumbnailSize = 96;
|
|
|
|
const thumbnailSize = 256;
|
|
|
|
|
|
|
|
interface PictrsImageProps {
|
|
|
|
src: string;
|
2021-02-06 20:20:41 +00:00
|
|
|
alt?: string;
|
2020-10-14 19:36:37 +00:00
|
|
|
icon?: boolean;
|
2022-01-06 16:26:32 +00:00
|
|
|
banner?: boolean;
|
2020-10-14 19:36:37 +00:00
|
|
|
thumbnail?: boolean;
|
|
|
|
nsfw?: boolean;
|
|
|
|
iconOverlay?: boolean;
|
|
|
|
pushup?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PictrsImage extends Component<PictrsImageProps, any> {
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<picture>
|
2021-02-22 02:39:04 +00:00
|
|
|
<source srcSet={this.src("webp")} type="image/webp" />
|
2022-01-12 00:53:23 +00:00
|
|
|
<source srcSet={this.props.src} />
|
2021-02-22 02:39:04 +00:00
|
|
|
<source srcSet={this.src("jpg")} type="image/jpeg" />
|
2020-10-14 19:36:37 +00:00
|
|
|
<img
|
2022-01-12 00:53:23 +00:00
|
|
|
src={this.props.src}
|
2021-02-06 20:20:41 +00:00
|
|
|
alt={this.alt()}
|
2021-08-23 20:16:05 +00:00
|
|
|
loading="lazy"
|
2022-01-12 00:53:23 +00:00
|
|
|
className={classNames({
|
|
|
|
"img-fluid": !this.props.icon && !this.props.iconOverlay,
|
2022-02-24 15:31:44 +00:00
|
|
|
banner: this.props.banner,
|
|
|
|
"thumbnail rounded":
|
|
|
|
this.props.thumbnail && !this.props.icon && !this.props.banner,
|
2022-01-12 00:53:23 +00:00
|
|
|
"img-expanded slight-radius":
|
|
|
|
!this.props.thumbnail && !this.props.icon,
|
|
|
|
"img-blur": this.props.thumbnail && this.props.nsfw,
|
2023-06-12 17:00:18 +00:00
|
|
|
"rounded-circle img-cover img-icon mr-2": this.props.icon,
|
|
|
|
"ml-2 mb-0 rounded-circle img-cover avatar-overlay": this.props.iconOverlay,
|
2022-01-12 00:53:23 +00:00
|
|
|
"avatar-pushup": this.props.pushup,
|
|
|
|
})}
|
2020-10-14 19:36:37 +00:00
|
|
|
/>
|
|
|
|
</picture>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
src(format: string): string {
|
|
|
|
// sample url:
|
|
|
|
// http://localhost:8535/pictrs/image/file.png?thumbnail=256&format=jpg
|
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const split = this.props.src.split("/pictrs/image/");
|
2020-10-14 19:36:37 +00:00
|
|
|
|
|
|
|
// If theres not multiple, then its not a pictrs image
|
|
|
|
if (split.length == 1) {
|
|
|
|
return this.props.src;
|
|
|
|
}
|
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const host = split[0];
|
|
|
|
const path = split[1];
|
2020-10-14 19:36:37 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const params = { format };
|
2020-10-14 19:36:37 +00:00
|
|
|
|
|
|
|
if (this.props.thumbnail) {
|
2021-02-22 02:39:04 +00:00
|
|
|
params["thumbnail"] = thumbnailSize;
|
2020-10-14 19:36:37 +00:00
|
|
|
} else if (this.props.icon) {
|
2021-02-22 02:39:04 +00:00
|
|
|
params["thumbnail"] = iconThumbnailSize;
|
2020-10-14 19:36:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const paramsStr = new URLSearchParams(params).toString();
|
|
|
|
const out = `${host}/pictrs/image/${path}?${paramsStr}`;
|
2020-10-14 19:36:37 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2021-02-06 20:20:41 +00:00
|
|
|
|
|
|
|
alt(): string {
|
|
|
|
if (this.props.icon) {
|
2021-02-22 02:39:04 +00:00
|
|
|
return "";
|
2021-02-06 20:20:41 +00:00
|
|
|
}
|
2021-02-22 02:39:04 +00:00
|
|
|
return this.props.alt || "";
|
2021-02-06 20:20:41 +00:00
|
|
|
}
|
2020-10-14 19:36:37 +00:00
|
|
|
}
|