Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Store facebook profile picture on a bitmap — Gideros Forum

Store facebook profile picture on a bitmap

somezombiegmsomezombiegm Member
edited July 2015 in General questions
Hello! How can i store my (or any other user) facebook profile picture on a variable? like a bitmap.
facebook:addEventListener(Event.REQUEST_COMPLETE, function(e)
        print(e.response)
end
facebook:graphRequest("me/picture")
Apparently on request complete I get the image's url, which I could then add to a texture and then create a bitmap. But on e.response I get a gigantic string of weird characters and symbols. So I'm kinda stuck here. Please help :(.

Comments

  • SinisterSoftSinisterSoft Maintainer
    edited July 2015 Accepted Answer
    I think the media plugin can help you with this.
    ( @ar2rsawseen originally coded this bit, his original code is somewhere on the forum )
    --to check if file exists
    function file_exists(name)
       local f=io.open(name,"r")
       if f~=nil then io.close(f) return true else return false end
    end
     
    --to delete a file
    function file_delete(name)
    	if file_exists(name) then
    		os.remove(name)
    	end
    end
     
    -- to process photo queue
    function fbProcessPhoto()
    	--if there is something in queue
    	if #fbPhotoQueue > 0 then
    		--get id
    		local id = table.remove(fbPhotoQueue)
    		--construct url
    		local url = "<a href="https://graph.facebook.com/"..id.."/picture?type=square&quot" rel="nofollow">https://graph.facebook.com/"..id.."/picture?type=square&quot</a>;
    		print(url)
    		--make UrlLoader call
    		local loader = UrlLoader.new(url)
    		loader:addEventListener(Event.COMPLETE,function(event)
    			print("complete", id)
     
    			-- try to save it in file as png
    			local out = io.open("|D|fb"..id..".png", "wb")
    			out:write(event.data)
    			out:close()
    			-- resave it as png, because image might have been jpeg
    			--but media plugin will automatically convert it to png
    			-- pcall because it might be other unsupported format
    			pcall(function()
    				local media = Media.new("|D|fb"..id..".png")
    				media:save()
    				media = nil
    			end)
    		end)
    		-- we can ignore errors photos and try them again on next sync
    		loader:addEventListener(Event.ERROR,function()
    			print("error",id)
    		end)
    		--call to process next photo
    		fbProcessPhoto()
    	end
    end
     
    function loadFBphoto(id)
    	local texture
    	local file="|D|fb"..id..".png"
    	-- if picture exists
    	if file_exists(file) then
    		print("loading "..file)
    		local success
    		success,texture=pcall(Texture.new,file,true)
    		-- if I was able to load it (has correct format)
    		if not success then
    			texture = nil
    			file_delete(file)
    			print("error loading "..file)
    		end
    	end
    	-- if no texture, take default pic
    	if not texture then
    		texture = Texture.new("fb_default.png", true)
    	end
    	return texture
    end
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • @SinisterSoft
    I get an error when trying to load the images :(.
    This is my output (I changed the id to 222):
    <a href="https://graph.facebook.com/222/picture?type=square" rel="nofollow">https://graph.facebook.com/222/picture?type=square</a>
    complete	222
    loading |D|fb222.png
    error loading |D|fb222.png
    fb_default.png: No such file or directory.
    stack traceback:
    	highScores.lua:93: in function 'loadFBphoto'
    	highScores.lua:330: in function <highScores.lua:324>
    The problem would be here:
    -- if no texture, take default pic
    	if not texture then
    		texture = Texture.new("fb_default.png", true)
    	end
  • somezombiegmsomezombiegm Member
    edited July 2015
    @SinisterSoft
    Wait, I didn't add the plugin... But I've just enter here http://giderosmobile.com/labs/media and on the left it says: Upgrade to Get it... So I guess I can't use it.
  • jdbcjdbc Member
    @SinisterSoft
    Wait, I didn't add the plugin... But I've just enter here http://giderosmobile.com/labs/media and on the left it says: Upgrade to Get it... So I guess I can't use it.
    Gideros is open source, you can use all plugins.
  • @somezombiegm Just use any picture you like - it's what will be show if no picture can be found.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • @jdbc couldn't download it from here http://giderosmobile.com/labs/media. But it's here with the other gideros plugins https://github.com/gideros/giderosplugins.
  • somezombiegmsomezombiegm Member
    edited August 2015
    @SinisterSoft I appreciate the help, but I still have the same problem. I haven't had a single example where I could retrieve a profile picture. I don't want to load any picture in case of no picture is found.

    I don't really understand that code and I don't know if the image is being wrongly saved or loaded, It would be great to make it work right.

    I understand this is what happens when no picture can be found, and that I should create a default picture in case of that.
    -- if no texture, take default pic
    	if not texture then
    		texture = Texture.new("fb_default.png", true)
    	end
    But what I really need to fix is the correct saving of the profile picture.

    Likes: jdbc

    +1 -1 (+1 / -0 )Share on Facebook
  • You can load a fileviewer on the android device, look in the gideros player folder and you will see your game name - the pictures should be in there somewhere.

    Likes: somezombiegm

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 )Share on Facebook
  • somezombiegmsomezombiegm Member
    edited August 2015
    @SinisterSoft Thanks again and sorry for being such a noob. Here's what I have so far:

    I installed a fileviewer and I can see the pictures now, they're always created and they're all in png.

    The programm always knows the pictures exist because on the loadFBphoto(id) function:
    file_exists(file)
    is always true.

    However,
    success,texture=pcall(Texture.new,file,true)
    always returns success as false. And
    texture = Texture.new(picturename.png, true)
    always gives me a "No such file or directory." error.

    The thing is, all the profile pictures are getting saved in gideros/myGame/documents. And all my game images are stored in gideros/myGame/resource. I have no problems loading textures from resource.

    I guess the solution could be:
    - To tell gideros to look on "documents" instead of "resource" when loading a texture?
    - Or maybe to create the profile pictures directly on "resource" instead of "documents"?
  • piepie Member
    @somezombiegm you can't write in resources, but you can load from documents, :) see http://docs.giderosmobile.com/file_system.html

  • Thanks @pie. Now I understand why SinisterSoft added the |D| on the image route.

    I'm testing the texture load again
    texture = Texture.new("|D|"..picturename..".png", true)
    and now I get the message: "Image format is not supported".

    But the images are saved in png :(
  • @pie, wait, scratch that! I'm getting the same error from before: No such file or directory.

    And I'm searching for an image on documents with it's exact name, adding |D| before... So it should've work since the first time I tested the code. =(
  • Some facebook images, for some reason, are in gif, and Gideros can't handle gif images.
    that's why in my original code:
    http://giderosmobile.com/forum/discussion/comment/33814#Comment_33814

    I use pcall, to catch exceptions of incorrect format and load default image instead
  • @ar2rsawseen - I still use that - see src above, he must have cut it out?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • I'm attaching a simplified example of what I'm working on, which would be to save the image, load it and then save it on a bitmap and show it.

    I added the loadFBphoto function and an aditional one: loadFBphoto2, which doesn't use pcall. With that method we can see the "Image format not supported" error even if the photo is saved on png.
    zip
    zip
    Fb Pics Example.zip
    7K
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2015
    @somezombiegm it does not matter how it was saved. If it was gif, it would not be converted to png, because nor gideros nor media library have gif library.
    So if you save it as .png it will still be gif and will be incorrect image format just as you get

    you just need to use default images in such cases

    Likes: somezombiegm

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen oh noes =(( bummer :((

    Well, case closed. Thanks to everyone for your help :)
  • XmanXman Member
    edited August 2015
    Btw, how about add an api for creating texture from image data for displaying image from network?
  • SinisterSoftSinisterSoft Maintainer
    edited August 2015
    @somezombiegm I've not had any gif images at all with any of my friends on FB.

    You could try reading the image data and drawing it as a rendertarget?
    https://en.wikipedia.org/wiki/GIF

    (if it really is important, but gifs should not be too common)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • jdbcjdbc Member
    edited September 2015
    I have the same problem than somezombiegm. I have used a profile jpg picture, Media plugin converts into png but i got "unsupported format" if I do not use pcall.

    If I used pcall then sucess and texture are nil.

    I am not sure that Media plugin converts a JPG to PNG in all cases, I mean Media plugin converts JPG images into PNG format, but not properly.

    I got nil |D|1585191695063321.png: Image format is not supported. when pcall is used.
  • I found my error, I just forgot to integrate Media plugin, so Media is nil but pcall avoid the error.

    May be somezombiegm has the same problem.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.