Jump to content
  • Advertisement
Sign in to follow this  
Charabis

Loading PNG In PHP And Returning It To C#

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a project that requires an array of textures. I'm looking to get the assets through PHP to avoid allowing direct access to them, but am having trouble getting them to return in a meaningful way. This is what I've built so far on the PHP side:


<?php
if(filter_has_var(INPUT_POST, "merchant") && filter_has_var(INPUT_POST, "test"))
{
$merchant = filter_input(INPUT_POST, "merchant", FILTER_SANITIZE_STRING);
$test = filter_input(INPUT_POST, "test", FILTER_SANITIZE_STRING);
}
else
{
die ("DataMissing");
}
$data_location = $_SERVER['DOCUMENT_ROOT'] . "/" . $merchant;
$arr = array();
if (is_dir($data_location))
{
$dir = opendir($data_location) or die("Couldn't access directory!");
while (($file = readdir($dir)) !== false)
{
$arr[] = file_get_contents($file);
}
echo $arr;
}
else
{
// Need to create it
mkdir($data_location, 0700) or die("Couldn't create " . $data_location);
echo "Empty";
}
?>


Creating the directory works fine, however I'm not seeming to get what I'd expect back to the C# program which calls it. In fact, with nothing in the folder, I seem to get back the string "Array". Needless to say, that's not what I expect or want!

Share this post


Link to post
Share on other sites
Advertisement
There is no [font=courier new,courier,monospace]to_str()[/font] method that will convert your array to readable data in PHP. If you want to display the content of an array (or any other object) you'll have to use [font=courier new,courier,monospace]print_r($mixed)[/font].

If you want your PHP script to output an image you could use the code below;

<?php
$file = 'creature.png';
header('Content-Type:image/png');
header('Content-Length: ' . filesize($file));
print(readfile($file))
?>


P.S It is very important that you do not output any kind of data (not even a newline, space or tab) anywhere else in the code. D.S

Share this post


Link to post
Share on other sites
I may not have said it quite the way I intended. let me clarify a bit more. I'm utilizing the Unity 3D game engine and need to get the PNG files into PHP and returned to C# as a byte array. The reason for needing this format is I intend to utilize "Texture2D.LoadImage()" to take the byte data and convert it into a Texture2D for display in the game. Sorry about the confusion :)

Share this post


Link to post
Share on other sites
Remember that your browser will interpret images and render them for you, instead of outputting the bytes they consist of. The code I posted will output the raw data from the image, meaning the bytes it consists of. I'm not sure whether or not C# [font=courier new,courier,monospace]Texture2D.LoadImage()[/font] accepts data containing the file-header, but unless you plan to output something that could be evaluated and parsed by C# I think you'll have to output the actual file.


$ lynx -source http://localhost:80/~username/gamedev.php

?PNG
?
IHDR2??? pHYs???o?d
OiCCPPhotoshop ICC profilex?SgTS?=???BK???Ko RB???&*! J?!??Q?EE?
...

Share this post


Link to post
Share on other sites
Excepting this has nothing to do with a browser :) I'm creating a PC/Mac standalone, not a web player application. Thus the need to figure out how to get a byte array to pass back to C# from PHP. It would be easy if "return" would actually work, but that doesn't seem to actually return anything to C#. Of course at the moment neither does the echo $arr :P

Share this post


Link to post
Share on other sites
Okay, seems there's a gotcha I wasn't aware of. I haven't confirmed 100% that the above works, but I was able to confirm that part of the code results in a bit of an oddity.


while (($file = readdir($dir)) !== false)
{
$arr[] = file_get_contents($file);
}

[/quote]

Detects 3 files even though only one is in the folder. The other two filenames it picks up are "." and "..". Wrapping the the line to load the file inside

if($file != "." && $file != "..")[/quote]

returns something that looks like it might be what I wanted. The size of the byte[] is about what I expected :)

Share this post


Link to post
Share on other sites
You're not understanding me... The code I posted will output a sequence of data that represents the image. Below are some examples on how to work with the above example in python. Although I haven't tested the code, in the first example, [font=courier new,courier,monospace]bytestream[/font] should be accepted by SFML as an image in memory.

1. To read the data as a stream of bytes in python

import urllib.request
bytestream = urllib.request.urlopen('http://localhost:80/~username/gamedev.php')


2. To read the data as an array of bytes in python

import urllib.request
bytearray = [c for c in urllib.request.urlopen('http://localhost:80/~username/gamedev.php').read()]


3. To read the data as a stream of bytes and save it to file in python

import urllib.request
open('image.png', 'wb').write(urllib.request.urlopen('http://localhost:80/~username/gamedev.php').read())

Share this post


Link to post
Share on other sites

Detects 3 files even though only one is in the folder. The other two filenames it picks up are "." and "..". Wrapping the the line to load the file inside


The symbolic links [font=courier new,courier,monospace].[/font] and [font=courier new,courier,monospace]..[/font] represents the current and the previous folder.

[font=courier new,courier,monospace]'C:\Program Files\.\.\.\..\Program Files\..'[/font] should place you in[font=courier new,courier,monospace][font=arial,helvetica,sans-serif] [/font]'C:\'[font=arial,helvetica,sans-serif], assuming that there is a directory called Program Files in your root.[/font][/font]


[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]Edit: Added the quote I was responding to.[/font][/font] Edited by Deprecated

Share this post


Link to post
Share on other sites
Hidden
Ah. It's interesting that these two came out syntactically equivalent:Ah. That's much shorter than trying to echo it, and gets the same results. Unfortunately, the results still don't seem to want to load as an image. Then too, there's the matter of returning it as an array of images regardless. At least this gets a good start on tomorrow. Thanks for the assist, Deprecated.

Share this post


Link to post
Ah. That's much shorter than trying to echo it, and gets the same results. Unfortunately, the results still don't seem to want to load as an image. Then too, there's the matter of returning it as an array of images regardless. At least this gets a good start on tomorrow. Thanks for the assist, Deprecated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!