http.Fetch png contents and then convert it into a Material() ?

I wanted to be able to do something like this:

[lua]
local function setLogo( str )
LOGO_BASE = Material( conversomehowtopath(str) ) // help me here
end

hook.Add( “Initialize”, “grabLogoBase”, function()
local png = file.Read( “gfe/logo.txt”, “DATA” )

if ( !png ) then
http.Fetch(“http://gunfireempire.com/home/mats/logos/med.png”, function( body )
setLogo( body )
file.Write( “gfe/logo.txt”, body )
end )
else
setLogo( png )
end

end )

// Then use something like this
/*
surface.SetDrawColor( 255, 255, 255 )
surface.SetMaterial( LOGO_BASE )
surface.DrawTexturedRect( 0, 0. 300. 120 )
*/
[/lua]

I believe I seen something like this done before, but im not entirely sure, any ideas?
Thank you

You might want to decompile the Minecraft player model mod. It has code that does exactly that.

Decomplied it like you said, found out they use DHTML:GetHTMLMaterial()

This is what I mocked up, now half of the process is done, incase anybody wanted to know this as well.

A last issue is actually converting the material to a txt file to save, so you don’t have to reach the url
everytime if you already have.

So now the question is, how to convert a Material() into a txt file and a txt file to a Material()

[lua]
local function GetMaterialFromURL( url, w, h, callback )
local html = vgui.Create( “DHTML” )
html:SetSize( w, h )
html:SetPaintedManually( true )
html:SetKeyBoardInputEnabled( false )
html:SetMouseInputEnabled( false )
html:SetAllowLua( true )
html:OpenURL( url )
html:SetVisible( false )

html:UpdateHTMLTexture()

hook.Add( "Think", "waitForCache"..url, function()
	if ( !html:IsLoading() ) then
		html:UpdateHTMLTexture()
		
		local mat = html:GetHTMLMaterial()
		
		callback( mat )
		
		html:Remove()
		hook.Remove( "Think", "waitForCache"..url )
	end
end )

end

hook.Add( “Initialize”, “loadMaterials”, function()
GetMaterialFromURL( “http://gunfireempire.com/home/mats/logos/med.png”, 400, 120, function( mat )
LOGO_BASE = mat
end )
end )

hook.Add( “HUDPaint”, “drawLogo”, function()
if ( !LOGO_BASE ) then return end

surface.SetDrawColor( 255, 255, 255 )
surface.SetMaterial( LOGO_BASE )
surface.DrawTexturedRect( 555, 5, 400, 120 )

end )
[/lua]

There’s a secret “feature” in Material() that allows you to load shit from garrysmod/data:



local mat
http.Fetch("url", function(data)
    file.Write("whatever.txt", data)
    mat = Material("../data/whatever.txt
.png")
end)

hook.Add("HUDPaint", "shit", function()
    if not mat then return end
    surface.SetDrawColor(255, 255, 255)
    surface.SetMaterial(mat)
    surface.DrawTexturedRect(0, 0, 300, 120)
end)


Honestly didn’t know it had that functionality, thank you very much anyways.

Just to finish this thread incase anyone else had this question

This is what I came up with, and works like a charm.

[lua]
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

.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

.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)
[/lua]