Author Topic: upload image to server  (Read 6388 times)

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
upload image to server
« on: October 19, 2017, 06:02:45 PM »
I know unity has www form to save images to a server through a php or cgi script on the server, but how is this done through tnet? Im thinking it has something to do with saving bytes of a texture and uploading that data to server? I dont know where to begin...

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: upload image to server
« Reply #1 on: October 19, 2017, 08:09:17 PM »
Custom packet handler and byte array? Add handler on server to save byte array to disk. TNet has built-in serialization for Unity's Texture2D, so you don't even need byte array. It uses Texture2D.EncodeToPNG under the hood.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: upload image to server
« Reply #2 on: October 20, 2017, 01:13:35 AM »
I managed to get this done using tnet save .... ::)
  1.  IEnumerator ProcessPicture()
  2.     {
  3.         // We should only read the screen after all rendering is complete
  4.         yield return new WaitForEndOfFrame();
  5.  
  6.         // Create a texture the size of the screen, RGB24 format
  7.         int width = Screen.width;
  8.         int height = Screen.height;
  9.         var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
  10.  
  11.         // Read screen contents into the texture
  12.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  13.         tex.Apply();
  14.  
  15.         // Encode texture into PNG
  16.         byte[] bytes = tex.EncodeToPNG();
  17.         //Destroy(tex);
  18.  
  19.         StartCoroutine(UploadPicture(bytes));
  20.     }
  21.  
  22.  
  23. IEnumerator UploadPicture(byte[] b)
  24.     {
  25.         yield return new WaitForSeconds(1);
  26.  
  27.         TNManager.SaveFile("filename" , b);
  28.  
  29.     }

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: upload image to server
« Reply #3 on: October 20, 2017, 03:56:42 AM »
Awesome, even easier :D

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: upload image to server
« Reply #4 on: October 22, 2017, 02:03:06 AM »
Im now trying to download that file , and apply it to a texture. All works well, except however, I randomly sometimes receive a "Malformed data packet" when downloading.

  1. public void GetPicture()
  2.     {
  3.         TNManager.LoadFile("filename.bytes", OnLoadFile);
  4.     }
  5.  
  6.     void OnLoadFile(string filename, byte[] data)
  7.     {
  8.         if (data.Length == 0)
  9.         {
  10.  
  11.         }
  12.         else
  13.         {
  14.  
  15.             Debug.Log("download picture a success!");
  16.             Texture2D tex;
  17.             tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
  18.             tex.LoadImage(data);
  19.             tex.Apply();
  20.  
  21.             mytexture.texture = tex;
  22.         }


Unity says the "Malformed data packet" error is coming from 'GetPicture' function.

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: upload image to server
« Reply #5 on: October 22, 2017, 06:38:57 PM »
Which version of TNet? I used your exact code w/ TNet 3.0.9 and received no errors.

acronyte

  • Newbie
  • *
  • Thank You
  • -Given: 9
  • -Receive: 0
  • Posts: 46
    • View Profile
Re: upload image to server
« Reply #6 on: October 24, 2017, 01:55:01 PM »
I managed to solve getting the "Malformed data packet" error.....

In my code where is says
  1. // Encode texture into PNG
  2.         byte[] bytes = tex.EncodeToPNG();

I changed that to
  1. // Encode texture into JPG
  2.         byte[] bytes = tex.EncodeToJPG(30);

which basically saves as a jpg rather than a png.... The difference between the two save types is a larger resulting file (png larger than jpg)....

The orginal code saved the png at 400kb, while the new code saves as jpg at 20kb...

I can only assume the error was caused by the size of the file?

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: upload image to server
« Reply #7 on: October 28, 2017, 06:54:33 PM »
TNet should support packets up to 16777216 bytes (16 MB).

I'd say it's more likely an issue with the Buffer class not packing the packets properly. I recall there being a bug with Buffer's length or size property back in TNet 2, don't remember if it was officially fixed.

Either way, glad you found a workaround :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: upload image to server
« Reply #8 on: November 06, 2017, 07:42:46 AM »
In Sightseer I let server admins upload their own flag textures, and I've seen fairly large ones (>700 kb) being used without issues. Can't say I've ran into the "malformed data packet" error. Maybe the SaveFile didn't have a chance to complete?