Davincible / goinsta

Unofficial Instagram API written in Golang (v2022)
MIT License
183 stars 56 forks source link

Image pre-processing in goinsta: Incorrect aspect ratio error while uploading #18

Open qwxxx opened 2 years ago

qwxxx commented 2 years ago

Hello, thanks you for maintaining this repo!

Today I tried to upload a photo, but I got the following error in response: 400:Uploaded image isn't in an allowed aspect ratio

Is it possible to do automatic cropping center to correct aspect ratio?

There is my code:

resp, err := http.Get(imageUrl)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    _, err = insta.Upload(
        &goinsta.UploadOptions{
            File:    resp.Body,
            Caption: getPostTextIG(post),
        },
    )

    if err != nil {
        fmt.Println(err)
    }
    return err

There is imageUrl for test(not working with it) imageUrl:="https://upload-96d5e9c2a2fefe65127343db553761dc.storage.yandexcloud.net/iblock/b1b/b1b528c283a73767c2e2ce8e1ff92f13/1025133_20000_5.jpg"

Davincible commented 2 years ago

Currently, not. I have thought about it, however, the problem with image processing is that to the best of my knowledge, the easiest way to do it is by calling external binaries such as ffmpeg or similar, from a go wrapper (never worked much with images). Goinsta strives to have no external dependencies, and this would also make it difficult to have a streamlined experience between linux/windows/mac.

So if you know how to do image processing in pure go, preferably without calling in any external libs (could possibly consider allowing libs, but they would also need to be standalone), feel free to work on it and open a PR.

I do not have the time to work on it, so for now providing correct media formats is up to the end-user.

pindamonhangaba commented 2 years ago
import (
    "image"
    "image/color"

        "github.com/disintegration/imaging"
)

func resizeImage(src image.Image, w, h int) image.Image {
    dst := imaging.New(w, h, color.White)
    tmp := imaging.Fit(src, w, h, imaging.CatmullRom)
    iw := tmp.Rect.Bounds().Max.X - tmp.Rect.Bounds().Min.X
    ih := tmp.Rect.Bounds().Max.Y - tmp.Rect.Bounds().Min.Y

    return imaging.Overlay(dst, tmp, image.Pt(w/2-iw/2, h/2-ih/2), 1.0)
}