8.0 KiB
📸 Astro Photography Portfolio Template
A modern, fast, and highly customizable photography portfolio template built with Astro. Ideal for photographers who want to showcase their work through a sleek, performant, and professional website.
✨ Features
- Lightning-fast performance with Astro
- Fully responsive design
- Optimized image loading and handling
- Easy to customize
- Easy to organized gallery via a yaml file
- Multiple albums support
- Image zoom capabilities
- Automatic deployment to GitHub pages
- Script to automatically create a gallery from images
🚀 Getting Started
Prerequisites
- Check AstroJS documentation for prerequisites
- Basic knowledge of Astro and web development
Installation
- click "Use this template" on GitHub
- Clone your newly created template
- Install dependencies:
npm install
# or
yarn install
- Start the development server:
npm run dev
# or
yarn dev
📝 Make it your own
Configuration
Edit the astro.config.ts file to update your github pages details:
export default defineConfig({
site: '<github pages domain>',
base: '<repository name>',
// ...
});
Edit the site.config.mts file to update your personal information:
export default {
title: 'SR',
favicon: 'favicon.ico',
owner: 'Sara Richard',
// ... Other configurations
};
Customize site icon
Replace public/favicon.ico with your icon and change the configuration
if your file has a different name/location.
Customize the About page
- Replace the profile image (see site.config.mts for configuration)
- Edit content in about page
Adding Your Photos
- Place your images in the
src/gallery/<album>directory - Update the gallery details in
src/gallery/gallery.yaml. Optionally, you can runnpm run generateto generate a gallery.yaml file from the images in the directory. - Update meta-data for images in the
src/gallery/gallery.yamlfile. - Images are automatically optimized during build
Adding photos to the featured section
"featured" is a builtin collection, and images can be added to it by specifying it in the collections parameter like any other collection.
📸 Phase 1 二次开发技术文档
项目名称: Astro Photography Portfolio (Customized)
版本: Phase 1 Completed
日期: 2025-11-26
1. 核心功能变更概览
本次二次开发主要针对原版模版进行了跨平台兼容性修复、国际化 (i18n) 重构、视觉交互升级以及性能优化。
🛠️ 系统与脚本优化
- 跨平台路径修复 (
npm run generate)- 问题: 原脚本在 Windows 下生成的
gallery.yaml路径包含反斜杠\,导致 Astro 在 Linux/Mac 环境或部署时无法识别图片。 - 解决: 修改
src/data/gallery-generator.ts,引入toUnixPath函数,强制将所有路径分隔符转换为 Unix 风格/。 - 新增: 脚本现在会自动识别文件名中包含
_featured的图片,并自动添加featured标签。 - 新增: 图片生成顺序改为按 EXIF 拍摄时间倒序排列(最新的在前)。
- 问题: 原脚本在 Windows 下生成的
- EXIF 数据透传与展示
- 问题: 原版
gallery.yaml虽然有 EXIF 数据,但前端并未读取和展示。 - 解决:
- 修改
src/data/galleryData.ts和imageStore.ts,确保exif对象被完整传递给前端组件。 - 修改
src/data/imageStore.ts中的builtInCollections,添加cover到白名单,防止报错。 - 修改
src/components/PhotoGrid.astro,格式化 EXIF 数据 (相机、焦段、光圈、ISO) 并注入到 Lightbox (GLightbox) 的描述中。
- 修改
- 问题: 原版
- 性能优化 (缩略图策略)
- 优化: 在
PhotoGrid.astro中,强制使用 Astro 的<Image width={720} ... />组件。 - 效果: 首页和列表页不再加载几千像素的原图,而是加载自动生成的 WebP 缩略图,大幅提升加载速度。
- 优化: 在
🌍 国际化 (i18n) 重构
- 目录结构变更
- 原
src/pages/*下的文件被移动至src/pages/zh/(中文版) 和src/pages/en/(英文版)。 - 根目录
src/pages/index.astro仅作为重定向器,根据base路径自动跳转至默认语言/zh/。 - 组件引用的 Import 路径已全部修正为相对路径 (
../../../等)。
- 原
- 双语支持
- 新建
src/i18n/ui.ts管理翻译字典。 src/components/NavBar.astro支持语言切换按钮,自动根据当前 URL 切换/zh/和/en/前缀。- 首页组件 (
LandingHero,FeaturedGallery) 被改造为支持props传参,允许在不同语言的index.astro中传入对应的翻译文案。
- 新建
🎨 UI 与视觉升级
- 全屏动态轮播背景 (
HeroBackground)- 新建
src/components/HeroBackground.astro组件。 - 逻辑: 自动读取
gallery.yaml中包含cover标签的图片。如果未找到,则回退显示前 3 张图片。 - 效果: 全屏淡入淡出轮播,并添加深色遮罩以保证前景文字可读性。
- 新建
- 自适应导航栏 (
NavBar)- 逻辑: 页面顶部时背景透明、文字白色(配合深色轮播图);向下滚动后背景变白、文字变黑(保证阅读清晰度)。
- 修复: 解决了 Astro Frontmatter 格式错误导致的页面顶部出现白色空白条和
---符号的问题。
2. 使用指南 (给用户/朋友)
如何管理图片
所有操作均在 src/gallery/gallery.yaml 或文件系统中进行。
- 添加新图片: 将图片放入
src/gallery/xxx/文件夹。 - 生成数据: 运行
npm run generate。 - 设置精选 (Featured):
- 方法 A: 图片文件名包含
_featured(如img_featured.jpg),重新运行脚本。 - 方法 B: 在
gallery.yaml中手动给图片添加- featured标签。
- 方法 A: 图片文件名包含
- 设置首页轮播图 (Cover):
- 在
gallery.yaml中手动给图片添加- cover标签。 - 注意:
cover标签不会出现在网站的分类过滤器中,仅用于首页背景。
- 在
3. 开发指南:如何添加新菜单项
如果你想添加一个全新的页面(例如:“视频 / Video”),请严格按照以下步骤操作:
第一步:创建页面文件
你需要分别为中文版和英文版创建页面文件。
-
中文版: 新建
src/pages/zh/video.astro代码段
--- import MainLayout from '../../layouts/MainLayout.astro'; --- <MainLayout lang="zh" title="视频作品"> <div class="container mx-auto py-20"> <h1>我的视频</h1> </div> </MainLayout> -
英文版: 新建
src/pages/en/video.astro(内容同上,语言改lang="en")。
第二步:定义菜单名称
打开 src/i18n/ui.ts,在字典中添加新菜单的翻译。
TypeScript
export const ui = {
zh: {
'nav.home': '首页',
'nav.gallery': '作品集',
'nav.about': '关于我',
'nav.video': '视频', // <--- 新增
},
en: {
'nav.home': 'Home',
'nav.gallery': 'Gallery',
'nav.about': 'About',
'nav.video': 'Video', // <--- 新增
},
};
第三步:添加到导航栏
打开 src/components/NavBar.astro,将新项加入 menuItems 数组。
TypeScript
// ...
const menuItems = [
{ name: t['nav.home'], link: homePath },
{ name: t['nav.gallery'], link: `${homePath}/collections` },
// 新增下面这一行
{ name: t['nav.video'], link: `${homePath}/video` },
{ name: t['nav.about'], link: `${homePath}/about` },
];
// ...
4. 已知待优化项 (Phase 2 计划)
- 分类名称国际化: 目前分类显示为文件夹名称(英文),尚未对接翻译字典。
- CMS 后台集成: 计划集成 Decap CMS,实现图形化管理图片和配置,无需接触代码。
Document End