New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding image dimensions (width and height) as header/meta attributes #4480
Comments
In case it is of any help to the OP, I also had this requirement and solved it by extending the Pocketbase framework with hooks in Go (code below). But it would be nice to see this included as a standard feature also, it is a relatively common use case. main.goapp.OnRecordBeforeCreateRequest("image_files").Add(func(e *core.RecordCreateEvent) error {
_, fh, err := e.HttpContext.Request().FormFile("file")
if err != nil {
return nil
}
uf := e.UploadedFiles["file"]
f := uf[0]
im, err := GetImageMetadata(f, fh)
if err != nil {
app.Logger().Error("Image metadata error", err)
}
e.Record.Set("original_name", im.OriginalName)
e.Record.Set("name", im.FileName)
e.Record.Set("mime_type", im.MimeType)
e.Record.Set("size", fh.Size)
e.Record.Set("width", im.Width)
e.Record.Set("height", im.Height)
return nil
}) meta.gopackage main
import (
"errors"
"image"
_ "image/jpeg"
_ "image/png"
"mime"
"mime/multipart"
"path/filepath"
"github.com/pocketbase/pocketbase/tools/filesystem"
)
type ImageMetadata struct {
OriginalName string
FileName string
MimeType string
Size int64
Width int
Height int
}
func GetImageMetadata(f *filesystem.File, fh *multipart.FileHeader) (im ImageMetadata, err error) {
if fh.Filename == "" {
return im, errors.New("no file name in headers")
}
ext := filepath.Ext(fh.Filename)
mimeType := mime.TypeByExtension(ext)
im.OriginalName = fh.Filename
im.Size = fh.Size
im.MimeType = mimeType
if f == nil {
return im, errors.New("no file available")
}
im.FileName = f.Name
if mimeType != "image/jpeg" && mimeType != "image/png" {
return
}
r, err := f.Reader.Open()
if err != nil {
return im, err
}
defer r.Close()
img, _, err := image.DecodeConfig(r)
if err != nil {
return im, err
}
im.Width = img.Width
im.Height = img.Height
return
} |
This is related to the issue i raised here : File-handling is currently one of the only pain-points in pocketbase for me Adding the following information as a meta would be really helpul
Width/Height might be problematic properties if we are dealing with non-image files |
Hey there,
Would you consider appending a header to the response of file requests that are common image extensions to indicate their dimensions?
For example, I was working on my gallery app, and I needed to know the dimensions of the image I was displaying to apply some logic. However, it seems that to get to the dimensions information, I would need to download most of the file chunks and try to parse them.
Anyway, do let me know if I'm missing something very obvious, but otherwise, cheers!
The text was updated successfully, but these errors were encountered: