0

I've written this piece of code to use a PHP script to send data to Flex.

Here's the Flex Code:

<s:HTTPService id="imageService" url="http://localhost/bookView/amfphp/services/ImageServer/showImage.php" 
                   useProxy="false" 
                   method="POST"
                   result="handleImageResult(event)" 
                   fault="handleFault(event)" 
                   showBusyCursor="true">
        <s:request xmlns="">
            <bdla>"lashf"</bdla>
        </s:request>
    </s:HTTPService>

Here's the PHP code:

        public function returnRandomImage(){
            $contents = file_get_contents("images/code_complete2.png");
            header('Content-Type: image/png');

            return $contents;
        }

Thing is: I'm really interested in using PHP to send an image file, so that I could render it and use it in Flex. However, when I .send() this HttpService, all I get is a fault event with this message: (I've tried both with the header() function and without it).

(mx.messaging.messages::AcknowledgeMessage)#0
 body = "PNG"

That's it. Hope someone can help. If there is no way to use HttpService for this end (i.e. send image files), how can one do it then? I've seen it in an app I worked on so I'm positive it can be done.

EDIT Added PHP code too.

Felipe
  • 11,557
  • 7
  • 56
  • 103
  • You should show your image result handler function and your php function that sends the image. – J_A_X Jul 01 '11 at 14:24

2 Answers2

2

You can encode the image using Base64, this way you can send it and receive it as text.

http://www.google.es/search?sourceid=chrome&ie=UTF-8&q=as3+base64+encoder

Mc-
  • 3,968
  • 10
  • 38
  • 61
  • Hmmm.. I see.. If I decode the file in Flex, will I be able to render it as an image? – Felipe Jun 30 '11 at 23:36
  • Sure, the same library will have a decoder. You can do the same (decode/encode) on your backend PHP, Python, Java... – Mc- Jun 30 '11 at 23:39
  • 1
    Yes, the BitmapData.setPixels() method writes a byteArray to a rectangular area that you can then assign to a Bitmap. – shanethehat Jun 30 '11 at 23:39
  • -1. Sending text is not an appropriate way to send a binary format. You can send binary over to php. this is just an annoying workaround. – J_A_X Jul 01 '11 at 14:24
  • @J_A_X I understand / respect your opinion but if you criticize something you must defend your opinion with a solution. Just trying to help here – Mc- Jul 01 '11 at 14:34
  • @Xavi, I cannot give an accurate solution without knowing more about the original problem, which I commented on the question. I know this is possible, but I think it's just a problem with the implementation the user is doing. – J_A_X Jul 01 '11 at 14:53
0

I will assume you are trying to send an image from php to flex when the flexHTTPservice requests it

You have to make sure the header is set correctly on the php side

// this method should be used if you have the image inside a database
    header('Content-type: image/png');
    echo $image;
//if the image is just an image on the server then you just need to point the HTTPService to the url "http://mydomain.com/testimage.jpg"




If you are sending an image from Flex to PHP then you have to base64 it for best results.

[EDIT]

// I would use a loader or the Flex Image component to work with images.
var antiCache:Date = new Date( );
_source = val + '&noCache=' +antiCache.getTime();

_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler )
_loader.load(new URLRequest(encodeURI(_source)));
addChild( _loader )
The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • I did that (send image I read from a file using the echo construct) and I got the error I mentioned in my post. – Felipe Jul 01 '11 at 00:03
  • Well now that I realize what you are doing I edited my post for you. Not really sure why you are using HTTPService. HTTPService isn't really meant for Images, but has some work arounds like the other post pointed out. – The_asMan Jul 01 '11 at 17:25
  • Thanks for helping. But if `HttpService` is not the way to go when you guys need to send binary data to/from flex, how to do it then? – Felipe Jul 02 '11 at 02:51
  • By the way, I found another way of assigning a `ByteArray` as source for an image. It's the first answer on this thread: http://stackoverflow.com/questions/906791/how-to-convert-bytearray-to-image-or-image-to-bytearray – Felipe Jul 02 '11 at 21:33