Files
red-valley/resources/[framework]/[addons]/0r_idcard/client/headshot.lua
2026-03-29 21:41:17 +03:00

75 lines
2.0 KiB
Lua

requests = {}
function GenerateId()
local id = ""
for i = 1, 15 do
id = id .. (math.random(1, 2) == 1 and string.char(math.random(97, 122)) or tostring(math.random(0,9)))
end
return id
end
function ClearHeadshots()
for i = 1, 32 do
if IsPedheadshotValid(i) then
UnregisterPedheadshot(i)
end
end
end
function GetHeadshot(ped)
ClearHeadshots()
if not ped then ped = PlayerPedId() end
if DoesEntityExist(ped) then
local handle, timer = RegisterPedheadshotTransparent(ped), GetGameTimer() + 5000
while not IsPedheadshotReady(handle) or not IsPedheadshotValid(handle) do
Wait(50)
if GetGameTimer() >= timer then
return {success=false, error="Could not load ped headshot."}
end
end
local txd = GetPedheadshotTxdString(handle)
local url = string.format("https://nui-img/%s/%s", txd, txd)
return {success=true, url=url, txd=txd, handle=handle}
end
end
function GetBase64(ped)
if not ped then ped = PlayerPedId() end
local headshot = GetHeadshot(ped)
if headshot.success then
local requestId = GenerateId()
requests[requestId] = nil
SendNUIMessage({
action = "convert_base64",
img = headshot.url,
handle = headshot.handle,
id = requestId
})
local timer = GetGameTimer() + 5000
while not requests[requestId] do
Wait(250)
if GetGameTimer() >= timer then
return {success=false, error="Waiting for base64 conversion timed out."}
end
end
return {success=true, base64=requests[requestId]}
else
return headshot
end
end
RegisterNUICallback("base64", function(data, cb)
if data.handle then
UnregisterPedheadshot(data.handle)
end
if data.id then
requests[data.id] = data.base64
Wait(2000)
requests[data.id] = nil
end
cb({ok=true})
end)