Saving images from flash to server using PHP - 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

Related

Whether there generate QR code in my project directory will get mix when multi people using it?

I have a question about the application generate QR code image.
I have an application when clients click a button there will generate a QR code image, my way is store in the project library, then print <img> with the url to the screen. then clients can see it.
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
my code is bellow:
function generate_qrcode($url){
$filename = 'hante_qrcode.png';
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;
//generate QR code image
$o = QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
echo "<pre>";
print_r($o);
print_r('<img src="hante_qrcode.png">');
}
if there get mix, how to solve this problem?
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
yes
how to solve this problem?
there are two ways to solve this problem
you can provide unique name for every files like using timestamp using time() function or with user ID. cause as per you are passing parameters while generating qr code you need to store the file. without saving file also possible but in that case you can't configure pixel size and frame size. you can refer this for PHP QR code-Examples
don't store image on server and find some js to generate qr code directly from client side.
having a one demo for that check if you can use it
var qrcode = new QRCode("qrcode");
qrcode.makeCode('https://stackoverflow.com');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<div id="qrcode"></div>
Of course it will be overwritten.
Solution 1
Create unique filename for every image. This way you can save your images for use later. Another benefit of this, you don't have to create image again for same url.
$filename = md5($url) . ".png";
if(!file_exists($filename)){
$o = QRcode::png($url, $filename, ...);
}
echo '<img src="'.$filename.'">';
Solution 2
If you don't want to save images for disk space reasons you can serve image directly. In your code, user sends request to index.php and fetch image address as response. After then browser makes another request to get image. You can return image rather than returning html.
// image.php
// Still we want to give uniqe filename because we can get another request while one request is processing
$filename = md5(microtime) . "_qr.png";
$o = QRcode::png($url, $filename, ...);
$image = file_get_contents($filename);
// remove the file after stored in a variable
unlink($filename);
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($image));
echo $image;
// index.html
<img src="image.php?url=someurl">

Actionscript - AS 2.0 - PHP copy file from server to server THEN run function

Basic Idea: I have a flash file that takes screenshots with a click of a button, sending the data to a PHP file, and then the user gets to save a PNG image. The images that are merged together (via PHP) require that they reside on the same server as the PHP, otherwise they do not merge and the final PNG shows up blank.
My solution so far: I have two PHP files, and I just need to find a way to merge them. The screenshot one, and one that copies a file from one server to another. This is my cheat work around to bring the image to reside on the same server, THEN run the screenshot php.
The Server-to-Server PHP Code:
<?PHP
$inputfile = FOPEN("https://www.google.com/intl/en_com/images/srpr/logo3w.png", "r");
$outputfile = FOPEN("transferedfile.gif", "w");
ECHO "File opened...";
$data = '';
WHILE (!FEOF($inputfile)) {
$data .= FREAD($inputfile, 8192);
}
ECHO "Data read...";
FWRITE($outputfile, $data);
ECHO "transfered data";
FCLOSE ($inputfile);
FCLOSE ($outputfile);
ECHO "Done.";
?>
So as you can see, it pulls Google's logo and saves it as "transferedfile.gif" to the directory the PHP resides on. I can get this PHP code to work by saving this as whateverIWant.php on my webserver, and visiting it directly, but I need to in place of Google's logo (in this example) put a value that will be dynamically changing via flash.
So basically… in the flash file, I'll have a dyniamic variable where the URL will change, in short. So we'll just say that I define that variable in flash as var imageToGet so somehow I need to pass that variable into this PHP. That's one step... here's the AS 2.0 code:
My Actionscript (2.0) Code:
button.onRelease = function ():Void {
sendImageToServer();
ScreenShot.save(_root, "screenshot.png", 0, 0, 100, 140);
};
the sendImageToServer() function isn't made yet. This is where I'm stuck. I would need the sendImageToServer() function to send var imageToGet as what image to get, THEN run the ScreenShot.save() function after the transfer is done (aka FCLOSE ($outputfile); is complete)
In Summary: A movie clip on the stage will have a dynamic image loaded into it, that once a button is pressed, it would need to copy that dynamic image to the local server, and then run the screenShot function. I believe once I have this figured out, I should be able to do everything else, such as saving as a unique name, saving multiple files, etc. But I just need pushed in the right direction :)
Thanks so much everyone # StackOverflow. You've been nothing but awesome to me thus far!
EDIT -- I've found a good starting point!!
I found a good starting point, and am answering my own question in case someone else stumbles upon this. I used these two codes as a starting point, and I think I'm on the right track…
In Flash: I simply made a dynamic textbox with the instance name of traceText
In Actionscript (2.0):
var send:LoadVars = new LoadVars;
var receive:LoadVars = new LoadVars;
send.toPHP = "asd123";
receive.onLoad = function(){
encrypted = this.toFlash;
traceText.text = encrypted;
}
send.sendAndLoad("test.php",receive,"POST");
In "test.php" file:
$fromFlash = $_POST['toPHP'];
$encrypted = $fromFlash;
$toFlash = "&toFlash=";
$toFlash .= $encrypted;
echo $toFlash;
What this ended up doing was sending the variable to PHP and then back again. Which is perfect for what I needed. For now, I should be good! Hope this helps anyone that needs it.

Is there any way to receive an image file through Flex's HttpService?

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

AS3 Load PNG from 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.

How to save a snapshot of a SWF to the server with AS3?

I am facing the task of having to upload a snapshot to the server. But I don't want the user to download the image to their computer.
I have explored a few solutions of generating an image serverside with PHP, but they all seem to use a method where the server sends the image to the user.
See for instance: http://mattkenefick.com/blog/2008/11/06/saving-jpegs-with-flash/
I'm wondering if it's possible to save $GLOBALS["HTTP_RAW_POST_DATA"], which in that example contains the ByteArray sent by Flash, to the server as an image file....
Use php code that is along these lines to save the contents of $GLOBALS["HTTP_RAW_POST_DATA"]
// untested code
$imageBytes = $GLOBALS["HTTP_RAW_POST_DATA"]
// in real code you better create a new file for every upload :-)
$file = fopen("uploads/test.jpg", "w");
if(!fwrite($file, $imageBytes)){
return "Error writing to file: $file";
}
fclose($file);

Categories