47 lines
1.9 KiB
Markdown
47 lines
1.9 KiB
Markdown
---
|
|
name: thumbnail_generate
|
|
kind: function
|
|
lang: go
|
|
domain: infra
|
|
version: "1.0.0"
|
|
purity: impure
|
|
signature: "func ThumbnailGenerate(srcPath string, dstPath string, maxWidth int, maxHeight int) error"
|
|
description: "Lee una imagen JPEG o PNG, la redimensiona manteniendo aspect ratio para que quepa en (maxWidth, maxHeight) y la guarda en dstPath. Solo soporta entrada/salida JPEG y PNG."
|
|
tags: [image, thumbnail, resize, file, upload, infra]
|
|
uses_functions: []
|
|
uses_types: []
|
|
returns: []
|
|
returns_optional: false
|
|
error_type: "error_go_core"
|
|
imports: [fmt, image, image/jpeg, image/png, os, path/filepath, strings]
|
|
params:
|
|
- name: srcPath
|
|
desc: "ruta de la imagen original (JPEG o PNG)"
|
|
- name: dstPath
|
|
desc: "ruta destino del thumbnail (extension determina formato de salida: .jpg/.jpeg/.png)"
|
|
- name: maxWidth
|
|
desc: "ancho maximo del thumbnail en pixeles (>0)"
|
|
- name: maxHeight
|
|
desc: "alto maximo del thumbnail en pixeles (>0)"
|
|
output: "nil si el thumbnail se genero correctamente, error si falla la lectura, decode, encode o si la extension no es soportada"
|
|
tested: true
|
|
tests: ["genera thumbnail JPEG mas pequeno que el original", "preserva aspect ratio", "rechaza extension de salida no soportada", "no agranda imagenes ya pequenas"]
|
|
test_file_path: "functions/infra/thumbnail_generate_test.go"
|
|
file_path: "functions/infra/thumbnail_generate.go"
|
|
---
|
|
|
|
## Ejemplo
|
|
|
|
```go
|
|
err := ThumbnailGenerate("./uploads/photo.jpg", "./uploads/thumbs/photo.jpg", 200, 200)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
```
|
|
|
|
## Notas
|
|
|
|
Implementacion solo con stdlib (`image`, `image/jpeg`, `image/png`). Resize nearest-neighbor: rapido y sin dependencias, pero baja calidad. Para apps que necesiten thumbnails de alta calidad usar `golang.org/x/image/draw` con `BiLinear` o `CatmullRom`.
|
|
|
|
NO soporta WebP, AVIF, HEIC ni GIF. Si la imagen original ya es mas pequena que (maxWidth, maxHeight), se guarda sin redimensionar (no se agranda).
|