fix(photos): รูปโรงหนังแตก — mirror รูปภายนอกมาเก็บในโปรเจกต์ + fallback

สาเหตุ: photos[] อ้างลิงก์ hotlink (googleusercontent/gstatic/fbcdn ฯลฯ)
ซึ่งตอบ 200 แต่ 0 ไบต์เมื่อลิงก์หมดอายุ/ถูกจำกัด rate → รูปแตกเงียบ ๆ
เช่น /cinema/phahol-theatre

- scripts/mirror-photos.mjs: ดาวน์โหลด 20 รูปจาก 12 โรง มาเก็บ public/images/cinemas
  แล้วแก้ content.json ชี้ path ในโปรเจกต์ (สำรอง .bak-photos-* ก่อนเขียน) → เหลือรูปภายนอก 0
- บีบอัดรูปใหญ่ (<=1600px, q82): 6.12 MB → 2.91 MB
- PhotoImage: img + onError fallback เป็นโลโก้ (ใช้ใน CinemaCard/Gallery) กันรูปแตกทุกกรณี
- scripts/audit-photos.mjs: ตรวจรูปเสีย exit 1
- DESIGN.md: กฎรูปภาพ (ห้าม hotlink, ต้องใช้ PhotoImage, เพดานขนาด)
This commit is contained in:
Bkk1234 committed 2026-11-15 22:32:52 +07:00
1 parent f3798e40da
commit 251932e548
27 files changed
+300 -15

No files matched your search

+5 -12
View File
@@ -2,6 +2,7 @@ import { IconChevronLeft, IconChevronRight } from "@tabler/icons-react";
import { useState, type ReactNode } from "react";
import { Logo } from "@/components/site/Logo";
import { PhotoImage } from "@/components/site/PhotoImage";
import { rotateGallery, splitGallery } from "@/lib/gallery";
import { useI18n } from "@/lib/i18n";
@@ -19,21 +20,13 @@ type GallerySlotProps = {
};
function GallerySlot({ photo, alt, placeholder, className }: GallerySlotProps) {
if (!photo) {
return (
<div
className={`flex items-center justify-center bg-[#242424] text-sm text-[#999] ${className}`}
>
{placeholder}
</div>
);
}
return (
<img
<PhotoImage
src={photo}
alt={alt}
className={`bg-[#242424] object-cover ${className}`}
size="sm"
className={className}
fallback={placeholder}
/>
);
}
+3 -3
View File
@@ -5,6 +5,7 @@ import {
} from "@tabler/icons-react";
import { Link } from "react-router";
import { PhotoImage } from "@/components/site/PhotoImage";
import { ShareButton } from "@/components/site/ShareButton";
import { useNearMe } from "@/hooks/use-near-me";
import { useReveal } from "@/hooks/use-reveal";
@@ -60,11 +61,10 @@ export function CinemaCard({
>
{cover ? (
<>
<img
<PhotoImage
src={cover}
alt=""
aria-hidden="true"
loading="lazy"
lazy
className="absolute inset-0 size-full object-cover transition-transform duration-700 ease-out group-hover:scale-[1.06]"
/>
{/* ไล่เงาเพื่อให้ตัวอักษรบนการ์ดอ่านได้ */}
+61
View File
@@ -0,0 +1,61 @@
import { useState, type ReactNode } from "react";
import { Logo } from "@/components/site/Logo";
type PhotoImageProps = {
/** URL รูป — ว่าง/null = แสดง fallback ทันที */
src: string | null | undefined;
alt: string;
className?: string;
/** เนื้อหาที่แสดงแทนเมื่อไม่มีรูปหรือรูปโหลดไม่สำเร็จ (ค่าเริ่มต้น = โลโก้) */
fallback?: ReactNode;
/** ขนาดโลโก้ตอนใช้ fallback */
size?: "sm" | "md";
/** โหลดแบบ lazy (ปิดได้ถ้าเป็นรูป hero ที่ต้องเห็นทันที) */
lazy?: boolean;
};
/**
* รูปภาพที่มี fallback ในตัว
*
* ทำไมต้องมี: ข้อมูลโรงหนังอ้างรูปจากภายนอก (เช่น Google Maps) ซึ่งลิงก์หมดอายุได้
* และบางโฮสต์ตอบ **200 แต่ 0 ไบต์** → เบราว์เซอร์ขึ้นรูปแตก หน้าเว็บดูพัง
* คอมโพเนนต์นี้จะสลับไปใช้โลโก้แทนทันทีที่ `onError` ทำงาน (จำ src ที่พังไว้
* เพื่อให้เปลี่ยนรูปใหม่แล้วลองโหลดได้อีกครั้ง)
*/
export function PhotoImage({
src,
alt,
className = "",
fallback,
size = "md",
lazy = true,
}: PhotoImageProps) {
const [failedSrc, setFailedSrc] = useState<string | null>(null);
const usable =
typeof src === "string" && src.trim() !== "" && failedSrc !== src;
if (!usable) {
return (
<div
className={`flex items-center justify-center bg-[#242424] ${className}`}
role="img"
aria-label={alt}
>
{fallback ?? <Logo size={size} />}
</div>
);
}
return (
<img
src={src}
alt={alt}
className={`bg-[#242424] object-cover ${className}`}
loading={lazy ? "lazy" : "eager"}
decoding="async"
referrerPolicy="no-referrer"
onError={() => setFailedSrc(src)}
/>
);
}