The Tinify API, designed as a REST service, allows you to compress and optimize WebP, JPEG and PNG images.
The Tinify function, written for AutoHotkey v1, uses the free public API and does not require authentication or have a maximum upload limit quota.
Parameters:
ImageIn
- The path to a local or external image file.
- Path must be the URL to an external image file, or the path to a local image file.
- The path to a local directory, with image filename.
- Directory will be created if it doesn't exist.
- Existing images with the same filename will be overwritten.
- The reference name used to call specific data related to the files. See Usage Example #5.
Function:
Code: Select all
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)
}
Usage Examples:
Compress external image file from URL / Save locally
Code: Select all
Tinify("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", "CompressedImage.png")
Compress local image file / Save locally
Code: Select all
Tinify("OriginalImage.png", "CompressedImage.png")
Compress local image file / Save locally / Store output URL to variable
Code: Select all
ImageURL := Tinify("OriginalImage.png", "CompressedImage.png")
Compress local image file / Store output URL to variable
Code: Select all
ImageURL := Tinify("OriginalImage.png")
Compress local image file / Save locally / Return compression data
Code: Select all
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
Compress local image file from directory / Save locally
Code: Select all
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
}
}
Previous Version
Tinify Websites
https://tinypng.com/
https://tinyjpg.com/
Tinify API Reference
https://tinypng.com/developers/reference
https://tinyjpg.com/developers/reference