PHPSEEKER.ORG

A free resort for PHP Developers

Save image from FLEX into server's folder with PHP

Save image from FLEX into server's folder with PHP

Postby toshio » Mon Jan 25, 2010 10:10 pm

It' getting kinda boring writing on this forum. Besides 4K++ registered users, not a single soul contributes or comment.
But anyway, where goes one more tip:
=======================================================================================

This is a simple way to upload images or screenshots to your server without having to rely on URLReference. URLReference is used for download / upload files
which INTERACT with the user, meaning, the user browser the file which will be upload or selected the local where the file will be saved (download).

This method just send Base64 data to PHP (which is capable of decoding it) and write it into an image again. Let's cut the bull and go through the code:
(on this sample I'm using a screenshot taken from a component, webcam, whatever, and saving it to a folder called [userFolder] which is on the host);

The first thing you want to do is take a screenshot of whatever you want (var myCanvas:Canvas):
Supposed you put an event on a button to trigger the whole thing:

=================================================================================
FLEX PART
=================================================================================
Code: Select all
<mx:myButton label="Save Shot"
                      click="saveToServerHandler()" />


The you can start the handler like this:
Code: Select all
private function saveToServerHandler () : void
{
        // set the encoder type
        pngEnc = new PNGEncoder();
       
         // get the ByteArray: that's what we will need to encode to send to the PHP script
         var bss:ImageSnapshot = ImageSnapshot.captureImage(myCanvas, 300, pngEnc); 
       
       // myCanvas: the component which I want a screenshot
       // 300: DPI value
      // pngEnc: the encoder type. You could use JPG but you need th GD library
      //               on PHP enable to use GIF

         var bba:ByteArray = bss.data as ByteArray;
         
         // make a request to your PHP script: set the script's URL
         var request:URLRequest = new URLRequest('lib/engine/save_raw_file.php');
         
         // create variables which will be sent to PHP
         var vars:URLVariables = new URLVariables();
         vars.name = _USER_;
         vars.shopid = _SHOPID_;
         vars.binary = Base64.encodeByteArray(bba); // Base64 class can be found here [http://dynamicflash.com/goodies/base64]
         
         request.method = URLRequestMethod.POST;
         request.data = vars;
         var loader: URLLoader = new URLLoader(request);
         
         // you can set listeners to display some kind of message, even use the PROGRESS handler to display the load %
         // If you don't know how to display %, read my post about Flex Progress percentage
         
         // loader.addEventListener(Event.COMPLETE, saveFrontCompleteHandler);
        //  loader.addEventListener(ProgressEvent.PROGRESS, saveFrontProgressHandler); 
}   


That's it! Pretty simple and straight forwards. You can send more data if you need but lets keep things simple for now :)
Now comes the PHP part:
======================================================================
PHP
======================================================================
Code: Select all
<?php

$binary  = $_REQUEST['binary']; // this is the image encoded
$user    = $_REQUEST['name'];  // this is an user reference
$shopid = $_REQUEST['shopid']; // this is a reseller reference (for example)

// decode the data
$img_data = base64_decode($binary);

// measure data
$img_size = strlen($img_data);

// set the local and file name where you want to save the image. If you need JPG, change the encoder on Flex and switch PNG for JPG
$img_filename = '../' . $shopid . '/' . $user . '/' . mt_rand('00000','99999') . '_' . $user . '.png;

// save file
$img_file = fopen($img_filename,"w");

fwrite($img_file, $img_data);
fclose($img_file);
 
?>


Of course you want to validate the code before you actually save it. This is just a quick WORKING sample of how to do it.
I hope that this snap code save somebody's time!

Ah, and pleeeeeeeease, comment, post, write....
Thanks!
PHPSEEKER.ORG Moderator
toshio
Site Admin
 
Posts: 34
Joined: Sat Dec 13, 2008 8:22 pm

Return to PHP + Flex 3

Who is online

Users browsing this forum: No registered users and 1 guest

cron