1

I need some help with loading a PNG file from a server into my flash project. What I want to do it be able to call a PHP page and send it a variable which I can do. Then I want to access a db and get a path for the image, I can do this too. What I'm not sure about is what to do then. What I need to happen is the PHP to return the PNG to flash somehow. Can I just add the PNG to the php page and use a loader somehow? I've been searching around google but most tutorials seem to be getting PNGs out.

Thanks Ben

Ben Gale
  • 5,231
  • 2
  • 16
  • 24
  • I want to mark all of the answers as correct. I'm going to return the filename and then get flash to load the serverurl with the filename at the end. Thanks for the help. – Ben Gale May 24 '11 at 13:44

3 Answers3

4

It is actually pretty easy. : )

<?php
    // do some mysql magic.
    // let's assume you get a filename back as $file_name;

    header('Content-type: image/png');
    readfile($file_name);

Note that you may have to include some path info as well. Not sure where your images are stored, but if the image are in /var/www/public/images, you'd wany to prepend that into your file_get_contents call.


Added: also, if you just want to return a path to the PNG, you can do a URLRequest to a PHP file, let it figure out where the image lives, and return a URL. This is even easier... I'd just recommend standardizing on a data interchange protocol like XML or (even better) JSON... that way, if you ever decide that you want to break out of Flash and into browser technologies, your backend will already be waiting for you.

<?php
    // do some mysql magic.
    // let's assume you get a filename back as $file_name;

    $retVal = array('pathname'=>$file_name);
    header('Content-type: application/json');
    echo json_encode($retVal);
John Green
  • 13,241
  • 3
  • 29
  • 51
  • 3
    the correct way is `header('Content-type: image/png'); readfile($file_name);` – Lawrence Cherone May 24 '11 at 11:21
  • Whoops on the content type. I've had issues with the readfile in the past, but I honestly forget the exact circumstances. So yes, agreed on both counts. – John Green May 24 '11 at 11:25
  • `readfile` sends the file chunked internally to the response stream, where as `file_get_contents` will read the file into the memory and then send it out via the buffer, using `readfile` will keep memory usage down. – RobertPitt May 24 '11 at 11:41
  • @RobertPitt - Yes, I do understand that (and, tbh, did when I typed it), thanks though -- keeping me honest. : ) – John Green May 24 '11 at 11:46
2

if you can just return the url to the flash it will be sufficient.

import flash.display.*;
 import flash.net.URLRequest;
 var rect:Shape = new Shape();
 rect.graphics.beginFill(0xFFFFFF);
 rect.graphics.drawRect(0, 0, 100, 100);
 rect.graphics.endFill();
 addChild(rect);
 var ldr:Loader = new Loader();
 ldr.mask = rect;
 var url:String = ""; //put the returned img url you got from the php here.
 var urlReq:URLRequest = new URLRequest(url);
 ldr.load(urlReq); //the loader will start loading the img
 addChild(ldr); //here you add the loader to stage. 

maybe for a millisec or two you just see nothing. But as soon as the loader has loaded the img you will see it.

You must input the returned img url. So not the url to the webpage that returns the img url.


If you combine the above with the answer of John Green - PageSpike you can use my code as long as the php page is that of the one in John Green - PageSpike his answer and you pass instead of the returned img then pass the url to the php page with parameter;

var url:String = "http://www.yoursite.com/getImage.php?imgParameter=image123";

So the url is now the link to the script of John Green - PageSpike which will return infact the image.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • he may not want to do that to prevent his private images directory from being sniffed. – RobertPitt May 24 '11 at 11:23
  • "What I want to do it be able to call a PHP page and send it a variable which I can do." Meaning that as long as the page is exposed everyone can get the image. Since I can just call the page in my browser and pass a get image. Or I can write a simple script to call the page with a POST var. So that doesnt matter. – SynerCoder May 24 '11 at 11:36
  • @RobertPitt, @SynerCoder - For context, I think he's actually working off of some previous advice that I gave him: http://stackoverflow.com/questions/6087598/as3-bitmap-storage/6087853#6087853. – John Green May 24 '11 at 11:48
1

As Jhon Green has mentioned, it will work

http://www.yourserver.com/filesforflash/?file_id={id}
header('Content-type: image/png');
readfile($file_name);

but some times you may need also need the above url will not work even if you send headers of content type image/png. Quick tip for that is to send file name itself instead if its id

http://www.yourserver.com/filesforflash/{id}-filename.png

For this you may need to use mod-rewrite.

Imran Naqvi
  • 2,202
  • 5
  • 26
  • 53