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;
|
2020-12-03 14:28:13 +00:00
|
|
|
const maxImageSize = 3000;
|
2020-10-14 19:36:37 +00:00
|
|
|
|
|
|
|
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" />
|
|
|
|
<source srcSet={this.src("jpg")} type="image/jpeg" />
|
2020-10-14 19:36:37 +00:00
|
|
|
<img
|
2021-02-22 02:39:04 +00:00
|
|
|
src={this.src("jpg")}
|
2021-02-06 20:20:41 +00:00
|
|
|
alt={this.alt()}
|
2021-08-23 20:16:05 +00:00
|
|
|
loading="lazy"
|
2020-10-24 19:41:01 +00:00
|
|
|
className={`
|
2021-02-22 02:39:04 +00:00
|
|
|
${!this.props.icon && !this.props.iconOverlay && "img-fluid "}
|
2022-01-06 16:26:32 +00:00
|
|
|
${this.props.banner && "banner "}
|
2020-12-01 15:02:01 +00:00
|
|
|
${
|
2022-01-06 16:26:32 +00:00
|
|
|
this.props.thumbnail && !this.props.icon && !this.props.banner
|
2021-02-22 02:39:04 +00:00
|
|
|
? "thumbnail rounded "
|
|
|
|
: "img-expanded "
|
2021-02-06 20:20:41 +00:00
|
|
|
}
|
2021-02-22 02:39:04 +00:00
|
|
|
${this.props.thumbnail && this.props.nsfw && "img-blur "}
|
|
|
|
${this.props.icon && "rounded-circle img-icon mr-2 "}
|
|
|
|
${this.props.iconOverlay && "ml-2 mb-0 rounded-circle avatar-overlay "}
|
|
|
|
${this.props.pushup && "avatar-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
|
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
let 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let host = split[0];
|
|
|
|
let path = split[1];
|
|
|
|
|
|
|
|
let params = { format };
|
|
|
|
|
|
|
|
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-12-01 15:02:01 +00:00
|
|
|
} else {
|
2021-02-22 02:39:04 +00:00
|
|
|
params["thumbnail"] = maxImageSize;
|
2020-10-14 19:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let paramsStr = `?${new URLSearchParams(params).toString()}`;
|
|
|
|
let out = `${host}/pictrs/image/${path}${paramsStr}`;
|
|
|
|
|
|
|
|
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
|
|
|
}
|