PHP

equal function String.fromCharCode in PHP

Posted in PHP, actionscript 3 on February 17th, 2010 by admin – Be the first to comment

Hey it was problem before I see this solution, to pass charactes to PHP like numbers, basicly because Flash use fromCharCode in UTF-8 format and PHP do chr function work in ASCII.

So here is the script solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function uniord($ch) {

    $n = ord($ch{0});

    if ($n < 128) {
        return $n; // no conversion required
    }

    if ($n < 192 || $n > 253) {
        return false; // bad first byte || out of range
    }

    $arr = array(1 => 192, // byte position => range from
                 2 => 224,
                 3 => 240,
                 4 => 248,
                 5 => 252,
                 );

    foreach ($arr as $key => $val) {
        if ($n >= $val) { // add byte to the 'char' array
            $char[] = ord($ch{$key}) - 128;
            $range  = $val;
        } else {
            break; // save some e-trees
        }
    }

    $retval = ($n - $range) * pow(64, sizeof($char));

    foreach ($char as $key => $val) {
        $pow = sizeof($char) - ($key + 1); // invert key
        $retval += $val * pow(64, $pow);   // dark magic
    }

    return $retval;
}

found on : Unnamed – post number 1799714

Post PHP vars without form

Posted in PHP on September 18th, 2009 by admin – Comments Off

You have to have cURL enabled. Take a look

1
2
3
4
5
6
7
8
9
<?php
    $email = $_POST['email'];
   
    $ch = curl_init('http://www.aweber.com/scripts/addlead.pl');
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, "from=".$email."&name=somename&meta_web_form_id=1829234431&meta_split_id=&unit=rm101-sign-up&redirect=http://www.aweber.com/form/thankyou_vo.html&meta_redirect_onlist=&meta_adtracking=&meta_message=1&meta_required=from&meta_forward_vars=0");
    curl_exec ($ch);
    curl_close ($ch);
?>

Flash video Player Snapshot

Posted in PHP, actionscript 3, projects & snippets on September 14th, 2009 by admin – Comments Off

Hi I have recently working on saving different format images from flash. My previous work on this script was: Image Encode with Flash and PHP. Here you can create snapshots from video source.

Example

In this example you can save exported images also in your computer. I like this example more, because you can get different images, not boring same image again.

Screenshots:
snapshot file 1
snapshot file 2
snapshot file 3

Image Encode with Flash and PHP

Posted in PHP, actionscript 3 on September 10th, 2009 by admin – Comments Off

hi, this is a little project to save export/save images from flash.

This application allow you to see how to export in different image formats like: png, jpg and bmp.

I create class ImageCreator, whit it it’s this easy create images on server. You call the class like this:

1
2
var imagecreator:ImageCreator = new ImageCreator(image_spr, ImageCreator.EXPORT_TYPE_PNG, name);
imagecreator.addEventListener(ImageCreator.EVENT_COMPLETE_SAVING, completeWritingEvent);

Event ImageCreator.EVENT_COMPLETE_SAVING, will be dispatched when image is correctly saved on your server.
This is ImageCreator Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.stanislavstankov.images {
    import com.adobe.images.*;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
    import flash.utils.ByteArray;
   
    public final class ImageCreator extends EventDispatcher {
       
        public static const EVENT_COMPLETE_SAVING:String = "eventCompleteSaving";
       
        public static const EXPORT_TYPE_JPG:int = 0;
        public static const EXPORT_TYPE_PNG:int = 1;
        public static const EXPORT_TYPE_BMP:int = 2;
       
        private const _FOLDER_FILE:String = "http://projects.stanislavstankov.com/imageEncoder/php";
        private const _PHP_FILE:String = "/imgEncoder.php";
       
       
        private var _filename:String;
       
        /*------------------------------------
            Constructor
        ------------------------------------*/

        /**
         * ImageCreator will create image on the server where the php file is.
         * Event EVENT_COMPLETE_SAVING, will be dispatched when image is created on server.
         * Also there is one get propert filepath, that will return the path where the image is saved.
         * @param   Dispaly object - sprite or moviclip that will be exported for image
         * @param   type of image that will be saved
         * @param   name of the file
         * @param   background color if transperent, leave it 0. If you need background change the value. It is ARGB type.
         */

        public function ImageCreator($displayObjectExport_:Sprite, $exportType_:int, $name_:String, $fillColorARGB_:uint = 0x0):void {
            var transperentFlag:Boolean = false;
            if ($exportType_ == EXPORT_TYPE_PNG) {
                transperentFlag = true;
            }
           
            const bmpdata:BitmapData = new BitmapData($displayObjectExport_.width, $displayObjectExport_.height, transperentFlag, $fillColorARGB_);
            bmpdata.draw($displayObjectExport_);
           
            createImage(bmpdata, $exportType_, $name_);
        }
        /*------------------------------------
            Public methods
        ------------------------------------*/

       
        public function get filepath():String {
            return _filename;
        }
       
       
        /*------------------------------------
            Private methods
        ------------------------------------*/

       
        private function createImage($bmpdata_:BitmapData, $exportType_:int, $name_:String):void {
            var byteArray:ByteArray;
            var contentType:String;
           
            switch ($exportType_) {
                case EXPORT_TYPE_JPG:
                    var jpgencode:JPGEncoder = new JPGEncoder(100);
                    byteArray = jpgencode.encode($bmpdata_);
                    contentType = 'image/jpg';
                    $name_ += ".jpg";
                break;
                case EXPORT_TYPE_PNG:
                    byteArray = PNGEncoder.encode($bmpdata_);
                    contentType = 'image/png';
                    $name_ += ".png";
                break;
                case EXPORT_TYPE_BMP:
                    byteArray = BMPEncoder.encode($bmpdata_);
                    contentType = 'image/bmp';
                    $name_ += ".bmp";
                break;
                default:
               
                break;
            }
           
            const request:URLRequest = new URLRequest(_FOLDER_FILE + _PHP_FILE + "?name=" + $name_);
            request.contentType = contentType;
            request.method = URLRequestMethod.POST;
            request.data = byteArray;
           
            const loader:URLLoader = new URLLoader(request);
            loader.addEventListener(Event.COMPLETE, loadResultHandler);
        }
       
       
        /*------------------------------------
            Event Functions
        ------------------------------------*/

       
        private function loadResultHandler(info:Event):void {
            info.target.removeEventListener(Event.COMPLETE, loadResultHandler);
            var urlvars:URLVariables = new URLVariables(info.target.data);
            _filename = _FOLDER_FILE + "/" + urlvars.filename;
           
            dispatchEvent(new Event(EVENT_COMPLETE_SAVING));
        }
       
       
    }
}

PHP imgEncoder.php file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {
    //$fullFilePath = dirname(__FILE__) . "/" . $_GET['name'];
    $fullFilePath = $_GET['name'];
   
    $handle = fopen($fullFilePath,"w");
    fwrite($handle, $GLOBALS["HTTP_RAW_POST_DATA"]);
    fclose($handle);
   
    echo "result=1&filename=" . $fullFilePath;
} else {
    echo 'result=0';
}
?>

Source file here

Edit 14.09.2009
Flash video Player Snapshot

Getting data from XML, AS3

Posted in PHP, actionscript 3 on June 16th, 2009 by admin – Comments Off

Actionscript3 & PHP & MySQL & XML

I’m gonna post series of tutorials:

Getting data from XML, AS3
Getting data from MySQL, AS3 + PHP + MySQL file
Uploading file with AS3 + PHP
Send email with AS3 + PHP

How to parse a simple XML file. This is our structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8" ?>
<data>
    <users>
        <user>
            <id>18</id>
            <name>bobanman</name>
            <avatar><![CDATA[]]></avatar>
            <registerData>13-07-2008</registerData>
            <email>bobanman@msn.com</email>
        </user>
        <user>
            <id>20</id>
            <name>ilya</name>
            <avatar><![CDATA[]]></avatar>
            <registerData>15-07-2008</registerData>
            <email>ilya@asymmetric.co.il</email>
        </user>
    </users>
</data>

First we will build XML parser. This is my favorite ways to parse it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// parce XML
private function parseXML($rawData_:String):void {
    var xmldoc:XMLDocument = new XMLDocument();
    xmldoc.ignoreWhite = true;
    xmldoc.parseXML($rawData_);
   
    var out:Array = [];
    const imax:int = xmldoc.firstChild.firstChild.childNodes.length;
    for (var i:int = 0; i < imax; i++ ) {
        //collect data from XML
        out[i] = getUserDTO(xmldoc.firstChild.firstChild.childNodes[i]);
    }
   
    // all user data goes here :)
    trace(out);
}

// create UserDTO from XMLNode
private function getUserDTO($xmlNode_:XMLNode):UserDTO {
    return new UserDTO(int(XMLNode($xmlNode_.childNodes[0]).firstChild.toString()),
    XMLNode($xmlNode_.childNodes[1]).firstChild.toString(),
    XMLNode($xmlNode_.childNodes[2]).firstChild.toString(),
    XMLNode($xmlNode_.childNodes[3]).firstChild.toString(),
    XMLNode($xmlNode_.childNodes[4]).firstChild.toString());
}

You can also parse it using XML class, but like I say I prefer to do it this way :)

so with this script we will retrieve all the data from XML file.
This is one way also to get data from DB, create with server side language xml files like this and to parce them. Other options are AMFPHP or ASQL but we will cover these two in future topics.
So the we have to load the XML and get set all data into lets say array object.

So full script goes like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.xml.XMLDocument;
    import flash.xml.XMLNode;
   
    [SWF(frameRate = '30')]
   
    public final class DocumentClass extends Sprite {
       
       
        public function DocumentClass():void {
            loadXML();
        }
       
        // load external XML
        private function loadXML():void {
            var ulloader:URLLoader = new URLLoader(new URLRequest("data.xml"));
            ulloader.addEventListener(Event.COMPLETE, completeHandler);
        }
       
        // parce XML
        private function parseXML($rawData_:String):void {
            var xmldoc:XMLDocument = new XMLDocument();
            xmldoc.ignoreWhite = true;
            xmldoc.parseXML($rawData_);
           
            var out:Array = [];
            const imax:int = xmldoc.firstChild.firstChild.childNodes.length;
            for (var i:int = 0; i < imax; i++ ) {
                //collect data from XML
                out[i] = getUserDTO(xmldoc.firstChild.firstChild.childNodes[i]);
            }
           
            // all user data goes here :)
            trace(out);
        }
       
        // create UserDTO from XMLNode
        private function getUserDTO($xmlNode_:XMLNode):UserDTO {
            return new UserDTO(int(XMLNode($xmlNode_.childNodes[0]).firstChild.toString()), XMLNode($xmlNode_.childNodes[1]).firstChild.toString(), XMLNode($xmlNode_.childNodes[2]).firstChild.toString(), XMLNode($xmlNode_.childNodes[3]).firstChild.toString(), XMLNode($xmlNode_.childNodes[4]).firstChild.toString());
        }
       
        // exterenal xml loaded. Parse it then
        private function completeHandler(info:Event):void {
            info.target.removeEventListener(Event.COMPLETE, completeHandler);
           
            parseXML(info.target.data);
        }
    }
}

class UserDTO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package {
   
    public final class UserDTO {
       
        private var _id:int;
        private var _name:String;
        private var _avatarURL:String;
        private var _regDate:String;
        private var _email:String;
       
        public function UserDTO($id_:int, $name_:String, $avatarURL_:String, $regDate_:String, $email_:String):void {
            _id = $id_;
            _name = $name_;
            _avatarURL = $avatarURL_;
            _regDate = $regDate_;
            _email = $email_;
        }
       
        public function get id():int {
            return _id;
        }
       
        public function get name():String {
            return _name;
        }
       
        public function get avatarURL():String {
            return _avatarURL;
        }
       
        public function get regDate():String {
            return _regDate;
        }
       
        public function get email():String {
            return _email;
        }
       
        public function toString():String {
            return "[ UserDTO id='" + _id + "' name='" + _name + "' avatarURL='" + _avatarURL + "' regDate='" + _regDate + "' email='" + _email + "' ]";
        }
       
    }
}

So now lets create PHP file that give us the date from MySQL with same structure.
Getting data from Mysql, AS3 + PHP + MySQL file