• http.Fetch PNG convert to .TXT
    7 replies, posted
I found this the other day on this thread The op last posted it as working but I have literally no idea how to implement it and it's not really working for me.. Just Purple & Black BitMap errors.. Can anyone please explain my stupidity?? file.CreateDir( "cache_img" ) httpImg = httpImg or {} httpImg.data = httpImg.data or {} function httpImg:define( name, url ) if ( self.data[ name ] ) then return end local exist = file.Exists( "cache_img/"..name..".txt", "DATA" ) if ( exist ) then local da_mat = Material( "../data/cache_img/"..name..".txt\n.png" ) self.data[ name ] = da_mat return end http.Fetch( url, function(data) file.Write("cache_img/"..name..".txt", data) local da_mat = Material( "../data/cache_img/"..name..".txt\n.png" ) self.data[ name ] = da_mat end ) end local DEFAULT_ERROR = Material( "error" ) function httpImg:get( name ) return self.data[ name ] or DEFAULT_ERROR end httpImg:define( "weapon", "http://www.springfield-armory.com/wp-content/uploads/2014/03/XD9649HCSP06_1200x782.png" ) hook.Add("HUDPaint", "shit", function() surface.SetDrawColor(255, 255, 255) surface.SetMaterial( httpImg:get( "weapon" ) ) surface.DrawTexturedRect(0, 0, 300, 120) end)
file.Write can write image files now (.png, .jpg and .vtf), so you can replace the ".txt" and ".txt\n.png" bits with just ".png" and it should work.
Got an error :/ halp [ERROR] addons/testhud/lua/autorun/test.lua:35: bad argument #1 to 'SetMaterial' (IMaterial expected, got nil)   1. SetMaterial - [C]:-1    2. fn - addons/test/lua/autorun/test.lua:35     3. unknown - addons/ulib/lua/ulib/shared/hook.lua:110
Post your new code.
file.CreateDir( "cache_img" ) httpImg = httpImg or {} httpImg.data = httpImg.data or {} function httpImg:define( name, url ) if ( self.data[ name ] ) then return end local exist = file.Exists( "cache_img/"..name..".png", "DATA" ) if ( exist ) then local da_mat = Material( "../data/cache_img/"..name..".png" ) self.data[ name ] = da_mat return end http.Fetch( url, function(data) file.Write("cache_img/"..name..".png", data) local da_mat = Material( "../data/cache_img/"..name..".png" ) self.data[ name ] = da_mat end ) end local DEFAULT_ERROR = Material( "error" ) function httpImg:get( name ) return self.data[ name ] or DEFAULT_ERROR end httpImg:define( "weapon", "http://www.springfield-armory.com/wp-content/uploads/2014/03/XD9649HCSP06_1200x782.png" ) hook.Add("HUDPaint", "shit", function()     surface.SetDrawColor(255, 255, 255)     surface.SetMaterial( weapon )     surface.DrawTexturedRect(0, 0, 300, 120) end)
It might be worth posting how you did it, so other people can learn as well.
file.CreateDir( "cache_img" ) httpImg = httpImg or {} httpImg.data = httpImg.data or {} function httpImg:define( name, url ) if ( self.data[ name ] ) then return end local exist = file.Exists( "cache_img/"..name..".png", "DATA" ) if ( exist ) then local da_mat = Material( "../data/cache_img/"..name..".png" ) self.data[ name ] = da_mat return end http.Fetch( url, function(data) file.Write("cache_img/"..name..".png", data) local da_mat = Material( "../data/cache_img/"..name..".png" ) self.data[ name ] = da_mat end ) end local DEFAULT_ERROR = Material( "error" ) function httpImg:get( name ) return self.data[ name ] or DEFAULT_ERROR end httpImg:define( "face_punch", "https://upload.wikimedia.org/wikipedia/commons/d/d5/Facepunchtransparent.png" ) local function Icons() if table.HasValue({"ceo", "superadmin", "head_admin", "admin"}, ply:GetNWString("usergroup")) then surface.SetMaterial(Material("data/cache_img/face_punch.png")) surface.SetDrawColor(255,255,255,255) surface.DrawTexturedRect(W - 10.5 * 101, H - 135, 22,21.9)  end local function DrawHUD() if not IsValid( ply ) then  if IsValid( LocalPlayer() ) then ply = LocalPlayer() end return end if ply:Alive() then Icons() end end hook.Add("HUDPaint","DrawHUD",DrawHUD)
Sorry, you need to Log In to post a reply to this thread.