Skip to content
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

Open
Fractal-Tess opened this issue Mar 3, 2024 · 2 comments
Open

Comments

@Fractal-Tess
Copy link

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!

@ganigeorgiev ganigeorgiev changed the title Adding file size to heade Adding image dimensions (width and height) as header/meta attributes Mar 4, 2024
@rodoch
Copy link

rodoch commented Mar 6, 2024

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.go

app.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.go

package 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
}

@calumk
Copy link

calumk commented Apr 15, 2024

This is related to the issue i raised here :

#1171 (reply in thread)

File-handling is currently one of the only pain-points in pocketbase for me
It does so well at stroing files, making them easily accessible, etc, but it feels like adding meta-data for the files would be really helpful for a lot of use cases.

Adding the following information as a meta would be really helpul

  OriginalName :  string
  FileName    : string
  MimeType     string
  Size         int64

Width/Height might be problematic properties if we are dealing with non-image files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants