Last active
March 6, 2021 10:56
-
-
Save tmplinshi/25319135a5fdf32c89da3ef7b030869b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Include <Gdip_All> | |
MsgBox, % link := Imgur_UploadFromClipboard("Your_Client_ID") | |
Run, % link | |
Imgur_UploadFromClipboard(ClientID) { | |
body := ClipImageToByteArray() | |
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1") | |
; whr.SetProxy(2, "localhost:1080") | |
whr.Open("POST", "https://api.imgur.com/3/image", "true") | |
whr.SetRequestHeader("Authorization", "Client-ID " . ClientID) | |
whr.Send(body) | |
whr.WaitForResponse() | |
if RegExMatch(whr.ResponseText, """link"":""\K[^""]+", result) { | |
return StrReplace(result, "\") | |
} else { | |
RegExMatch(whr.ResponseText, """error"":""\K[^""]+", errMsg) | |
throw errMsg ? errMsg : "Unkown Error" | |
} | |
} | |
ClipImageToByteArray(ext := "png", Quality := 75) { | |
pToken := Gdip_Startup() | |
pBitmap := Gdip_CreateBitmapFromClipboard() | |
if (pBitmap < 0) { | |
Gdip_Shutdown(pToken) | |
throw "Gdip_CreateBitmapFromClipboard fail: " pBitmap | |
} | |
byteArr := Gdip_EncodeBitmapToByteArray(pBitmap, ext, Quality) | |
Gdip_DisposeImage(pBitmap) | |
Gdip_Shutdown(pToken) | |
return byteArr | |
} | |
; Modified from iseahound's Gdip_EncodeBitmapToBase64 | |
; https://www.autohotkey.com/boards/viewtopic.php?t=59113&p=248990 | |
Gdip_EncodeBitmapToByteArray(pBitmap, ext, Quality=75) { | |
; Thanks to noname. | |
if Ext not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG | |
return -1 | |
Extension := "." Ext | |
DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize) | |
VarSetCapacity(ci, nSize) | |
DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci) | |
if !(nCount && nSize) | |
return -2 | |
Loop, %nCount% | |
{ | |
sString := StrGet(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16") | |
if !InStr(sString, "*" Extension) | |
continue | |
pCodec := &ci+idx | |
break | |
} | |
if !pCodec | |
return -3 | |
if (Quality != 75) | |
{ | |
Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality | |
if Extension in .JPG,.JPEG,.JPE,.JFIF | |
{ | |
DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize) | |
VarSetCapacity(EncoderParameters, nSize, 0) | |
DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters) | |
Loop, % NumGet(EncoderParameters, "UInt") | |
{ | |
elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0) | |
if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6) | |
{ | |
p := elem+&EncoderParameters-pad-4 | |
NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt") | |
break | |
} | |
} | |
} | |
} | |
DllCall("ole32\CreateStreamOnHGlobal", "ptr",0, "int",true, "ptr*",pStream) | |
DllCall("gdiplus\GdipSaveImageToStream", "ptr",pBitmap, "ptr",pStream, "ptr",pCodec, "uint",p ? p : 0) | |
DllCall("ole32\GetHGlobalFromStream", "ptr",pStream, "uint*",hData) | |
pData := DllCall("GlobalLock", "ptr",hData, "uptr") | |
nSize := DllCall("GlobalSize", "uint",pData) | |
safeArray := ComObjArray(0x11, nSize) ; Create SAFEARRAY = VT_ARRAY|VT_UI1 | |
pvData := NumGet(ComObjValue(safeArray) + 12 + (A_PtrSize==8 ? 4 : 0)) ; get pvData memeber | |
DllCall("RtlMoveMemory", "ptr", pvData, "ptr", pData, "ptr", nSize) | |
DllCall("GlobalUnlock", "ptr",hData) | |
DllCall(NumGet(NumGet(pStream + 0, 0, "uptr") + (A_PtrSize * 2), 0, "uptr"), "ptr",pStream) | |
DllCall("GlobalFree", "ptr",hData) | |
return safeArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment