AS3 Load PNG from PHP - php

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

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);

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.

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.

Related

How to dynmically draw picture in php gd

Hi I have searched the web for 2 days but did not accomplish what I am looking for.
I have an apache server which will be accessed by 146 students. the user picks an angle from dropdown lets say 45 degress, then user clicks CALCULATE button. Then user clicks DIAGRAM button to see how the sine graph looks like.
Works like charm when i write the image to a file e.g: imagepng($img,"diagram.png");
Now the problem is that the diagram.png will always get overwritten by the last user. So for example if another user logs in and calculates the Sin 135. Both users will see Sine 135 because filename is hardcoded since there is conflict of filename.
I have searched the web on how to create the image dynamically instead of writing to a file and then reading the file. I have come across the following but not working:
base64_encode and decode
What would I have to do to my code of imagepng(...., ...) mentioned above to make use of base64 so I can actually draw the picture of already processed data. Let assume if I comment out the imagepng(..) code, then what do I replace it with. I hope I don't have to change my code a whole lot.
Please help
thanks
Amit
The filename argument to imagepng is optional. From the manual:
filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
You would just need to send a png header at the top of the script and you would get the image as output for that script.
It's hard to tell without seeing you code how it is structured
but if once the user submits the form all you do is show the image by itself, then you can do something like this.
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
If you embed the image into an html page as the result, then your best best would be to change the url of the image on the success page to something like this.
<img src="/path/to/file.php?deg=45" />
Then in the file.php
$deg = $_GET['deg'] + 0; // make sure it is a number
$img= function_render_graph($deg);
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
By using a GET request, rather then a POST request then the image will likely be cached by the browser, so it doesn't need to be rendered each time. (Given that you have a drop list of angles, there must be a limited number of graphs that can actually be drawn)
Draw_Resultant_Prism_Graph (parameters)
{
$img = imagecreatetruecolor(800,750);
....
....
...
the following lines captures the data from output buffer and displays on same screen
***some version of IE have some issues mostly the dumb terminals where IE update is ADMIN
***restricted
ob_start();
header("Content-type: image/jpeg");
imagepng($img);
$output = ob_get_contents();
ob_end_clean();
imagedestroy($img);
echo img src="data:image/jpeg;base64,'.base64_encode($output).'"
user tags around img above and semicolon af
}

How can I pass a very long string from one page to another?

I'm making a data visualisation tool that works using SVG, d3.js and JQuery. I am currently making a feature to export (and download) as an SVG file:
// Code on main page
var svg = $("#svg-wrap").html();
var win = window.open("export.php?svg=" + svg, '_blank'); // _blank means export.php opens in a new tab
win.focus;
// Code in export.php
<?php
ob_start();
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=data.svg");
$svg = $_GET["svg"];
echo stripslashes($svg);
?>
This doesn't work though, because although some of the SVG is passed through, the full code is too long for a query string (or so it seems).
Is there some way that I can fix this? I could use compression, but that would only shrink it up to a limit and I think it would probably still be too long - the SVG code could be hundreds of lines :( .
Most UAs put limits on GET requests. Use POST instead.
You can store it in session variable and access it on other page... but not the best practice though...
You may want to use window.postMessage to handle the communication between your main app window and your popup, this way you could just stay on the client side only.
It is hard to tell the limitation,
a browser can have a limit, for post vs get
a proxy can have a limit, for post vs get
a web server can have a limit, for post vs get
the application itself can set a limit...
etc
As suggested, you could go with writing a svg file and send the url instead.
How big is the svg file?

Send Dynamically generated Image in Javascript as Image Header in PHP

What I am doing
Generating a dynamic image via javascript based on some variables specified in the query string. I am using this in a PHP Website.
What I need
The point here is I want to send this dynamically generated image in javascript to be displayed on the page as Image Header.
What I am using
PHP with APACHE Server (wamp)
Similar example of what I require
http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
This is a Google Chart API, upon hitting the above URL, it returns you a PNG image based on the parameters specified in the Query String.
I have tried a lot, but yet unable to reach the success point.
Just, to show you what I am doing here is the Code
My Code Portion
/*
Logic to generate a canvas based on some parameters goes here
*/
// Canvas to Image
var img = canvas.toDataURL("image/png");
/*
My goal is to set this image to header of the page,
so that when the page is hit, an image is returned,
just like the Google Chart API thing mentioned above
*/
Create a javascript variable like this:
var monthly ="value to pass";
var recent ="value to pass";
var imgURL="http://YOURSITEURL/FOLDER/app1?monthly="+monthly+"&recent="+recent+"&likes="+likes;
Or:
var image = document.createElement("img");
image.src ="http://117.204.247.139:8084/myapps/app?monthly="+monthly+"&recent="+recent+"&likes="+likes;
I am not completely sure If I understand what you want. But on the PHP side you can
generate a PNG or JPEG file with a library like GD or Imagemagick.
(http://php.net/manual/en/book.image.php)
You can then send the data back to the client by setting the correct header, for PNG:
<?php
header("Content-Type: image/png");
ob_start();
echo $pngdata; // the binary data
ob_end_flush();

Saving images from flash to server using PHP

I am trying to save some images from Flash to Php by sending a jpgstream, capturing it in php and pushing it to a file. Not sure what I am doing wrong here.
I am putting all the images I need into an array like so: (history is just where I am keeping all the image data)
for each($value in history)
{
var jpgSource:BitmapData = new BitmapData ($value.sourceImg.width, $value.sourceImg.height);
jpgSource.draw($value.sourceImg);
var encoder:JPEGEncoder = new JPEGEncoder(100);
var jpgStream:ByteArray = encoder.encode(jpgSource);
var imgDetailArr:Array = new Array(jpgStream, $value.name);
imgArr.push(imgDetailArr);
}
And then i send that to PHP using a remote object and amfphp:
rmObj.saveUserImages( imgArr);
On the php side I am doing this:
function saveUserImages( $imgArr)
{
foreach($imgArr as $value)
{
ob_start();
/* output image as JPEG */
$image = imagecreatefromjpeg($value[0]);
header('Content-type: image/jpeg');
imagejpeg( $image );
/* save output as file */
ob_flush();
file_put_contents( "images", ob_get_contents() );
}
}
But this doesn't seem to do the trick. I have been going through a bunch of different tutes and code snippets, so maybe I just ogt something confused along the way. I have done this before though and don't remember it being this difficult.
You may be better off calling JS from Flash and pass a path to the file via AJAX. Then, use PHP to upload the files directly. PHP has support for file uploads from a hard drive.
Edit:
On second thought, try switching ob_flush with the line after it. It looks like you are deleting your temporary data before saving it.
im no expert on flash but this seems to be doing what you are looking for ... thoughts?
http://henryjones.us/articles/using-the-as3-jpeg-encoder

How to load dynamic image w/php gd library, w/o saving it on server or having src="script.php"?

I would like to generate a dynamic image from a script, and then have it load to the browser without being persistent on the server.
However, I cannot call this by setting the image's src="script.php", since that would require running the script that just generated the page and its data all over again, just to get the final data that will generate the graph.
Is there a way to do this that is similar to setting image's src="script.php", but which is called from within another script, and just sends the image without saving it? I need access to the data that is used in the generation of the markup, in order to create this dynamic image.
Or, if not, what is the easiest way to destroy the image once the page is loaded? a quick ajax call?
Is there any way to cache certain data for some limited time frame in order for it to be available to some other script?
Any ideas would be greatly appreciated, as I'm having a really hard time finding the right solution to this...
Thanks!
You can inline the image into a <img> tag if you need to.
Like
<?php
$final_image_data; // Your image data, generated by GD
$base64_data = base64_encode($final_image_data);
echo "<img src=\"data:image/png;base64,{$base64_data}\" ... />";
?>
That should work on all modern browsers, and IE8. Doesn't work well with some email clients tho (Outlook, for one).
Also, another solution I found is to store the image in a session variable which is then called from a php script in the image tag. This would allow a user specific image to be served, and then removed from memory by the script... This also avoids messy img src="" tags...
Hopefully that is helpful to someone.
Use a rewrite rule.
RewriteRule ^magicimage.jpg$ /myscript.php
Then simply echo your image data from gd, instead of writing it to disk -- which is as simple as not providing a filename to the appropriate image*() function
myscript.php
<?php
$im = imagecreatetruecolor($w, $h);
//...do gd stuff...
header('Content-type: image/jpeg');
//this outputs the content directly to the browser
//without creating a temporary file or anything
imagejpeg($im);
And finally, utilize the above
display.php
<img src="magicimage.jpg">

Categories