用网络API压缩图像.ahk

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=31547

; 从 URL 压缩外部图像文件/本地保存
Tinify("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", "CompressedImage.png")

; 压缩本地图片文件/本地保存
Tinify("OriginalImage.png", "CompressedImage.png")

; 压缩本地图像文件/本地保存/将输出 URL 存储到变量
ImageURL := Tinify("OriginalImage.png", "CompressedImage.png")

; 压缩本地图像文件/将输出 URL 存储到变量
ImageURL := Tinify("OriginalImage.png")

; 压缩本地图像文件/本地保存/返回压缩数据
ImageURL := Tinify("OriginalImage.png", "CompressedImage.png", MyImage)

MsgBox, % "Input Size: " MyImage.input.size
. "`nInput Type: " MyImage.input.type
. "`nOutput Size: " MyImage.output.size
. "`nOutput Type: " MyImage.output.type
. "`nOutput Width: " MyImage.output.width
. "`nOutput Height: " MyImage.output.height
. "`nOutput Ratio: " MyImage.output.ratio
. "`nOutput URL: " MyImage.output.url
. "`nInput Path: " MyImage.ImageIn
. "`nOutput Path: " MyImage.ImageOut
. "`nJSON String: " MyImage.StrJSON

; 从目录压缩本地图像文件/本地保存
Loop, Files, C:\Windows\Web\Wallpaper\*.*, FR
{
    If A_LoopFileExt IN PNG,APNG,JPG,JPEG,WEBP
    {
    ; // Replace original
    ;Tinify(A_LoopFileLongPath, A_LoopFileLongPath)

    ; // New file with suffix
    SplitPath, A_LoopFileLongPath,, Dir, Ext, NameNoExt
    MsgBox, % Dir "\" NameNoExt "-tinified." Ext
    }
}


Tinify(ImageIn, ImageOut := "", ByRef ObjRet = "") {
  ComObjError(False)

  IsURL := (InStr(ImageIn, "/") ? 1 : 0)
  ImageIn := (!InStr(ImageIn, ":") && !IsURL ? A_ScriptDir "\" ImageIn : ImageIn)
  ImageOut := (!InStr(ImageOut, ":") ? A_ScriptDir "\" ImageOut : ImageOut)

  If (IsURL) {
    BodyIn := "{""source"":{""url"":""" ImageIn """}}"
  } Else {
    If (!FileExist(ImageIn)) {
      return "Invalid input path."
    }

    FileIn := FileOpen(ImageIn, "r")
    BodyIn := ComObjArray(0x11, FileIn.Length)
    DataIn := NumGet(ComObjValue(BodyIn) + 8 + A_PtrSize)
    FileIn.RawRead(DataIn + 0, FileIn.Length)
    FileIn.Close()
  }

  HttpReq := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  HttpReq.SetTimeouts(0, 60000, 30000, 120000)
  HttpReq.Open("POST", "https://tinypng.com/web/shrink")
  HttpReq.SetRequestHeader("Content-Type", (IsURL ? "application/json" : "application/x-www-form-urlencoded"))
  HttpReq.Send(BodyIn)
  HttpReq.WaitForResponse()

  HTMLDoc := ComObjCreate("HTMLFile")
  HTMLDoc.Write("<meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">")
  ObjRet := HTMLDoc.parentWindow.eval("(" HttpReq.ResponseText ")")
  ObjRet.StrJSON := HttpReq.ResponseText
  ObjRet.ImageIn := ImageIn
  ObjRet.ImageOut := ImageOut

  VarSetCapacity(HTMLDoc, 0)
  HTMLDoc := ""

  OutputURL := ObjRet.output.url

  If (ImageOut && OutputURL <> "") {
    SplitPath, ImageOut, OutFileName, OutDir

    If !FileExist(OutDir) {
      FileCreateDir, % OutDir
    }

    HttpReq.Open("GET", OutputURL)
    HttpReq.Send()
    HttpReq.WaitForResponse()
    FileOut := FileOpen(ImageOut, "w")
    BodyOut := HttpReq.ResponseBody
    DataOut := NumGet(ComObjValue(BodyOut) + 8 + A_PtrSize)
    FileOut.RawWrite(DataOut + 0, BodyOut.MaxIndex() + 1)
    FileOut.Close()
  }

  VarSetCapacity(HttpReq, 0)
  HttpReq := ""

  return (OutputURL = "" ? "Error" : OutputURL)
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。