A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Display a base64 encoded image in flash?

  1. #1
    Junior Member
    Join Date
    Sep 2004
    Posts
    7

    Display a base64 encoded image in flash?

    Hi all,

    I am using a service on the net which provides dynamic map images. The idea is basically that you make a "GET" request to the mapping server providing them with co-ordinates of what area of the map would you like to view and the scale to display, and then they send you back XML which has details of the area inside it - and most importantly, a map image which has been dynamically generated for you (can be any format you choose including jpg, gif, png etc).

    The image is inside the XML in Base64Binary encoded format (raw image data), for example (truncated this example a lot):

    <Image>/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODw...etc</Image>

    You are then supposed to base64 decode this, and that will give you your map image.

    I have flash/actionscript code that will make a GET request to their server and retrieve the XML. It then goes through the XML and obtains the data between the <Image></Image> tags and puts it into a myImage variable.

    My question is...what would I then need to do in flash/actionscript to get this raw image data into something displayed on the screen to the user as a proper image? Is there any way to create a new image on the fly using raw image data?

    The only thing I can think of is that I would make the actionscript write the data out to the hard drive as "newfile.jpg" and then make it read the image back in and display it from there...but I am wondering if there is a better way since a lot of people will be using this and I would like to avoid creating tons of temporary image files on the hard drive.

    Does anyone know how to solve this problem (or alternatively, can provide me code on how to write the image data out to a file on the hard drive, and then make flash display the newly created image file on the fly?)

    Thank you in advance!

  2. #2
    Senior Member
    Join Date
    May 2002
    Location
    Salt Lake City
    Posts
    211
    I may be wrong, but I don't think flash can do what you want it to. Maybe you could use another language to convert the image into a jpg or something, and then have flash deal with it. My php isn't good enough to know whether that could handle it. I'd love to see a solution. Good luck.
    tap water builds character
    -Aesop Rock

  3. #3
    Junior Member
    Join Date
    Sep 2004
    Posts
    7

    php

    I am fearing the same but I don't think PHP can do anything more than Flash can with this. The only thing you can do from PHP is something like:

    <?
    $var = <raw image data>;

    header("Content-type: image/jpg");
    header("Content-length: 2630");
    echo base64_decode($var);
    ?>

    This works to display the image in PHP I believe - but doesn't really help with getting it displayed in Flash :| I think the only way this will work is if I save the raw image data to the hard drive, then somehow make flash read that file back in on the fly and display it.

    Does anyone know how to make flash read a image file off the hard drive and display it whilst the movie is running?

    Thanks in advance,

  4. #4
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    I think the only way this will work is if I save the raw image data to the hard drive, then somehow make flash read that file back in on the fly and display it.
    I also fear that this is the only way to achieve this.

    Pelase have a look at the question I asked last month about flash and PHP communication (I learned a lot out of that thread).

    So basically, what you would need to do is call the PHP script from inside flash. Then PHP should create the image like you showed using the "base64_decode($var)" function and instead of echoing it back on the screen it should write it to a file (something like "image.jpg") using the file manipulation functions of PHP, such as "fopen" function (it is documented here)

    I may help you out with this whole thing about flash and PHP if I get some free time this weekend.

    Good luck!

  5. #5
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    code:

    <?php
    function base64_to_jpeg( $inputfile, $outputfile ) {
    /* read data (binary) */
    $ifp = fopen( $inputfile, "rb" );
    $imageData = fread( $ifp, filesize( $inputfile ) );
    fclose( $ifp );
    /* encode & write data (binary) */
    $ifp = fopen( $outputfile, "wb" );
    fwrite( $ifp, base64_decode( $imageData ) );
    fclose( $ifp );
    /* return output filename */
    return( $outputfile );
    }
    ?>



    http://us4.php.net/manual/en/function.base64-decode.php

  6. #6
    Junior Member
    Join Date
    Sep 2004
    Posts
    7

    thanks :)

    Thank you for the php code And that brings me to my next question which you already touched on...

    Is it possible in flash to pull images from other servers and display them on the fly? for example if I have an image at http://www.fubar.com/mypicture.jpg - is it possible to make flash/actionscript grab that whilst the movie is running, and then display it within the flash movie?

    Alternatively, if this is not possible - is there a way to do it with a image that is stored on the hard drive of the server where the swf is? For example if www.server.com is where i have my html page with the flash on - and there is a mypicture.jpg on that server in the same directory as the .swf - is it possible to read that in and display it whilst the movie is running? The reason I need is this is because when the php grabs the base64 raw image data and converts + saves it to the hard drive...the flash will then need to read this image in and display it icon_smile.gif

    Thanks!

  7. #7
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    Yes you can load an image from another server as long as you use the absolute url.....

    loadMovie....
    Action; load a SWF or JPEG file into the Flash Player while the original movie is playing. The loadMovie action lets you display several movies at once and switch between movies without loading another HTML document.

    A movie or image loaded into a target movie clip inherits the position, rotation, and scale properties of the targeted movie clip. The upper left corner of the loaded image or movie aligns with the registration point of the targeted movie clip. Alternatively, if the target is the _root Timeline, the upper left corner of the image or movie aligns with the upper left corner of the Stage.

    loadMovie ..... loads to a target

    loadMovieNum ...... loads to a level

  8. #8
    Member
    Join Date
    Feb 2004
    Location
    Somewhere on Earth (I think)
    Posts
    87
    Darn, I forgot to post the thread I was talking about.

    He it is anyway: http://www.flashkit.com/board/showth...hreadid=576654

  9. #9
    Junior Member
    Join Date
    Sep 2004
    Posts
    7

    gif

    thank you for all your help

    Is it possible in any way to load .gif images on the fly too (from a url) ?

  10. #10
    Junior Member
    Join Date
    May 2008
    Posts
    19

    Exclamation Decoding a byteArray image

    I don't know if this is possible (haven't seen it done anywhere). Is there a way to decode a Base64 string back into a ByteArray and subsequently into a Bitmap so that it can be displayed within Flash?

    I have successfully been able to encode the BitmapData into PNG via a PNGEncoder class. This results in a ByteArray which I have encoded using a Base64 class.

    I know this is an old thread but thought that this may be possible now with AS3.

    Any help much appreciated.

  11. #11

  12. #12
    Junior Member
    Join Date
    May 2008
    Posts
    19

    Thumbs up

    Great. Much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center