goinsta icon indicating copy to clipboard operation
goinsta copied to clipboard

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

Open qwxxx opened this issue 3 years ago • 2 comments

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"

qwxxx avatar Dec 11 '21 17:12 qwxxx

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.

Davincible avatar Dec 11 '21 17:12 Davincible

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

pindamonhangaba avatar Aug 22 '22 14:08 pindamonhangaba