Added different image sizes

This commit is contained in:
Óscar M. Lage 2024-10-28 12:57:45 +01:00
parent e88fe268f1
commit 9723046c56
12 changed files with 136 additions and 1 deletions

View File

@ -184,5 +184,12 @@ func DownloadImage(url, slug string) error {
}
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
// Generate thumbnails for the specified resolutions
resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails
if err := utils.GenerateThumbnails(filePath, slug, imageDir, resolutions); err != nil {
return fmt.Errorf("error generating thumbnails: %w", err)
}
return nil
}

View File

@ -231,5 +231,13 @@ func DownloadImage(url, slug, suffix string) error {
}
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
// Generate thumbnails for the specified resolutions
thumbnail_slug := fmt.Sprintf("%s%s", slug, suffix)
resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails
if err := utils.GenerateThumbnails(filePath, thumbnail_slug, imageDir, resolutions); err != nil {
return fmt.Errorf("error generating thumbnails: %w", err)
}
return nil
}

View File

@ -102,5 +102,13 @@ func DownloadImage(url, slug, imageType string) error {
}
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
// Generate thumbnails for the specified resolutions
thumbnail_slug := fmt.Sprintf("%s-%s", slug, imageType)
resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails
if err := utils.GenerateThumbnails(filePath, thumbnail_slug, imageDir, resolutions); err != nil {
return fmt.Errorf("error generating thumbnails: %w", err)
}
return nil
}

View File

@ -151,5 +151,12 @@ func DownloadImage(url, slug string) error {
}
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
// Generate thumbnails for the specified resolutions
resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails
if err := utils.GenerateThumbnails(filePath, slug, imageDir, resolutions); err != nil {
return fmt.Errorf("error generating thumbnails: %w", err)
}
return nil
}

View File

@ -102,5 +102,13 @@ func DownloadImage(url, slug, imageType string) error {
}
fmt.Printf(" - Image saved successfully at: %s\n", filePath)
// Generate thumbnails for the specified resolutions
thumbnail_slug := fmt.Sprintf("%s-%s", slug, imageType)
resolutions := []uint{1024, 640, 320} // Define resolutions array for thumbnails
if err := utils.GenerateThumbnails(filePath, thumbnail_slug, imageDir, resolutions); err != nil {
return fmt.Errorf("error generating thumbnails: %w", err)
}
return nil
}

View File

@ -8,6 +8,9 @@ rate: {{ .Rate }}
pages: {{ .Pages }}
progress: {{ .Progress }}
image: {{ .Image }}
image320: {{ imageSize .Image "320" }}
image640: {{ imageSize .Image "640" }}
image1024: {{ imageSize .Image "1024" }}
date: {{ .Date }}
draft: false
tags: {{ .Tags }}

View File

@ -6,8 +6,17 @@ year: {{ .Year }}
rate: {{ .Rate }}
progress: {{ .Progress }}
image: {{ .Image }}
image320: {{ imageSize .Image "320" }}
image640: {{ imageSize .Image "640" }}
image1024: {{ imageSize .Image "1024" }}
poster-image: {{ .Poster }}
poster-image320: {{ imageSize .Poster "320" }}
poster-image640: {{ imageSize .Poster "640" }}
poster-image1024: {{ imageSize .Poster "1024" }}
background-image: {{ .Background }}
background-image320: {{ imageSize .Background "320" }}
background-image640: {{ imageSize .Background "640" }}
background-image1024: {{ imageSize .Background "1024" }}
date: {{ .Date }}
draft: false
tags: {{ .Tags }}

View File

@ -6,8 +6,17 @@ year: {{ .Year }}
rate: {{ .Rate }}
progress: {{ .Progress }}
image: {{ .Image }}
image320: {{ imageSize .Image "320" }}
image640: {{ imageSize .Image "640" }}
image1024: {{ imageSize .Image "1024" }}
poster-image: {{ .Poster }}
poster-image320: {{ imageSize .Poster "320" }}
poster-image640: {{ imageSize .Poster "640" }}
poster-image1024: {{ imageSize .Poster "1024" }}
background-image: {{ .Background }}
background-image320: {{ imageSize .Background "320" }}
background-image640: {{ imageSize .Background "640" }}
background-image1024: {{ imageSize .Background "1024" }}
date: {{ .Date }}
draft: false
tags: {{ .Tags }}

View File

@ -6,6 +6,9 @@ artist: "{{ .Artist }}"
year: {{ .Year }}
rate: {{ .Rate }}
image: {{ .Image }}
image320: {{ imageSize .Image "320" }}
image640: {{ imageSize .Image "640" }}
image1024: {{ imageSize .Image "1024" }}
tracks: {{ .Tracks }}
date: {{ .Date }}
draft: false

View File

@ -7,8 +7,17 @@ rate: {{ .Rate }}
progress: {{ .Progress }}
episode: {{ .Episode }}
image: {{ .Image }}
image320: {{ imageSize .Image "320" }}
image640: {{ imageSize .Image "640" }}
image1024: {{ imageSize .Image "1024" }}
poster-image: {{ .Poster }}
poster-image320: {{ imageSize .Poster "320" }}
poster-image640: {{ imageSize .Poster "640" }}
poster-image1024: {{ imageSize .Poster "1024" }}
background-image: {{ .Background }}
background-image320: {{ imageSize .Background "320" }}
background-image640: {{ imageSize .Background "640" }}
background-image1024: {{ imageSize .Background "1024" }}
date: {{ .Date }}
draft: false
tags: {{ .Tags }}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
@ -22,7 +23,15 @@ type FrontMatter struct {
}
func GenerateMarkdown(templatePath string, outputPath string, data map[string]interface{}) error {
tmpl, err := template.ParseFiles(templatePath)
// Define a template funcition that generates image versions
funcMap := template.FuncMap{
"imageSize": func(imagePath string, size string) string {
base := strings.TrimSuffix(imagePath, filepath.Ext(imagePath))
return fmt.Sprintf("%s-%s.jpg", base, size)
},
}
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(funcMap).ParseFiles(templatePath)
if err != nil {
return err
}

View File

@ -1,12 +1,21 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"unicode"
"github.com/nfnt/resize"
)
func CreateDirIfNotExists(dir string) error {
@ -74,3 +83,49 @@ func Sluggify(s string) string {
return slug
}
func GenerateThumbnails(originalPath, slug, outputDir string, resolutions []uint) error {
// Open the original image
file, err := os.Open(originalPath)
if err != nil {
return fmt.Errorf("error opening original image: %w", err)
}
defer file.Close()
imgData, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("error reading original image data: %w", err)
}
// Decode the original image
img, _, err := image.Decode(bytes.NewReader(imgData))
if err != nil {
return fmt.Errorf("error decoding image: %w", err)
}
// Generate thumbnails for each resolution
for _, res := range resolutions {
// Resize the image while maintaining the aspect ratio
thumbnail := resize.Resize(res, 0, img, resize.Lanczos3)
// Define the new filename with resolution suffix
thumbnailFilename := fmt.Sprintf("%s-%d.jpg", slug, res)
thumbnailPath := filepath.Join(outputDir, thumbnailFilename)
// Save the resized image
thumbnailFile, err := os.Create(thumbnailPath)
if err != nil {
return fmt.Errorf("error creating thumbnail file: %w", err)
}
defer thumbnailFile.Close()
err = jpeg.Encode(thumbnailFile, thumbnail, nil)
if err != nil {
return fmt.Errorf("error encoding thumbnail: %w", err)
}
fmt.Printf(" - Thumbnail saved successfully at: %s\n", thumbnailPath)
}
return nil
}