views:

43

answers:

2

I want to pass an image as a byte array from php to a .NET web serice. The php client is as follows:

<?php
class Image{
  public $ImgIn = array();
}
$file = file_get_contents('chathura.jpg');
$ImgIn = str_split($file);
foreach ($ImgIn as $key=>$val) { $ImgIn[$key] = ord($val); }

$client = new SoapClient('http://localhost:64226/Service1.asmx?wsdl');
$result = $client->PutImage(new Image());
echo $result->PutImageResult;
//print_r($ImgIn);

?>

Here is the web method in ASP.NET web service:

    [WebMethod]
    public string PutImage(byte[] ImgIn)
    {
        System.IO.MemoryStream ms =
           new System.IO.MemoryStream(ImgIn);
        System.Drawing.Bitmap b =
          (System.Drawing.Bitmap)Image.FromStream(ms);

        b.Save("imageTest", System.Drawing.Imaging.ImageFormat.Jpeg);
        return "test";
    }

When I run this the image content is correctly read to ImgIm array in php client. (In this instance the image had 16992 elements.) However when the array is passed to the web service method it contains only 5 elements (the first 5 elements of the image)
Can I know what is the reason for the loss of data ? How can I avoid it ?

Thanks

+1  A: 

file_get_contents returns the file contents as string which is not useful for binary files such as images. Try this:

$handle = fopen("chathura.jpg", "r");
$contents = fread($handle, filesize("chathura.jpg"));
fclose($handle);
$client = new SoapClient('http://localhost:64226/Service1.asmx?wsdl');
$result = $client->PutImage($contents);
Darin Dimitrov
@Darin: I tried your code but it does not pass anything to the web service. However the method file_get_contents I have used is successful considering the method I have used in web service. But the problem is that the entire array content is not being passed to the web service.
chathuradd
@Darin: Your code also worked. But it has the problem I have mentioned. That is not the entire content of the array is being passed. I m thinking it might be due to a limitation in array size in php. Any ideas ?
chathuradd
A: 

Guys, it seems that it is not going to be any use to try and pass data as a byte array as PHP anyway converts it to a string when sending. This conversion seems to introduce control characters to the string, making it only send a part of the byte array. I got this to work by sending a base64 encoded string and decoding inside the server. My client side code:

<?php
class Image{
public $ImgIn = '';
}
//ini_set("memory_limit","20M");
$imageData = file_get_contents('chathura.jpg');
$encodedData = base64_encode($imageData);
$Img = new Image();
$Img->ImgIn = $encodedData;
$client = new SoapClient('http://localhost:64226/Service1.asmx?wsdl');
$result = $client->PutImage($Img);
echo($result->PutImageResult);    
?>

ASP .NET web service code:

    [WebMethod]
    public string PutImage(String ImgIn)
    {
        byte[] ImgInBytes = Convert.FromBase64String(ImgIn);
        System.IO.MemoryStream ms =
           new System.IO.MemoryStream(ImgInBytes);
        System.Drawing.Bitmap b =
          (System.Drawing.Bitmap)Image.FromStream(ms);

        b.Save("C:\\imageTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        return "success";
    }
chathuradd