From 538085b851f32f7daf756462ce3aaf66154f7a98 Mon Sep 17 00:00:00 2001 From: Luuk Verhoeven Date: Thu, 7 Jun 2018 14:14:37 +0200 Subject: [PATCH] Update structure --- blocks/mfavatar/README.txt => README.txt | 0 .../block_mfavatar.php => block_mfavatar.php | 186 +-- {blocks/mfavatar/db => db}/access.php | 114 +- {blocks/mfavatar/js => js}/swfobject.js | 0 .../lang => lang}/en/block_mfavatar.php | 102 +- .../lang => lang}/nl/block_mfavatar.php | 86 +- blocks/mfavatar/locallib.php => locallib.php | 118 +- blocks/mfavatar/module.js => module.js | 326 ++--- blocks/mfavatar/renderer.php => renderer.php | 112 +- blocks/mfavatar/settings.php => settings.php | 60 +- .../mfavatar/source => source}/MyButton.as | 46 +- .../com/adobe/images/BitString.as | 76 +- .../com/adobe/images/JPGEncoder.as | 1296 ++++++++--------- .../com/adobe/images/PNGEncoder.as | 280 ++-- .../org/phprpc/util/Base64.as | 288 ++-- {blocks/mfavatar/source => source}/script.as | 438 +++--- .../mfavatar/source => source}/snapshot.fla | Bin .../mfavatar/source => source}/snapshot.swf | Bin blocks/mfavatar/styles.css => styles.css | 44 +- .../mfavatar/swf => swf}/expressInstall.swf | Bin {blocks/mfavatar/swf => swf}/snapshot.swf | Bin blocks/mfavatar/upload.php => upload.php | 176 +-- blocks/mfavatar/version.php => version.php | 62 +- blocks/mfavatar/view.php => view.php | 104 +- 24 files changed, 1957 insertions(+), 1957 deletions(-) rename blocks/mfavatar/README.txt => README.txt (100%) rename blocks/mfavatar/block_mfavatar.php => block_mfavatar.php (97%) rename {blocks/mfavatar/db => db}/access.php (97%) rename {blocks/mfavatar/js => js}/swfobject.js (100%) rename {blocks/mfavatar/lang => lang}/en/block_mfavatar.php (97%) rename {blocks/mfavatar/lang => lang}/nl/block_mfavatar.php (97%) rename blocks/mfavatar/locallib.php => locallib.php (97%) rename blocks/mfavatar/module.js => module.js (97%) rename blocks/mfavatar/renderer.php => renderer.php (97%) rename blocks/mfavatar/settings.php => settings.php (97%) rename {blocks/mfavatar/source => source}/MyButton.as (93%) rename {blocks/mfavatar/source => source}/com/adobe/images/BitString.as (97%) rename {blocks/mfavatar/source => source}/com/adobe/images/JPGEncoder.as (96%) rename {blocks/mfavatar/source => source}/com/adobe/images/PNGEncoder.as (97%) rename {blocks/mfavatar/source => source}/org/phprpc/util/Base64.as (97%) rename {blocks/mfavatar/source => source}/script.as (96%) rename {blocks/mfavatar/source => source}/snapshot.fla (100%) rename {blocks/mfavatar/source => source}/snapshot.swf (100%) rename blocks/mfavatar/styles.css => styles.css (95%) rename {blocks/mfavatar/swf => swf}/expressInstall.swf (100%) rename {blocks/mfavatar/swf => swf}/snapshot.swf (100%) rename blocks/mfavatar/upload.php => upload.php (96%) rename blocks/mfavatar/version.php => version.php (97%) rename blocks/mfavatar/view.php => view.php (97%) diff --git a/blocks/mfavatar/README.txt b/README.txt similarity index 100% rename from blocks/mfavatar/README.txt rename to README.txt diff --git a/blocks/mfavatar/block_mfavatar.php b/block_mfavatar.php similarity index 97% rename from blocks/mfavatar/block_mfavatar.php rename to block_mfavatar.php index 50e236d..0dce5ed 100644 --- a/blocks/mfavatar/block_mfavatar.php +++ b/block_mfavatar.php @@ -1,94 +1,94 @@ -. - -/** - * Snapshot block contains the button to go to snapshot view page - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -defined('MOODLE_INTERNAL') || die(); - -class block_mfavatar extends block_base { - - function init() { - $this->title = get_string('pluginname', 'block_mfavatar'); - } - - function instance_allow_multiple() { - return false; - } - - function has_config() { - return true; - } - - function applicable_formats() { - return array( - 'my' => true, - 'all' => true, - ); - } - - function instance_allow_config() { - return true; - } - - function specialization() { - - // load userdefined title and make sure it's never empty - if (empty($this->config->title)) { - $this->title = get_string('pluginname', 'block_mfavatar'); - } else { - $this->title = $this->config->title; - } - } - - function get_content() { - global $CFG, $COURSE; - - require_once $CFG->libdir . '/formslib.php'; - - if ($this->content !== null) { - return $this->content; - } - - $systemcontext = context_system::instance(); - if ((!isloggedin() || isguestuser() || !has_capability('block/mfavatar:view', $systemcontext)) || !has_capability('moodle/user:editownprofile', $systemcontext) || $CFG->disableuserimages) { - $this->content = new stdClass(); - $this->content->text = ''; - - return $this->content; - } - - $this->content = new stdClass(); - $this->content->text = '
-
-
- - - -
-
-
'; - $this->content->footer = ''; - - return $this->content; - } +. + +/** + * Snapshot block contains the button to go to snapshot view page + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +defined('MOODLE_INTERNAL') || die(); + +class block_mfavatar extends block_base { + + function init() { + $this->title = get_string('pluginname', 'block_mfavatar'); + } + + function instance_allow_multiple() { + return false; + } + + function has_config() { + return true; + } + + function applicable_formats() { + return array( + 'my' => true, + 'all' => true, + ); + } + + function instance_allow_config() { + return true; + } + + function specialization() { + + // load userdefined title and make sure it's never empty + if (empty($this->config->title)) { + $this->title = get_string('pluginname', 'block_mfavatar'); + } else { + $this->title = $this->config->title; + } + } + + function get_content() { + global $CFG, $COURSE; + + require_once $CFG->libdir . '/formslib.php'; + + if ($this->content !== null) { + return $this->content; + } + + $systemcontext = context_system::instance(); + if ((!isloggedin() || isguestuser() || !has_capability('block/mfavatar:view', $systemcontext)) || !has_capability('moodle/user:editownprofile', $systemcontext) || $CFG->disableuserimages) { + $this->content = new stdClass(); + $this->content->text = ''; + + return $this->content; + } + + $this->content = new stdClass(); + $this->content->text = '
+
+
+ + + +
+
+
'; + $this->content->footer = ''; + + return $this->content; + } } \ No newline at end of file diff --git a/blocks/mfavatar/db/access.php b/db/access.php similarity index 97% rename from blocks/mfavatar/db/access.php rename to db/access.php index dc4b181..38bc381 100644 --- a/blocks/mfavatar/db/access.php +++ b/db/access.php @@ -1,58 +1,58 @@ -. - -/** - * Snapshot block access settings - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -defined('MOODLE_INTERNAL') || die(); - -$capabilities = array( - 'block/mfavatar:myaddinstance' => array( - 'captype' => 'write', - 'contextlevel' => CONTEXT_SYSTEM, - 'archetypes' => array( - 'user' => CAP_ALLOW - ), - 'clonepermissionsfrom' => 'moodle/my:manageblocks' - ), - 'block/mfavatar:addinstance' => array( - 'riskbitmask' => RISK_SPAM | RISK_XSS, - 'captype' => 'write', - 'contextlevel' => CONTEXT_BLOCK, - 'archetypes' => array( - 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW - ), - 'clonepermissionsfrom' => 'moodle/site:manageblocks' - ), - 'block/mfavatar:view' => array( - 'riskbitmask' => RISK_SPAM | RISK_XSS, - 'captype' => 'write', - 'contextlevel' => CONTEXT_BLOCK, - 'archetypes' => array( - 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW, - 'user' => CAP_ALLOW - ), - 'clonepermissionsfrom' => 'moodle/site:manageblocks' - ), +. + +/** + * Snapshot block access settings + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +defined('MOODLE_INTERNAL') || die(); + +$capabilities = array( + 'block/mfavatar:myaddinstance' => array( + 'captype' => 'write', + 'contextlevel' => CONTEXT_SYSTEM, + 'archetypes' => array( + 'user' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'moodle/my:manageblocks' + ), + 'block/mfavatar:addinstance' => array( + 'riskbitmask' => RISK_SPAM | RISK_XSS, + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'moodle/site:manageblocks' + ), + 'block/mfavatar:view' => array( + 'riskbitmask' => RISK_SPAM | RISK_XSS, + 'captype' => 'write', + 'contextlevel' => CONTEXT_BLOCK, + 'archetypes' => array( + 'editingteacher' => CAP_ALLOW, + 'manager' => CAP_ALLOW, + 'user' => CAP_ALLOW + ), + 'clonepermissionsfrom' => 'moodle/site:manageblocks' + ), ); \ No newline at end of file diff --git a/blocks/mfavatar/js/swfobject.js b/js/swfobject.js similarity index 100% rename from blocks/mfavatar/js/swfobject.js rename to js/swfobject.js diff --git a/blocks/mfavatar/lang/en/block_mfavatar.php b/lang/en/block_mfavatar.php similarity index 97% rename from blocks/mfavatar/lang/en/block_mfavatar.php rename to lang/en/block_mfavatar.php index 7050cb7..6945fca 100644 --- a/blocks/mfavatar/lang/en/block_mfavatar.php +++ b/lang/en/block_mfavatar.php @@ -1,51 +1,51 @@ -. - -/** - * EN lang - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -$string['mfavatar:addinstance'] = 'Add a avatar snapshot tool'; -$string['mfavatar:myaddinstance'] = 'Add a new avatar snapshot block to My home'; -$string['mfavatar:view'] = 'View Mfavatar'; - -$string['pluginname'] = 'MoodleFreak Avatar'; -$string['makesnapshot'] = 'Make Snapshot'; -$string['snapshotpage'] = 'Snapshot'; -$string['installflash'] = 'Please install flash'; - -$string['flash:textselectdevice'] = 'Please select your camera device:'; -$string['flash:text_make_snapshot'] = 'Save snapshot'; -$string['flash:text_feed_field'] = 'Your camera feed'; -$string['flash:text_result_field'] = 'Your result'; -$string['flash:failed_saving'] = 'Error saving your snapshot!!'; -$string['flash:success_saving'] = 'Snapshot saved!!!'; - -$string['failed:disableuserimages'] = 'Error: Site has disabeld user images'; -$string['failed'] = 'Error: Failed Uploading'; -$string['failed:sesskey'] = 'Error: failed saving you are still logged? Refresh the page and retry'; -$string['failed:permission_editownprofile'] = 'Error: User can\'t change their images'; - -$string['pluginname_desc'] = 'Customize MoodleFreak avatar below.'; -$string['webrtc_enabled'] = 'WebRTC (modern) browser will support this'; - - - +. + +/** + * EN lang + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +$string['mfavatar:addinstance'] = 'Add a avatar snapshot tool'; +$string['mfavatar:myaddinstance'] = 'Add a new avatar snapshot block to My home'; +$string['mfavatar:view'] = 'View Mfavatar'; + +$string['pluginname'] = 'MoodleFreak Avatar'; +$string['makesnapshot'] = 'Make Snapshot'; +$string['snapshotpage'] = 'Snapshot'; +$string['installflash'] = 'Please install flash'; + +$string['flash:textselectdevice'] = 'Please select your camera device:'; +$string['flash:text_make_snapshot'] = 'Save snapshot'; +$string['flash:text_feed_field'] = 'Your camera feed'; +$string['flash:text_result_field'] = 'Your result'; +$string['flash:failed_saving'] = 'Error saving your snapshot!!'; +$string['flash:success_saving'] = 'Snapshot saved!!!'; + +$string['failed:disableuserimages'] = 'Error: Site has disabeld user images'; +$string['failed'] = 'Error: Failed Uploading'; +$string['failed:sesskey'] = 'Error: failed saving you are still logged? Refresh the page and retry'; +$string['failed:permission_editownprofile'] = 'Error: User can\'t change their images'; + +$string['pluginname_desc'] = 'Customize MoodleFreak avatar below.'; +$string['webrtc_enabled'] = 'WebRTC (modern) browser will support this'; + + + diff --git a/blocks/mfavatar/lang/nl/block_mfavatar.php b/lang/nl/block_mfavatar.php similarity index 97% rename from blocks/mfavatar/lang/nl/block_mfavatar.php rename to lang/nl/block_mfavatar.php index 5582d52..8cbd345 100644 --- a/blocks/mfavatar/lang/nl/block_mfavatar.php +++ b/lang/nl/block_mfavatar.php @@ -1,44 +1,44 @@ -. - -/** - * NL lang - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -$string['mfavatar:addinstance'] = 'Voeg avatar snapshot tool toe'; -$string['mfavatar:myaddinstance'] = 'Voeg een nieuwe avatar snapshot blok toe aan Mijn startpagina'; -$string['mfavatar:view'] = 'MoodleFreak Avatar weergeven'; -$string['pluginname'] = 'MoodleFreak Avatar'; -$string['makesnapshot'] = 'Maak Snapshot'; -$string['snapshotpage'] = 'Snapshot'; -$string['installflash'] = 'Installeer flash'; - -$string['flash:textselectdevice'] = 'Selecteer uw camera'; -$string['flash:text_make_snapshot'] = 'Opslaan'; -$string['flash:text_feed_field'] = 'Camera voorbeeld'; -$string['flash:text_result_field'] = 'Resultaat'; -$string['flash:failed_saving'] = 'Het is niet gelukt om snapshot op te slaan'; -$string['flash:success_saving'] = 'Snapshot is opgeslagen als uw avatar'; - -$string['failed:disableuserimages'] = 'Fout: gebruikers afbeelding zijn uitgeschakeld op de site.'; -$string['failed'] = 'Fout: uploaden niet gelukt!'; -$string['failed:sesskey'] = 'Fout: bent u nog steeds ingelogd? Herlaad de pagina en probeer nogmaals.'; +. + +/** + * NL lang + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +$string['mfavatar:addinstance'] = 'Voeg avatar snapshot tool toe'; +$string['mfavatar:myaddinstance'] = 'Voeg een nieuwe avatar snapshot blok toe aan Mijn startpagina'; +$string['mfavatar:view'] = 'MoodleFreak Avatar weergeven'; +$string['pluginname'] = 'MoodleFreak Avatar'; +$string['makesnapshot'] = 'Maak Snapshot'; +$string['snapshotpage'] = 'Snapshot'; +$string['installflash'] = 'Installeer flash'; + +$string['flash:textselectdevice'] = 'Selecteer uw camera'; +$string['flash:text_make_snapshot'] = 'Opslaan'; +$string['flash:text_feed_field'] = 'Camera voorbeeld'; +$string['flash:text_result_field'] = 'Resultaat'; +$string['flash:failed_saving'] = 'Het is niet gelukt om snapshot op te slaan'; +$string['flash:success_saving'] = 'Snapshot is opgeslagen als uw avatar'; + +$string['failed:disableuserimages'] = 'Fout: gebruikers afbeelding zijn uitgeschakeld op de site.'; +$string['failed'] = 'Fout: uploaden niet gelukt!'; +$string['failed:sesskey'] = 'Fout: bent u nog steeds ingelogd? Herlaad de pagina en probeer nogmaals.'; $string['failed:permission_editownprofile'] = 'Fout: gebruikers kunnen hun afbeelding niet zelf bewerken.'; \ No newline at end of file diff --git a/blocks/mfavatar/locallib.php b/locallib.php similarity index 97% rename from blocks/mfavatar/locallib.php rename to locallib.php index 36b1b1e..0f80fcd 100644 --- a/blocks/mfavatar/locallib.php +++ b/locallib.php @@ -1,60 +1,60 @@ -. - -/** - * block based function - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -defined('MOODLE_INTERNAL') || die(); - -/** - * load the module needed to make snapshots - */ -function block_mfavatar_add_javascript_module() { - global $PAGE, $CFG, $USER; - - $config = get_config('block_mfavatar'); - - // load swfobject 2.2 - $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/blocks/mfavatar/js/swfobject.js'), true); - - $jsmodule = array( - 'name' => 'block_mfavatar', - 'fullpath' => '/blocks/mfavatar/module.js', - 'requires' => array( 'io-base',) - ); - - $PAGE->requires->js_init_call('M.block_mfavatar.init', array( - $CFG->wwwroot . '/blocks/mfavatar/swf/snapshot.swf?' . time(), - $CFG->wwwroot . '/blocks/mfavatar/swf/expressInstall.swf', - array( - 'sessionid' => $USER->sesskey, - 'uploadPath' => $CFG->wwwroot . '/blocks/mfavatar/upload.php', - 'text_select_device' => get_string('flash:textselectdevice', 'block_mfavatar'), - 'text_make_snapshot' => get_string('flash:text_make_snapshot', 'block_mfavatar'), - 'text_result_field' => get_string('flash:text_result_field', 'block_mfavatar'), - 'text_feed_field' => get_string('flash:text_feed_field', 'block_mfavatar'), - 'failed_saving' => get_string('flash:failed_saving', 'block_mfavatar'), - 'success_saving' => get_string('flash:success_saving', 'block_mfavatar'), - ), - $config->webrtc_enabled - ), false, $jsmodule); +. + +/** + * block based function + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +defined('MOODLE_INTERNAL') || die(); + +/** + * load the module needed to make snapshots + */ +function block_mfavatar_add_javascript_module() { + global $PAGE, $CFG, $USER; + + $config = get_config('block_mfavatar'); + + // load swfobject 2.2 + $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/blocks/mfavatar/js/swfobject.js'), true); + + $jsmodule = array( + 'name' => 'block_mfavatar', + 'fullpath' => '/blocks/mfavatar/module.js', + 'requires' => array( 'io-base',) + ); + + $PAGE->requires->js_init_call('M.block_mfavatar.init', array( + $CFG->wwwroot . '/blocks/mfavatar/swf/snapshot.swf?' . time(), + $CFG->wwwroot . '/blocks/mfavatar/swf/expressInstall.swf', + array( + 'sessionid' => $USER->sesskey, + 'uploadPath' => $CFG->wwwroot . '/blocks/mfavatar/upload.php', + 'text_select_device' => get_string('flash:textselectdevice', 'block_mfavatar'), + 'text_make_snapshot' => get_string('flash:text_make_snapshot', 'block_mfavatar'), + 'text_result_field' => get_string('flash:text_result_field', 'block_mfavatar'), + 'text_feed_field' => get_string('flash:text_feed_field', 'block_mfavatar'), + 'failed_saving' => get_string('flash:failed_saving', 'block_mfavatar'), + 'success_saving' => get_string('flash:success_saving', 'block_mfavatar'), + ), + $config->webrtc_enabled + ), false, $jsmodule); } \ No newline at end of file diff --git a/blocks/mfavatar/module.js b/module.js similarity index 97% rename from blocks/mfavatar/module.js rename to module.js index 2c6c527..05e29df 100644 --- a/blocks/mfavatar/module.js +++ b/module.js @@ -1,164 +1,164 @@ -/** - * adding the flash container to view page also this will try to update img.profilepic - * Added support for detecting webrtc most modern browser will support this - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -M.block_mfavatar = { - log : function (val) - { - try{ - console.log(val); - } catch (e) { - - } - }, - init : function (Y, applicationpath, expresspath, flashvars, supportwebrtc){ - - supportwebrtc = (supportwebrtc == 1) ? true : false; - - if (this.webrtc_is_supported() && supportwebrtc){ - - M.block_mfavatar.log('We have support for Webrtc'); - Y.one('#snapshotholder_webrtc').setStyle('display', 'block'); - - this.webrtc_load(flashvars); - }else{ - - M.block_mfavatar.log('Default using flash for Webcam '); - Y.one('#snapshotholder').setStyle('display', 'block'); - - swfobject.embedSWF( - applicationpath, - "snapshot", "100%", "100%", "11.1.0", - expresspath, - flashvars, { - menu : "false", - scale : "noScale", - allowFullscreen : "true", - allowScriptAccess: "always", - wmode : "transparent", - bgcolor : "#fff" - },{ - id: "snapshot" - }, function (e){ - //we are loaded? - // this.log(e); - }); - } - }, - webrtc_load : function (flashvars) - { - var snapshotButton = document.querySelector('button#snapshot'); - var video = window.video = document.querySelector('video'); - var canvasrender = window.canvas = document.querySelector('canvas#render'); - var canvaspreview = window.canvas = document.querySelector('canvas#preview'); - - snapshotButton.onclick = function () - { - canvasrender.width = video.videoWidth; - canvasrender.height = video.videoHeight; - - // video size - canvasrender.getContext('2d').drawImage(video, 0, 0, canvasrender.width, canvasrender.height); - - // preview small - canvaspreview.getContext('2d').drawImage(video, 0, 0,canvaspreview.width, canvaspreview.height); - // set saved text - canvaspreview.getContext('2d').font = "30px Comic Sans MS"; - canvaspreview.getContext('2d').fillStyle = "white"; - canvaspreview.getContext('2d').textAlign = "center"; - canvaspreview.getContext('2d').fillText("Saved!", canvas.width/2, canvas.height/2); - - var data = canvasrender.toDataURL('image/png'); - YUI().use('io-base', function (Y) - { - // saving the file - var cfg = { - method : 'POST', - data: {'sesskey': flashvars.sessionid, 'file': data} - }; - var request = Y.io(flashvars.uploadPath, cfg); - - //on completed request - Y.on('io:complete', onComplete, Y); - }); - }; - - navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; - - var constraints = { - audio: false, - "video": { - "mandatory": { - "minWidth": "480", - "minHeight": "480", - "minFrameRate": "30", - "minAspectRatio": "1", - "maxWidth": "480", - "maxHeight": "480", - "maxFrameRate": "30", - "maxAspectRatio": "1" - }, - "optional": [] - } - }; - - function successCallback(stream){ - - window.stream = stream; // make stream available to browser console - if (window.URL){ - video.src = window.URL.createObjectURL(stream); - }else{ - video.src = stream; - } - } - - function onComplete(transactionid, response, arguments) - { - try{ - var json = JSON.parse(response.response); - - if(json.status == true) { - //reload profile picture - M.block_mfavatar.saved(); - } - M.block_mfavatar.log(json); - } catch (exc){ - console.log(exc); - } - } - - function errorCallback(error) { - console.log('navigator.getUserMedia error: ', error); - } - - navigator.getUserMedia(constraints, successCallback, errorCallback); - }, - webrtc_is_supported: function () { - return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); - }, - saved : function () { - this.log('Saved!!!'); - var profilePicture = Y.one('img.profilepic'); - if (profilePicture) - { - var src = profilePicture.getAttribute('src'); - profilePicture.setAttribute('src', ''); - setTimeout(function () - { - var now = new Date().getTime() / 1000; - profilePicture.setAttribute('src', src + '&c=' + now); - }, 500); - - } - }, - error : function (err){ - M.block_mfavatar.log('Error!'); - M.block_mfavatar.log(err); - } +/** + * adding the flash container to view page also this will try to update img.profilepic + * Added support for detecting webrtc most modern browser will support this + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +M.block_mfavatar = { + log : function (val) + { + try{ + console.log(val); + } catch (e) { + + } + }, + init : function (Y, applicationpath, expresspath, flashvars, supportwebrtc){ + + supportwebrtc = (supportwebrtc == 1) ? true : false; + + if (this.webrtc_is_supported() && supportwebrtc){ + + M.block_mfavatar.log('We have support for Webrtc'); + Y.one('#snapshotholder_webrtc').setStyle('display', 'block'); + + this.webrtc_load(flashvars); + }else{ + + M.block_mfavatar.log('Default using flash for Webcam '); + Y.one('#snapshotholder').setStyle('display', 'block'); + + swfobject.embedSWF( + applicationpath, + "snapshot", "100%", "100%", "11.1.0", + expresspath, + flashvars, { + menu : "false", + scale : "noScale", + allowFullscreen : "true", + allowScriptAccess: "always", + wmode : "transparent", + bgcolor : "#fff" + },{ + id: "snapshot" + }, function (e){ + //we are loaded? + // this.log(e); + }); + } + }, + webrtc_load : function (flashvars) + { + var snapshotButton = document.querySelector('button#snapshot'); + var video = window.video = document.querySelector('video'); + var canvasrender = window.canvas = document.querySelector('canvas#render'); + var canvaspreview = window.canvas = document.querySelector('canvas#preview'); + + snapshotButton.onclick = function () + { + canvasrender.width = video.videoWidth; + canvasrender.height = video.videoHeight; + + // video size + canvasrender.getContext('2d').drawImage(video, 0, 0, canvasrender.width, canvasrender.height); + + // preview small + canvaspreview.getContext('2d').drawImage(video, 0, 0,canvaspreview.width, canvaspreview.height); + // set saved text + canvaspreview.getContext('2d').font = "30px Comic Sans MS"; + canvaspreview.getContext('2d').fillStyle = "white"; + canvaspreview.getContext('2d').textAlign = "center"; + canvaspreview.getContext('2d').fillText("Saved!", canvas.width/2, canvas.height/2); + + var data = canvasrender.toDataURL('image/png'); + YUI().use('io-base', function (Y) + { + // saving the file + var cfg = { + method : 'POST', + data: {'sesskey': flashvars.sessionid, 'file': data} + }; + var request = Y.io(flashvars.uploadPath, cfg); + + //on completed request + Y.on('io:complete', onComplete, Y); + }); + }; + + navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; + + var constraints = { + audio: false, + "video": { + "mandatory": { + "minWidth": "480", + "minHeight": "480", + "minFrameRate": "30", + "minAspectRatio": "1", + "maxWidth": "480", + "maxHeight": "480", + "maxFrameRate": "30", + "maxAspectRatio": "1" + }, + "optional": [] + } + }; + + function successCallback(stream){ + + window.stream = stream; // make stream available to browser console + if (window.URL){ + video.src = window.URL.createObjectURL(stream); + }else{ + video.src = stream; + } + } + + function onComplete(transactionid, response, arguments) + { + try{ + var json = JSON.parse(response.response); + + if(json.status == true) { + //reload profile picture + M.block_mfavatar.saved(); + } + M.block_mfavatar.log(json); + } catch (exc){ + console.log(exc); + } + } + + function errorCallback(error) { + console.log('navigator.getUserMedia error: ', error); + } + + navigator.getUserMedia(constraints, successCallback, errorCallback); + }, + webrtc_is_supported: function () { + return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); + }, + saved : function () { + this.log('Saved!!!'); + var profilePicture = Y.one('img.profilepic'); + if (profilePicture) + { + var src = profilePicture.getAttribute('src'); + profilePicture.setAttribute('src', ''); + setTimeout(function () + { + var now = new Date().getTime() / 1000; + profilePicture.setAttribute('src', src + '&c=' + now); + }, 500); + + } + }, + error : function (err){ + M.block_mfavatar.log('Error!'); + M.block_mfavatar.log(err); + } }; \ No newline at end of file diff --git a/blocks/mfavatar/renderer.php b/renderer.php similarity index 97% rename from blocks/mfavatar/renderer.php rename to renderer.php index 5ad42e8..88a4d89 100644 --- a/blocks/mfavatar/renderer.php +++ b/renderer.php @@ -1,57 +1,57 @@ -. - -/** - * html render class - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -class block_mfavatar_renderer extends plugin_renderer_base { - - /** - * add the snapshot tool - * - * @return string - * @throws coding_exception - */ - public function snapshot_tool() { - $html = ''; - - //add webrtc container - $html .= ''; - - return $html; - } - +. + +/** + * html render class + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +class block_mfavatar_renderer extends plugin_renderer_base { + + /** + * add the snapshot tool + * + * @return string + * @throws coding_exception + */ + public function snapshot_tool() { + $html = ''; + + //add webrtc container + $html .= ''; + + return $html; + } + } \ No newline at end of file diff --git a/blocks/mfavatar/settings.php b/settings.php similarity index 97% rename from blocks/mfavatar/settings.php rename to settings.php index ff32036..52f9064 100644 --- a/blocks/mfavatar/settings.php +++ b/settings.php @@ -1,31 +1,31 @@ -. - -/** - * settings - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -defined('MOODLE_INTERNAL') || die(); - -if ($ADMIN->fulltree) { - $settings->add(new admin_setting_heading('block_mfavatar_settings', '', get_string('pluginname_desc', 'block_mfavatar'))); - $settings->add(new admin_setting_configcheckbox('block_mfavatar/webrtc_enabled', get_string('webrtc_enabled', 'block_mfavatar'), '', 0)); +. + +/** + * settings + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +defined('MOODLE_INTERNAL') || die(); + +if ($ADMIN->fulltree) { + $settings->add(new admin_setting_heading('block_mfavatar_settings', '', get_string('pluginname_desc', 'block_mfavatar'))); + $settings->add(new admin_setting_configcheckbox('block_mfavatar/webrtc_enabled', get_string('webrtc_enabled', 'block_mfavatar'), '', 0)); } \ No newline at end of file diff --git a/blocks/mfavatar/source/MyButton.as b/source/MyButton.as similarity index 93% rename from blocks/mfavatar/source/MyButton.as rename to source/MyButton.as index 4ccff33..81ecd86 100644 --- a/blocks/mfavatar/source/MyButton.as +++ b/source/MyButton.as @@ -1,23 +1,23 @@ -package { - - import flash.display.SimpleButton; - - public class MyButton extends SimpleButton { - - public function MyButton() { - // constructor code - } - - public function enable(b:Boolean = true):void { - if (b) { - this.enabled = true; - this.alpha = 1; - } else { - this.enabled = false; - this.alpha = 0.33; - } - } - - } - -} +package { + + import flash.display.SimpleButton; + + public class MyButton extends SimpleButton { + + public function MyButton() { + // constructor code + } + + public function enable(b:Boolean = true):void { + if (b) { + this.enabled = true; + this.alpha = 1; + } else { + this.enabled = false; + this.alpha = 0.33; + } + } + + } + +} diff --git a/blocks/mfavatar/source/com/adobe/images/BitString.as b/source/com/adobe/images/BitString.as similarity index 97% rename from blocks/mfavatar/source/com/adobe/images/BitString.as rename to source/com/adobe/images/BitString.as index b5c2b84..809fee9 100644 --- a/blocks/mfavatar/source/com/adobe/images/BitString.as +++ b/source/com/adobe/images/BitString.as @@ -1,39 +1,39 @@ -/* - Copyright (c) 2008, Adobe Systems Incorporated - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Adobe Systems Incorporated nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -package com.adobe.images -{ - public class BitString - { - public var len:int = 0; - public var val:int = 0; - } +/* + Copyright (c) 2008, Adobe Systems Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Adobe Systems Incorporated nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package com.adobe.images +{ + public class BitString + { + public var len:int = 0; + public var val:int = 0; + } } \ No newline at end of file diff --git a/blocks/mfavatar/source/com/adobe/images/JPGEncoder.as b/source/com/adobe/images/JPGEncoder.as similarity index 96% rename from blocks/mfavatar/source/com/adobe/images/JPGEncoder.as rename to source/com/adobe/images/JPGEncoder.as index 100d7e9..4040de3 100644 --- a/blocks/mfavatar/source/com/adobe/images/JPGEncoder.as +++ b/source/com/adobe/images/JPGEncoder.as @@ -1,648 +1,648 @@ -/* - Copyright (c) 2008, Adobe Systems Incorporated - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Adobe Systems Incorporated nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -package com.adobe.images -{ - import flash.geom.*; - import flash.display.*; - import flash.utils.*; - - /** - * Class that converts BitmapData into a valid JPEG - */ - public class JPGEncoder - { - - // Static table initialization - - private var ZigZag:Array = [ - 0, 1, 5, 6,14,15,27,28, - 2, 4, 7,13,16,26,29,42, - 3, 8,12,17,25,30,41,43, - 9,11,18,24,31,40,44,53, - 10,19,23,32,39,45,52,54, - 20,22,33,38,46,51,55,60, - 21,34,37,47,50,56,59,61, - 35,36,48,49,57,58,62,63 - ]; - - private var YTable:Array = new Array(64); - private var UVTable:Array = new Array(64); - private var fdtbl_Y:Array = new Array(64); - private var fdtbl_UV:Array = new Array(64); - - private function initQuantTables(sf:int):void - { - var i:int; - var t:Number; - var YQT:Array = [ - 16, 11, 10, 16, 24, 40, 51, 61, - 12, 12, 14, 19, 26, 58, 60, 55, - 14, 13, 16, 24, 40, 57, 69, 56, - 14, 17, 22, 29, 51, 87, 80, 62, - 18, 22, 37, 56, 68,109,103, 77, - 24, 35, 55, 64, 81,104,113, 92, - 49, 64, 78, 87,103,121,120,101, - 72, 92, 95, 98,112,100,103, 99 - ]; - for (i = 0; i < 64; i++) { - t = Math.floor((YQT[i]*sf+50)/100); - if (t < 1) { - t = 1; - } else if (t > 255) { - t = 255; - } - YTable[ZigZag[i]] = t; - } - var UVQT:Array = [ - 17, 18, 24, 47, 99, 99, 99, 99, - 18, 21, 26, 66, 99, 99, 99, 99, - 24, 26, 56, 99, 99, 99, 99, 99, - 47, 66, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, 99, 99, 99, - 99, 99, 99, 99, 99, 99, 99, 99 - ]; - for (i = 0; i < 64; i++) { - t = Math.floor((UVQT[i]*sf+50)/100); - if (t < 1) { - t = 1; - } else if (t > 255) { - t = 255; - } - UVTable[ZigZag[i]] = t; - } - var aasf:Array = [ - 1.0, 1.387039845, 1.306562965, 1.175875602, - 1.0, 0.785694958, 0.541196100, 0.275899379 - ]; - i = 0; - for (var row:int = 0; row < 8; row++) - { - for (var col:int = 0; col < 8; col++) - { - fdtbl_Y[i] = (1.0 / (YTable [ZigZag[i]] * aasf[row] * aasf[col] * 8.0)); - fdtbl_UV[i] = (1.0 / (UVTable[ZigZag[i]] * aasf[row] * aasf[col] * 8.0)); - i++; - } - } - } - - private var YDC_HT:Array; - private var UVDC_HT:Array; - private var YAC_HT:Array; - private var UVAC_HT:Array; - - private function computeHuffmanTbl(nrcodes:Array, std_table:Array):Array - { - var codevalue:int = 0; - var pos_in_table:int = 0; - var HT:Array = new Array(); - for (var k:int=1; k<=16; k++) { - for (var j:int=1; j<=nrcodes[k]; j++) { - HT[std_table[pos_in_table]] = new BitString(); - HT[std_table[pos_in_table]].val = codevalue; - HT[std_table[pos_in_table]].len = k; - pos_in_table++; - codevalue++; - } - codevalue*=2; - } - return HT; - } - - private var std_dc_luminance_nrcodes:Array = [0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0]; - private var std_dc_luminance_values:Array = [0,1,2,3,4,5,6,7,8,9,10,11]; - private var std_ac_luminance_nrcodes:Array = [0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d]; - private var std_ac_luminance_values:Array = [ - 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12, - 0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07, - 0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, - 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0, - 0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16, - 0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, - 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39, - 0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49, - 0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, - 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69, - 0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79, - 0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, - 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98, - 0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7, - 0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, - 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5, - 0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4, - 0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, - 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea, - 0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8, - 0xf9,0xfa - ]; - - private var std_dc_chrominance_nrcodes:Array = [0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0]; - private var std_dc_chrominance_values:Array = [0,1,2,3,4,5,6,7,8,9,10,11]; - private var std_ac_chrominance_nrcodes:Array = [0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77]; - private var std_ac_chrominance_values:Array = [ - 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21, - 0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71, - 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, - 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0, - 0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34, - 0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, - 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38, - 0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48, - 0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, - 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68, - 0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78, - 0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, - 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96, - 0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5, - 0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, - 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3, - 0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2, - 0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, - 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9, - 0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8, - 0xf9,0xfa - ]; - - private function initHuffmanTbl():void - { - YDC_HT = computeHuffmanTbl(std_dc_luminance_nrcodes,std_dc_luminance_values); - UVDC_HT = computeHuffmanTbl(std_dc_chrominance_nrcodes,std_dc_chrominance_values); - YAC_HT = computeHuffmanTbl(std_ac_luminance_nrcodes,std_ac_luminance_values); - UVAC_HT = computeHuffmanTbl(std_ac_chrominance_nrcodes,std_ac_chrominance_values); - } - - private var bitcode:Array = new Array(65535); - private var category:Array = new Array(65535); - - private function initCategoryNumber():void - { - var nrlower:int = 1; - var nrupper:int = 2; - var nr:int; - for (var cat:int=1; cat<=15; cat++) { - //Positive numbers - for (nr=nrlower; nr= 0 ) { - if (value & uint(1 << posval) ) { - bytenew |= uint(1 << bytepos); - } - posval--; - bytepos--; - if (bytepos < 0) { - if (bytenew == 0xFF) { - writeByte(0xFF); - writeByte(0); - } - else { - writeByte(bytenew); - } - bytepos=7; - bytenew=0; - } - } - } - - private function writeByte(value:int):void - { - byteout.writeByte(value); - } - - private function writeWord(value:int):void - { - writeByte((value>>8)&0xFF); - writeByte((value )&0xFF); - } - - // DCT & quantization core - - private function fDCTQuant(data:Array, fdtbl:Array):Array - { - var tmp0:Number, tmp1:Number, tmp2:Number, tmp3:Number, tmp4:Number, tmp5:Number, tmp6:Number, tmp7:Number; - var tmp10:Number, tmp11:Number, tmp12:Number, tmp13:Number; - var z1:Number, z2:Number, z3:Number, z4:Number, z5:Number, z11:Number, z13:Number; - var i:int; - /* Pass 1: process rows. */ - var dataOff:int=0; - for (i=0; i<8; i++) { - tmp0 = data[dataOff+0] + data[dataOff+7]; - tmp7 = data[dataOff+0] - data[dataOff+7]; - tmp1 = data[dataOff+1] + data[dataOff+6]; - tmp6 = data[dataOff+1] - data[dataOff+6]; - tmp2 = data[dataOff+2] + data[dataOff+5]; - tmp5 = data[dataOff+2] - data[dataOff+5]; - tmp3 = data[dataOff+3] + data[dataOff+4]; - tmp4 = data[dataOff+3] - data[dataOff+4]; - - /* Even part */ - tmp10 = tmp0 + tmp3; /* phase 2 */ - tmp13 = tmp0 - tmp3; - tmp11 = tmp1 + tmp2; - tmp12 = tmp1 - tmp2; - - data[dataOff+0] = tmp10 + tmp11; /* phase 3 */ - data[dataOff+4] = tmp10 - tmp11; - - z1 = (tmp12 + tmp13) * 0.707106781; /* c4 */ - data[dataOff+2] = tmp13 + z1; /* phase 5 */ - data[dataOff+6] = tmp13 - z1; - - /* Odd part */ - tmp10 = tmp4 + tmp5; /* phase 2 */ - tmp11 = tmp5 + tmp6; - tmp12 = tmp6 + tmp7; - - /* The rotator is modified from fig 4-8 to avoid extra negations. */ - z5 = (tmp10 - tmp12) * 0.382683433; /* c6 */ - z2 = 0.541196100 * tmp10 + z5; /* c2-c6 */ - z4 = 1.306562965 * tmp12 + z5; /* c2+c6 */ - z3 = tmp11 * 0.707106781; /* c4 */ - - z11 = tmp7 + z3; /* phase 5 */ - z13 = tmp7 - z3; - - data[dataOff+5] = z13 + z2; /* phase 6 */ - data[dataOff+3] = z13 - z2; - data[dataOff+1] = z11 + z4; - data[dataOff+7] = z11 - z4; - - dataOff += 8; /* advance pointer to next row */ - } - - /* Pass 2: process columns. */ - dataOff = 0; - for (i=0; i<8; i++) { - tmp0 = data[dataOff+ 0] + data[dataOff+56]; - tmp7 = data[dataOff+ 0] - data[dataOff+56]; - tmp1 = data[dataOff+ 8] + data[dataOff+48]; - tmp6 = data[dataOff+ 8] - data[dataOff+48]; - tmp2 = data[dataOff+16] + data[dataOff+40]; - tmp5 = data[dataOff+16] - data[dataOff+40]; - tmp3 = data[dataOff+24] + data[dataOff+32]; - tmp4 = data[dataOff+24] - data[dataOff+32]; - - /* Even part */ - tmp10 = tmp0 + tmp3; /* phase 2 */ - tmp13 = tmp0 - tmp3; - tmp11 = tmp1 + tmp2; - tmp12 = tmp1 - tmp2; - - data[dataOff+ 0] = tmp10 + tmp11; /* phase 3 */ - data[dataOff+32] = tmp10 - tmp11; - - z1 = (tmp12 + tmp13) * 0.707106781; /* c4 */ - data[dataOff+16] = tmp13 + z1; /* phase 5 */ - data[dataOff+48] = tmp13 - z1; - - /* Odd part */ - tmp10 = tmp4 + tmp5; /* phase 2 */ - tmp11 = tmp5 + tmp6; - tmp12 = tmp6 + tmp7; - - /* The rotator is modified from fig 4-8 to avoid extra negations. */ - z5 = (tmp10 - tmp12) * 0.382683433; /* c6 */ - z2 = 0.541196100 * tmp10 + z5; /* c2-c6 */ - z4 = 1.306562965 * tmp12 + z5; /* c2+c6 */ - z3 = tmp11 * 0.707106781; /* c4 */ - - z11 = tmp7 + z3; /* phase 5 */ - z13 = tmp7 - z3; - - data[dataOff+40] = z13 + z2; /* phase 6 */ - data[dataOff+24] = z13 - z2; - data[dataOff+ 8] = z11 + z4; - data[dataOff+56] = z11 - z4; - - dataOff++; /* advance pointer to next column */ - } - - // Quantize/descale the coefficients - for (i=0; i<64; i++) { - // Apply the quantization and scaling factor & Round to nearest integer - data[i] = Math.round((data[i]*fdtbl[i])); - } - return data; - } - - // Chunk writing - - private function writeAPP0():void - { - writeWord(0xFFE0); // marker - writeWord(16); // length - writeByte(0x4A); // J - writeByte(0x46); // F - writeByte(0x49); // I - writeByte(0x46); // F - writeByte(0); // = "JFIF",'\0' - writeByte(1); // versionhi - writeByte(1); // versionlo - writeByte(0); // xyunits - writeWord(1); // xdensity - writeWord(1); // ydensity - writeByte(0); // thumbnwidth - writeByte(0); // thumbnheight - } - - private function writeSOF0(width:int, height:int):void - { - writeWord(0xFFC0); // marker - writeWord(17); // length, truecolor YUV JPG - writeByte(8); // precision - writeWord(height); - writeWord(width); - writeByte(3); // nrofcomponents - writeByte(1); // IdY - writeByte(0x11); // HVY - writeByte(0); // QTY - writeByte(2); // IdU - writeByte(0x11); // HVU - writeByte(1); // QTU - writeByte(3); // IdV - writeByte(0x11); // HVV - writeByte(1); // QTV - } - - private function writeDQT():void - { - writeWord(0xFFDB); // marker - writeWord(132); // length - writeByte(0); - var i:int; - for (i=0; i<64; i++) { - writeByte(YTable[i]); - } - writeByte(1); - for (i=0; i<64; i++) { - writeByte(UVTable[i]); - } - } - - private function writeDHT():void - { - writeWord(0xFFC4); // marker - writeWord(0x01A2); // length - var i:int; - - writeByte(0); // HTYDCinfo - for (i=0; i<16; i++) { - writeByte(std_dc_luminance_nrcodes[i+1]); - } - for (i=0; i<=11; i++) { - writeByte(std_dc_luminance_values[i]); - } - - writeByte(0x10); // HTYACinfo - for (i=0; i<16; i++) { - writeByte(std_ac_luminance_nrcodes[i+1]); - } - for (i=0; i<=161; i++) { - writeByte(std_ac_luminance_values[i]); - } - - writeByte(1); // HTUDCinfo - for (i=0; i<16; i++) { - writeByte(std_dc_chrominance_nrcodes[i+1]); - } - for (i=0; i<=11; i++) { - writeByte(std_dc_chrominance_values[i]); - } - - writeByte(0x11); // HTUACinfo - for (i=0; i<16; i++) { - writeByte(std_ac_chrominance_nrcodes[i+1]); - } - for (i=0; i<=161; i++) { - writeByte(std_ac_chrominance_values[i]); - } - } - - private function writeSOS():void - { - writeWord(0xFFDA); // marker - writeWord(12); // length - writeByte(3); // nrofcomponents - writeByte(1); // IdY - writeByte(0); // HTY - writeByte(2); // IdU - writeByte(0x11); // HTU - writeByte(3); // IdV - writeByte(0x11); // HTV - writeByte(0); // Ss - writeByte(0x3f); // Se - writeByte(0); // Bf - } - - // Core processing - private var DU:Array = new Array(64); - - private function processDU(CDU:Array, fdtbl:Array, DC:Number, HTDC:Array, HTAC:Array):Number - { - var EOB:BitString = HTAC[0x00]; - var M16zeroes:BitString = HTAC[0xF0]; - var i:int; - - var DU_DCT:Array = fDCTQuant(CDU, fdtbl); - //ZigZag reorder - for (i=0;i<64;i++) { - DU[ZigZag[i]]=DU_DCT[i]; - } - var Diff:int = DU[0] - DC; DC = DU[0]; - //Encode DC - if (Diff==0) { - writeBits(HTDC[0]); // Diff might be 0 - } else { - writeBits(HTDC[category[32767+Diff]]); - writeBits(bitcode[32767+Diff]); - } - //Encode ACs - var end0pos:int = 63; - for (; (end0pos>0)&&(DU[end0pos]==0); end0pos--) { - }; - //end0pos = first element in reverse order !=0 - if ( end0pos == 0) { - writeBits(EOB); - return DC; - } - i = 1; - while ( i <= end0pos ) { - var startpos:int = i; - for (; (DU[i]==0) && (i<=end0pos); i++) { - } - var nrzeroes:int = i-startpos; - if ( nrzeroes >= 16 ) { - for (var nrmarker:int=1; nrmarker <= nrzeroes/16; nrmarker++) { - writeBits(M16zeroes); - } - nrzeroes = int(nrzeroes&0xF); - } - writeBits(HTAC[nrzeroes*16+category[32767+DU[i]]]); - writeBits(bitcode[32767+DU[i]]); - i++; - } - if ( end0pos != 63 ) { - writeBits(EOB); - } - return DC; - } - - private var YDU:Array = new Array(64); - private var UDU:Array = new Array(64); - private var VDU:Array = new Array(64); - - private function RGB2YUV(img:BitmapData, xpos:int, ypos:int):void - { - var pos:int=0; - for (var y:int=0; y<8; y++) { - for (var x:int=0; x<8; x++) { - var P:uint = img.getPixel32(xpos+x,ypos+y); - var R:Number = Number((P>>16)&0xFF); - var G:Number = Number((P>> 8)&0xFF); - var B:Number = Number((P )&0xFF); - YDU[pos]=((( 0.29900)*R+( 0.58700)*G+( 0.11400)*B))-128; - UDU[pos]=(((-0.16874)*R+(-0.33126)*G+( 0.50000)*B)); - VDU[pos]=((( 0.50000)*R+(-0.41869)*G+(-0.08131)*B)); - pos++; - } - } - } - - /** - * Constructor for JPEGEncoder class - * - * @param quality The quality level between 1 and 100 that detrmines the - * level of compression used in the generated JPEG - * @langversion ActionScript 3.0 - * @playerversion Flash 9.0 - * @tiptext - */ - public function JPGEncoder(quality:Number = 50) - { - if (quality <= 0) { - quality = 1; - } - if (quality > 100) { - quality = 100; - } - var sf:int = 0; - if (quality < 50) { - sf = int(5000 / quality); - } else { - sf = int(200 - quality*2); - } - // Create tables - initHuffmanTbl(); - initCategoryNumber(); - initQuantTables(sf); - } - - /** - * Created a JPEG image from the specified BitmapData - * - * @param image The BitmapData that will be converted into the JPEG format. - * @return a ByteArray representing the JPEG encoded image data. - * @langversion ActionScript 3.0 - * @playerversion Flash 9.0 - * @tiptext - */ - public function encode(image:BitmapData):ByteArray - { - // Initialize bit writer - byteout = new ByteArray(); - bytenew=0; - bytepos=7; - - // Add JPEG headers - writeWord(0xFFD8); // SOI - writeAPP0(); - writeDQT(); - writeSOF0(image.width,image.height); - writeDHT(); - writeSOS(); - - - // Encode 8x8 macroblocks - var DCY:Number=0; - var DCU:Number=0; - var DCV:Number=0; - bytenew=0; - bytepos=7; - for (var ypos:int=0; ypos= 0 ) { - var fillbits:BitString = new BitString(); - fillbits.len = bytepos+1; - fillbits.val = (1<<(bytepos+1))-1; - writeBits(fillbits); - } - - writeWord(0xFFD9); //EOI - return byteout; - } - } -} +/* + Copyright (c) 2008, Adobe Systems Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Adobe Systems Incorporated nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package com.adobe.images +{ + import flash.geom.*; + import flash.display.*; + import flash.utils.*; + + /** + * Class that converts BitmapData into a valid JPEG + */ + public class JPGEncoder + { + + // Static table initialization + + private var ZigZag:Array = [ + 0, 1, 5, 6,14,15,27,28, + 2, 4, 7,13,16,26,29,42, + 3, 8,12,17,25,30,41,43, + 9,11,18,24,31,40,44,53, + 10,19,23,32,39,45,52,54, + 20,22,33,38,46,51,55,60, + 21,34,37,47,50,56,59,61, + 35,36,48,49,57,58,62,63 + ]; + + private var YTable:Array = new Array(64); + private var UVTable:Array = new Array(64); + private var fdtbl_Y:Array = new Array(64); + private var fdtbl_UV:Array = new Array(64); + + private function initQuantTables(sf:int):void + { + var i:int; + var t:Number; + var YQT:Array = [ + 16, 11, 10, 16, 24, 40, 51, 61, + 12, 12, 14, 19, 26, 58, 60, 55, + 14, 13, 16, 24, 40, 57, 69, 56, + 14, 17, 22, 29, 51, 87, 80, 62, + 18, 22, 37, 56, 68,109,103, 77, + 24, 35, 55, 64, 81,104,113, 92, + 49, 64, 78, 87,103,121,120,101, + 72, 92, 95, 98,112,100,103, 99 + ]; + for (i = 0; i < 64; i++) { + t = Math.floor((YQT[i]*sf+50)/100); + if (t < 1) { + t = 1; + } else if (t > 255) { + t = 255; + } + YTable[ZigZag[i]] = t; + } + var UVQT:Array = [ + 17, 18, 24, 47, 99, 99, 99, 99, + 18, 21, 26, 66, 99, 99, 99, 99, + 24, 26, 56, 99, 99, 99, 99, 99, + 47, 66, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99 + ]; + for (i = 0; i < 64; i++) { + t = Math.floor((UVQT[i]*sf+50)/100); + if (t < 1) { + t = 1; + } else if (t > 255) { + t = 255; + } + UVTable[ZigZag[i]] = t; + } + var aasf:Array = [ + 1.0, 1.387039845, 1.306562965, 1.175875602, + 1.0, 0.785694958, 0.541196100, 0.275899379 + ]; + i = 0; + for (var row:int = 0; row < 8; row++) + { + for (var col:int = 0; col < 8; col++) + { + fdtbl_Y[i] = (1.0 / (YTable [ZigZag[i]] * aasf[row] * aasf[col] * 8.0)); + fdtbl_UV[i] = (1.0 / (UVTable[ZigZag[i]] * aasf[row] * aasf[col] * 8.0)); + i++; + } + } + } + + private var YDC_HT:Array; + private var UVDC_HT:Array; + private var YAC_HT:Array; + private var UVAC_HT:Array; + + private function computeHuffmanTbl(nrcodes:Array, std_table:Array):Array + { + var codevalue:int = 0; + var pos_in_table:int = 0; + var HT:Array = new Array(); + for (var k:int=1; k<=16; k++) { + for (var j:int=1; j<=nrcodes[k]; j++) { + HT[std_table[pos_in_table]] = new BitString(); + HT[std_table[pos_in_table]].val = codevalue; + HT[std_table[pos_in_table]].len = k; + pos_in_table++; + codevalue++; + } + codevalue*=2; + } + return HT; + } + + private var std_dc_luminance_nrcodes:Array = [0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0]; + private var std_dc_luminance_values:Array = [0,1,2,3,4,5,6,7,8,9,10,11]; + private var std_ac_luminance_nrcodes:Array = [0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d]; + private var std_ac_luminance_values:Array = [ + 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12, + 0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07, + 0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, + 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0, + 0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16, + 0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, + 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39, + 0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49, + 0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, + 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69, + 0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79, + 0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, + 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98, + 0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7, + 0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, + 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5, + 0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4, + 0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, + 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea, + 0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8, + 0xf9,0xfa + ]; + + private var std_dc_chrominance_nrcodes:Array = [0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0]; + private var std_dc_chrominance_values:Array = [0,1,2,3,4,5,6,7,8,9,10,11]; + private var std_ac_chrominance_nrcodes:Array = [0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77]; + private var std_ac_chrominance_values:Array = [ + 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21, + 0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71, + 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, + 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0, + 0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34, + 0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, + 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38, + 0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48, + 0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, + 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68, + 0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78, + 0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, + 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96, + 0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5, + 0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, + 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3, + 0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2, + 0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, + 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9, + 0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8, + 0xf9,0xfa + ]; + + private function initHuffmanTbl():void + { + YDC_HT = computeHuffmanTbl(std_dc_luminance_nrcodes,std_dc_luminance_values); + UVDC_HT = computeHuffmanTbl(std_dc_chrominance_nrcodes,std_dc_chrominance_values); + YAC_HT = computeHuffmanTbl(std_ac_luminance_nrcodes,std_ac_luminance_values); + UVAC_HT = computeHuffmanTbl(std_ac_chrominance_nrcodes,std_ac_chrominance_values); + } + + private var bitcode:Array = new Array(65535); + private var category:Array = new Array(65535); + + private function initCategoryNumber():void + { + var nrlower:int = 1; + var nrupper:int = 2; + var nr:int; + for (var cat:int=1; cat<=15; cat++) { + //Positive numbers + for (nr=nrlower; nr= 0 ) { + if (value & uint(1 << posval) ) { + bytenew |= uint(1 << bytepos); + } + posval--; + bytepos--; + if (bytepos < 0) { + if (bytenew == 0xFF) { + writeByte(0xFF); + writeByte(0); + } + else { + writeByte(bytenew); + } + bytepos=7; + bytenew=0; + } + } + } + + private function writeByte(value:int):void + { + byteout.writeByte(value); + } + + private function writeWord(value:int):void + { + writeByte((value>>8)&0xFF); + writeByte((value )&0xFF); + } + + // DCT & quantization core + + private function fDCTQuant(data:Array, fdtbl:Array):Array + { + var tmp0:Number, tmp1:Number, tmp2:Number, tmp3:Number, tmp4:Number, tmp5:Number, tmp6:Number, tmp7:Number; + var tmp10:Number, tmp11:Number, tmp12:Number, tmp13:Number; + var z1:Number, z2:Number, z3:Number, z4:Number, z5:Number, z11:Number, z13:Number; + var i:int; + /* Pass 1: process rows. */ + var dataOff:int=0; + for (i=0; i<8; i++) { + tmp0 = data[dataOff+0] + data[dataOff+7]; + tmp7 = data[dataOff+0] - data[dataOff+7]; + tmp1 = data[dataOff+1] + data[dataOff+6]; + tmp6 = data[dataOff+1] - data[dataOff+6]; + tmp2 = data[dataOff+2] + data[dataOff+5]; + tmp5 = data[dataOff+2] - data[dataOff+5]; + tmp3 = data[dataOff+3] + data[dataOff+4]; + tmp4 = data[dataOff+3] - data[dataOff+4]; + + /* Even part */ + tmp10 = tmp0 + tmp3; /* phase 2 */ + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + data[dataOff+0] = tmp10 + tmp11; /* phase 3 */ + data[dataOff+4] = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * 0.707106781; /* c4 */ + data[dataOff+2] = tmp13 + z1; /* phase 5 */ + data[dataOff+6] = tmp13 - z1; + + /* Odd part */ + tmp10 = tmp4 + tmp5; /* phase 2 */ + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + /* The rotator is modified from fig 4-8 to avoid extra negations. */ + z5 = (tmp10 - tmp12) * 0.382683433; /* c6 */ + z2 = 0.541196100 * tmp10 + z5; /* c2-c6 */ + z4 = 1.306562965 * tmp12 + z5; /* c2+c6 */ + z3 = tmp11 * 0.707106781; /* c4 */ + + z11 = tmp7 + z3; /* phase 5 */ + z13 = tmp7 - z3; + + data[dataOff+5] = z13 + z2; /* phase 6 */ + data[dataOff+3] = z13 - z2; + data[dataOff+1] = z11 + z4; + data[dataOff+7] = z11 - z4; + + dataOff += 8; /* advance pointer to next row */ + } + + /* Pass 2: process columns. */ + dataOff = 0; + for (i=0; i<8; i++) { + tmp0 = data[dataOff+ 0] + data[dataOff+56]; + tmp7 = data[dataOff+ 0] - data[dataOff+56]; + tmp1 = data[dataOff+ 8] + data[dataOff+48]; + tmp6 = data[dataOff+ 8] - data[dataOff+48]; + tmp2 = data[dataOff+16] + data[dataOff+40]; + tmp5 = data[dataOff+16] - data[dataOff+40]; + tmp3 = data[dataOff+24] + data[dataOff+32]; + tmp4 = data[dataOff+24] - data[dataOff+32]; + + /* Even part */ + tmp10 = tmp0 + tmp3; /* phase 2 */ + tmp13 = tmp0 - tmp3; + tmp11 = tmp1 + tmp2; + tmp12 = tmp1 - tmp2; + + data[dataOff+ 0] = tmp10 + tmp11; /* phase 3 */ + data[dataOff+32] = tmp10 - tmp11; + + z1 = (tmp12 + tmp13) * 0.707106781; /* c4 */ + data[dataOff+16] = tmp13 + z1; /* phase 5 */ + data[dataOff+48] = tmp13 - z1; + + /* Odd part */ + tmp10 = tmp4 + tmp5; /* phase 2 */ + tmp11 = tmp5 + tmp6; + tmp12 = tmp6 + tmp7; + + /* The rotator is modified from fig 4-8 to avoid extra negations. */ + z5 = (tmp10 - tmp12) * 0.382683433; /* c6 */ + z2 = 0.541196100 * tmp10 + z5; /* c2-c6 */ + z4 = 1.306562965 * tmp12 + z5; /* c2+c6 */ + z3 = tmp11 * 0.707106781; /* c4 */ + + z11 = tmp7 + z3; /* phase 5 */ + z13 = tmp7 - z3; + + data[dataOff+40] = z13 + z2; /* phase 6 */ + data[dataOff+24] = z13 - z2; + data[dataOff+ 8] = z11 + z4; + data[dataOff+56] = z11 - z4; + + dataOff++; /* advance pointer to next column */ + } + + // Quantize/descale the coefficients + for (i=0; i<64; i++) { + // Apply the quantization and scaling factor & Round to nearest integer + data[i] = Math.round((data[i]*fdtbl[i])); + } + return data; + } + + // Chunk writing + + private function writeAPP0():void + { + writeWord(0xFFE0); // marker + writeWord(16); // length + writeByte(0x4A); // J + writeByte(0x46); // F + writeByte(0x49); // I + writeByte(0x46); // F + writeByte(0); // = "JFIF",'\0' + writeByte(1); // versionhi + writeByte(1); // versionlo + writeByte(0); // xyunits + writeWord(1); // xdensity + writeWord(1); // ydensity + writeByte(0); // thumbnwidth + writeByte(0); // thumbnheight + } + + private function writeSOF0(width:int, height:int):void + { + writeWord(0xFFC0); // marker + writeWord(17); // length, truecolor YUV JPG + writeByte(8); // precision + writeWord(height); + writeWord(width); + writeByte(3); // nrofcomponents + writeByte(1); // IdY + writeByte(0x11); // HVY + writeByte(0); // QTY + writeByte(2); // IdU + writeByte(0x11); // HVU + writeByte(1); // QTU + writeByte(3); // IdV + writeByte(0x11); // HVV + writeByte(1); // QTV + } + + private function writeDQT():void + { + writeWord(0xFFDB); // marker + writeWord(132); // length + writeByte(0); + var i:int; + for (i=0; i<64; i++) { + writeByte(YTable[i]); + } + writeByte(1); + for (i=0; i<64; i++) { + writeByte(UVTable[i]); + } + } + + private function writeDHT():void + { + writeWord(0xFFC4); // marker + writeWord(0x01A2); // length + var i:int; + + writeByte(0); // HTYDCinfo + for (i=0; i<16; i++) { + writeByte(std_dc_luminance_nrcodes[i+1]); + } + for (i=0; i<=11; i++) { + writeByte(std_dc_luminance_values[i]); + } + + writeByte(0x10); // HTYACinfo + for (i=0; i<16; i++) { + writeByte(std_ac_luminance_nrcodes[i+1]); + } + for (i=0; i<=161; i++) { + writeByte(std_ac_luminance_values[i]); + } + + writeByte(1); // HTUDCinfo + for (i=0; i<16; i++) { + writeByte(std_dc_chrominance_nrcodes[i+1]); + } + for (i=0; i<=11; i++) { + writeByte(std_dc_chrominance_values[i]); + } + + writeByte(0x11); // HTUACinfo + for (i=0; i<16; i++) { + writeByte(std_ac_chrominance_nrcodes[i+1]); + } + for (i=0; i<=161; i++) { + writeByte(std_ac_chrominance_values[i]); + } + } + + private function writeSOS():void + { + writeWord(0xFFDA); // marker + writeWord(12); // length + writeByte(3); // nrofcomponents + writeByte(1); // IdY + writeByte(0); // HTY + writeByte(2); // IdU + writeByte(0x11); // HTU + writeByte(3); // IdV + writeByte(0x11); // HTV + writeByte(0); // Ss + writeByte(0x3f); // Se + writeByte(0); // Bf + } + + // Core processing + private var DU:Array = new Array(64); + + private function processDU(CDU:Array, fdtbl:Array, DC:Number, HTDC:Array, HTAC:Array):Number + { + var EOB:BitString = HTAC[0x00]; + var M16zeroes:BitString = HTAC[0xF0]; + var i:int; + + var DU_DCT:Array = fDCTQuant(CDU, fdtbl); + //ZigZag reorder + for (i=0;i<64;i++) { + DU[ZigZag[i]]=DU_DCT[i]; + } + var Diff:int = DU[0] - DC; DC = DU[0]; + //Encode DC + if (Diff==0) { + writeBits(HTDC[0]); // Diff might be 0 + } else { + writeBits(HTDC[category[32767+Diff]]); + writeBits(bitcode[32767+Diff]); + } + //Encode ACs + var end0pos:int = 63; + for (; (end0pos>0)&&(DU[end0pos]==0); end0pos--) { + }; + //end0pos = first element in reverse order !=0 + if ( end0pos == 0) { + writeBits(EOB); + return DC; + } + i = 1; + while ( i <= end0pos ) { + var startpos:int = i; + for (; (DU[i]==0) && (i<=end0pos); i++) { + } + var nrzeroes:int = i-startpos; + if ( nrzeroes >= 16 ) { + for (var nrmarker:int=1; nrmarker <= nrzeroes/16; nrmarker++) { + writeBits(M16zeroes); + } + nrzeroes = int(nrzeroes&0xF); + } + writeBits(HTAC[nrzeroes*16+category[32767+DU[i]]]); + writeBits(bitcode[32767+DU[i]]); + i++; + } + if ( end0pos != 63 ) { + writeBits(EOB); + } + return DC; + } + + private var YDU:Array = new Array(64); + private var UDU:Array = new Array(64); + private var VDU:Array = new Array(64); + + private function RGB2YUV(img:BitmapData, xpos:int, ypos:int):void + { + var pos:int=0; + for (var y:int=0; y<8; y++) { + for (var x:int=0; x<8; x++) { + var P:uint = img.getPixel32(xpos+x,ypos+y); + var R:Number = Number((P>>16)&0xFF); + var G:Number = Number((P>> 8)&0xFF); + var B:Number = Number((P )&0xFF); + YDU[pos]=((( 0.29900)*R+( 0.58700)*G+( 0.11400)*B))-128; + UDU[pos]=(((-0.16874)*R+(-0.33126)*G+( 0.50000)*B)); + VDU[pos]=((( 0.50000)*R+(-0.41869)*G+(-0.08131)*B)); + pos++; + } + } + } + + /** + * Constructor for JPEGEncoder class + * + * @param quality The quality level between 1 and 100 that detrmines the + * level of compression used in the generated JPEG + * @langversion ActionScript 3.0 + * @playerversion Flash 9.0 + * @tiptext + */ + public function JPGEncoder(quality:Number = 50) + { + if (quality <= 0) { + quality = 1; + } + if (quality > 100) { + quality = 100; + } + var sf:int = 0; + if (quality < 50) { + sf = int(5000 / quality); + } else { + sf = int(200 - quality*2); + } + // Create tables + initHuffmanTbl(); + initCategoryNumber(); + initQuantTables(sf); + } + + /** + * Created a JPEG image from the specified BitmapData + * + * @param image The BitmapData that will be converted into the JPEG format. + * @return a ByteArray representing the JPEG encoded image data. + * @langversion ActionScript 3.0 + * @playerversion Flash 9.0 + * @tiptext + */ + public function encode(image:BitmapData):ByteArray + { + // Initialize bit writer + byteout = new ByteArray(); + bytenew=0; + bytepos=7; + + // Add JPEG headers + writeWord(0xFFD8); // SOI + writeAPP0(); + writeDQT(); + writeSOF0(image.width,image.height); + writeDHT(); + writeSOS(); + + + // Encode 8x8 macroblocks + var DCY:Number=0; + var DCU:Number=0; + var DCV:Number=0; + bytenew=0; + bytepos=7; + for (var ypos:int=0; ypos= 0 ) { + var fillbits:BitString = new BitString(); + fillbits.len = bytepos+1; + fillbits.val = (1<<(bytepos+1))-1; + writeBits(fillbits); + } + + writeWord(0xFFD9); //EOI + return byteout; + } + } +} diff --git a/blocks/mfavatar/source/com/adobe/images/PNGEncoder.as b/source/com/adobe/images/PNGEncoder.as similarity index 97% rename from blocks/mfavatar/source/com/adobe/images/PNGEncoder.as rename to source/com/adobe/images/PNGEncoder.as index 83c95f6..6c20e43 100644 --- a/blocks/mfavatar/source/com/adobe/images/PNGEncoder.as +++ b/source/com/adobe/images/PNGEncoder.as @@ -1,141 +1,141 @@ -/* - Copyright (c) 2008, Adobe Systems Incorporated - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - * Neither the name of Adobe Systems Incorporated nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -package com.adobe.images -{ - import flash.geom.*; - import flash.display.Bitmap; - import flash.display.BitmapData; - import flash.utils.ByteArray; - - /** - * Class that converts BitmapData into a valid PNG - */ - public class PNGEncoder - { - /** - * Created a PNG image from the specified BitmapData - * - * @param image The BitmapData that will be converted into the PNG format. - * @return a ByteArray representing the PNG encoded image data. - * @langversion ActionScript 3.0 - * @playerversion Flash 9.0 - * @tiptext - */ - public static function encode(img:BitmapData):ByteArray { - // Create output byte array - var png:ByteArray = new ByteArray(); - // Write PNG signature - png.writeUnsignedInt(0x89504e47); - png.writeUnsignedInt(0x0D0A1A0A); - // Build IHDR chunk - var IHDR:ByteArray = new ByteArray(); - IHDR.writeInt(img.width); - IHDR.writeInt(img.height); - IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA - IHDR.writeByte(0); - writeChunk(png,0x49484452,IHDR); - // Build IDAT chunk - var IDAT:ByteArray= new ByteArray(); - for(var i:int=0;i < img.height;i++) { - // no filter - IDAT.writeByte(0); - var p:uint; - var j:int; - if ( !img.transparent ) { - for(j=0;j < img.width;j++) { - p = img.getPixel(j,i); - IDAT.writeUnsignedInt( - uint(((p&0xFFFFFF) << 8)|0xFF)); - } - } else { - for(j=0;j < img.width;j++) { - p = img.getPixel32(j,i); - IDAT.writeUnsignedInt( - uint(((p&0xFFFFFF) << 8)| - (p>>>24))); - } - } - } - IDAT.compress(); - writeChunk(png,0x49444154,IDAT); - // Build IEND chunk - writeChunk(png,0x49454E44,null); - // return PNG - return png; - } - - private static var crcTable:Array; - private static var crcTableComputed:Boolean = false; - - private static function writeChunk(png:ByteArray, - type:uint, data:ByteArray):void { - if (!crcTableComputed) { - crcTableComputed = true; - crcTable = []; - var c:uint; - for (var n:uint = 0; n < 256; n++) { - c = n; - for (var k:uint = 0; k < 8; k++) { - if (c & 1) { - c = uint(uint(0xedb88320) ^ - uint(c >>> 1)); - } else { - c = uint(c >>> 1); - } - } - crcTable[n] = c; - } - } - var len:uint = 0; - if (data != null) { - len = data.length; - } - png.writeUnsignedInt(len); - var p:uint = png.position; - png.writeUnsignedInt(type); - if ( data != null ) { - png.writeBytes(data); - } - var e:uint = png.position; - png.position = p; - c = 0xffffffff; - for (var i:int = 0; i < (e-p); i++) { - c = uint(crcTable[ - (c ^ png.readUnsignedByte()) & - uint(0xff)] ^ uint(c >>> 8)); - } - c = uint(c^uint(0xffffffff)); - png.position = e; - png.writeUnsignedInt(c); - } - } +/* + Copyright (c) 2008, Adobe Systems Incorporated + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of Adobe Systems Incorporated nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +package com.adobe.images +{ + import flash.geom.*; + import flash.display.Bitmap; + import flash.display.BitmapData; + import flash.utils.ByteArray; + + /** + * Class that converts BitmapData into a valid PNG + */ + public class PNGEncoder + { + /** + * Created a PNG image from the specified BitmapData + * + * @param image The BitmapData that will be converted into the PNG format. + * @return a ByteArray representing the PNG encoded image data. + * @langversion ActionScript 3.0 + * @playerversion Flash 9.0 + * @tiptext + */ + public static function encode(img:BitmapData):ByteArray { + // Create output byte array + var png:ByteArray = new ByteArray(); + // Write PNG signature + png.writeUnsignedInt(0x89504e47); + png.writeUnsignedInt(0x0D0A1A0A); + // Build IHDR chunk + var IHDR:ByteArray = new ByteArray(); + IHDR.writeInt(img.width); + IHDR.writeInt(img.height); + IHDR.writeUnsignedInt(0x08060000); // 32bit RGBA + IHDR.writeByte(0); + writeChunk(png,0x49484452,IHDR); + // Build IDAT chunk + var IDAT:ByteArray= new ByteArray(); + for(var i:int=0;i < img.height;i++) { + // no filter + IDAT.writeByte(0); + var p:uint; + var j:int; + if ( !img.transparent ) { + for(j=0;j < img.width;j++) { + p = img.getPixel(j,i); + IDAT.writeUnsignedInt( + uint(((p&0xFFFFFF) << 8)|0xFF)); + } + } else { + for(j=0;j < img.width;j++) { + p = img.getPixel32(j,i); + IDAT.writeUnsignedInt( + uint(((p&0xFFFFFF) << 8)| + (p>>>24))); + } + } + } + IDAT.compress(); + writeChunk(png,0x49444154,IDAT); + // Build IEND chunk + writeChunk(png,0x49454E44,null); + // return PNG + return png; + } + + private static var crcTable:Array; + private static var crcTableComputed:Boolean = false; + + private static function writeChunk(png:ByteArray, + type:uint, data:ByteArray):void { + if (!crcTableComputed) { + crcTableComputed = true; + crcTable = []; + var c:uint; + for (var n:uint = 0; n < 256; n++) { + c = n; + for (var k:uint = 0; k < 8; k++) { + if (c & 1) { + c = uint(uint(0xedb88320) ^ + uint(c >>> 1)); + } else { + c = uint(c >>> 1); + } + } + crcTable[n] = c; + } + } + var len:uint = 0; + if (data != null) { + len = data.length; + } + png.writeUnsignedInt(len); + var p:uint = png.position; + png.writeUnsignedInt(type); + if ( data != null ) { + png.writeBytes(data); + } + var e:uint = png.position; + png.position = p; + c = 0xffffffff; + for (var i:int = 0; i < (e-p); i++) { + c = uint(crcTable[ + (c ^ png.readUnsignedByte()) & + uint(0xff)] ^ uint(c >>> 8)); + } + c = uint(c^uint(0xffffffff)); + png.position = e; + png.writeUnsignedInt(c); + } + } } \ No newline at end of file diff --git a/blocks/mfavatar/source/org/phprpc/util/Base64.as b/source/org/phprpc/util/Base64.as similarity index 97% rename from blocks/mfavatar/source/org/phprpc/util/Base64.as rename to source/org/phprpc/util/Base64.as index 46a7c78..17510e4 100644 --- a/blocks/mfavatar/source/org/phprpc/util/Base64.as +++ b/source/org/phprpc/util/Base64.as @@ -1,145 +1,145 @@ -/**********************************************************\ - | | - | The implementation of PHPRPC Protocol 3.0 | - | | - | Base64.as | - | | - | Release 3.0.1 | - | Copyright by Team-PHPRPC | - | | - | WebSite: http://www.phprpc.org/ | - | http://www.phprpc.net/ | - | http://www.phprpc.com/ | - | http://sourceforge.net/projects/php-rpc/ | - | | - | Authors: Ma Bingyao | - | | - | This file may be distributed and/or modified under the | - | terms of the GNU General Public License (GPL) version | - | 2.0 as published by the Free Software Foundation and | - | appearing in the included file LICENSE. | - | | - \**********************************************************/ -/* Base64 library for ActionScript 3.0. - * - * Copyright: Ma Bingyao - * Version: 1.1 - * LastModified: Apr 12, 2010 - * This library is free. You can redistribute it and/or modify it under GPL. - */ -/* - * interfaces: - * import org.phprpc.util.Base64; - * import flash.utils.ByteArray; - * var data:ByteArray = new ByteArray(); - * data.writeUTFBytes("Hello PHPRPC"); - * var b64:String = Base64.encode(data); - * trace(b64); - * trace(Base64.decode(b64)); - */ -package org.phprpc.util{ -import flash.utils.ByteArray; -public class Base64 { - private static const encodeChars:Array = - ['A','B','C','D','E','F','G','H', - 'I','J','K','L','M','N','O','P', - 'Q','R','S','T','U','V','W','X', - 'Y','Z','a','b','c','d','e','f', - 'g','h','i','j','k','l','m','n', - 'o','p','q','r','s','t','u','v', - 'w','x','y','z','0','1','2','3', - '4','5','6','7','8','9','+','/']; - private static const decodeChars:Array = - [-1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, 62, -1, -1, -1, 63, - 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, -1, -1, -1, -1, -1, -1, - -1, 0, 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, -1, -1, -1, -1, -1, - -1, 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, -1, -1, -1, -1, -1]; - public static function encode(data:ByteArray):String { - var out:Array = []; - var i:int = 0; - var j:int = 0; - var r:int = data.length % 3; - var len:int = data.length - r; - var c:int; - while (i < len) { - c = data[i++] << 16 | data[i++] << 8 | data[i++]; - out[j++] = encodeChars[c >> 18] + encodeChars[c >> 12 & 0x3f] + encodeChars[c >> 6 & 0x3f] + encodeChars[c & 0x3f]; - } - if (r == 1) { - c = data[i++]; - out[j++] = encodeChars[c >> 2] + encodeChars[(c & 0x03) << 4] + "=="; - } - else if (r == 2) { - c = data[i++] << 8 | data[i++]; - out[j++] = encodeChars[c >> 10] + encodeChars[c >> 4 & 0x3f] + encodeChars[(c & 0x0f) << 2] + "="; - } - return out.join(''); - } - public static function decode(str:String):ByteArray { - var c1:int; - var c2:int; - var c3:int; - var c4:int; - var i:int; - var len:int; - var out:ByteArray; - len = str.length; - i = 0; - out = new ByteArray(); - while (i < len) { -// c1 - do { - c1 = decodeChars[str.charCodeAt(i++) & 0xff]; - } while (i < len && c1 == -1); - if (c1 == -1) { - break; - } -// c2 - do { - c2 = decodeChars[str.charCodeAt(i++) & 0xff]; - } while (i < len && c2 == -1); - if (c2 == -1) { - break; - } - out.writeByte((c1 << 2) | ((c2 & 0x30) >> 4)); -// c3 - do { - c3 = str.charCodeAt(i++) & 0xff; - if (c3 == 61) { - return out; - } - c3 = decodeChars[c3]; - } while (i < len && c3 == -1); - if (c3 == -1) { - break; - } - out.writeByte(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2)); -// c4 - do { - c4 = str.charCodeAt(i++) & 0xff; - if (c4 == 61) { - return out; - } - c4 = decodeChars[c4]; - } while (i < len && c4 == -1); - if (c4 == -1) { - break; - } - out.writeByte(((c3 & 0x03) << 6) | c4); - } - return out; - } -} +/**********************************************************\ + | | + | The implementation of PHPRPC Protocol 3.0 | + | | + | Base64.as | + | | + | Release 3.0.1 | + | Copyright by Team-PHPRPC | + | | + | WebSite: http://www.phprpc.org/ | + | http://www.phprpc.net/ | + | http://www.phprpc.com/ | + | http://sourceforge.net/projects/php-rpc/ | + | | + | Authors: Ma Bingyao | + | | + | This file may be distributed and/or modified under the | + | terms of the GNU General Public License (GPL) version | + | 2.0 as published by the Free Software Foundation and | + | appearing in the included file LICENSE. | + | | + \**********************************************************/ +/* Base64 library for ActionScript 3.0. + * + * Copyright: Ma Bingyao + * Version: 1.1 + * LastModified: Apr 12, 2010 + * This library is free. You can redistribute it and/or modify it under GPL. + */ +/* + * interfaces: + * import org.phprpc.util.Base64; + * import flash.utils.ByteArray; + * var data:ByteArray = new ByteArray(); + * data.writeUTFBytes("Hello PHPRPC"); + * var b64:String = Base64.encode(data); + * trace(b64); + * trace(Base64.decode(b64)); + */ +package org.phprpc.util{ +import flash.utils.ByteArray; +public class Base64 { + private static const encodeChars:Array = + ['A','B','C','D','E','F','G','H', + 'I','J','K','L','M','N','O','P', + 'Q','R','S','T','U','V','W','X', + 'Y','Z','a','b','c','d','e','f', + 'g','h','i','j','k','l','m','n', + 'o','p','q','r','s','t','u','v', + 'w','x','y','z','0','1','2','3', + '4','5','6','7','8','9','+','/']; + private static const decodeChars:Array = + [-1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 62, -1, -1, -1, 63, + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, -1, -1, -1, -1, -1, -1, + -1, 0, 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, -1, -1, -1, -1, -1, + -1, 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, -1, -1, -1, -1, -1]; + public static function encode(data:ByteArray):String { + var out:Array = []; + var i:int = 0; + var j:int = 0; + var r:int = data.length % 3; + var len:int = data.length - r; + var c:int; + while (i < len) { + c = data[i++] << 16 | data[i++] << 8 | data[i++]; + out[j++] = encodeChars[c >> 18] + encodeChars[c >> 12 & 0x3f] + encodeChars[c >> 6 & 0x3f] + encodeChars[c & 0x3f]; + } + if (r == 1) { + c = data[i++]; + out[j++] = encodeChars[c >> 2] + encodeChars[(c & 0x03) << 4] + "=="; + } + else if (r == 2) { + c = data[i++] << 8 | data[i++]; + out[j++] = encodeChars[c >> 10] + encodeChars[c >> 4 & 0x3f] + encodeChars[(c & 0x0f) << 2] + "="; + } + return out.join(''); + } + public static function decode(str:String):ByteArray { + var c1:int; + var c2:int; + var c3:int; + var c4:int; + var i:int; + var len:int; + var out:ByteArray; + len = str.length; + i = 0; + out = new ByteArray(); + while (i < len) { +// c1 + do { + c1 = decodeChars[str.charCodeAt(i++) & 0xff]; + } while (i < len && c1 == -1); + if (c1 == -1) { + break; + } +// c2 + do { + c2 = decodeChars[str.charCodeAt(i++) & 0xff]; + } while (i < len && c2 == -1); + if (c2 == -1) { + break; + } + out.writeByte((c1 << 2) | ((c2 & 0x30) >> 4)); +// c3 + do { + c3 = str.charCodeAt(i++) & 0xff; + if (c3 == 61) { + return out; + } + c3 = decodeChars[c3]; + } while (i < len && c3 == -1); + if (c3 == -1) { + break; + } + out.writeByte(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2)); +// c4 + do { + c4 = str.charCodeAt(i++) & 0xff; + if (c4 == 61) { + return out; + } + c4 = decodeChars[c4]; + } while (i < len && c4 == -1); + if (c4 == -1) { + break; + } + out.writeByte(((c3 & 0x03) << 6) | c4); + } + return out; + } +} } \ No newline at end of file diff --git a/blocks/mfavatar/source/script.as b/source/script.as similarity index 96% rename from blocks/mfavatar/source/script.as rename to source/script.as index 44510d8..b4aa95d 100644 --- a/blocks/mfavatar/source/script.as +++ b/source/script.as @@ -1,220 +1,220 @@ -import flash.display.SimpleButton; -import flash.display.DisplayObject; -import flash.media.Camera; -import flash.events.Event; -import flash.events.MouseEvent; -import flash.display.BitmapData; -import flash.utils.ByteArray; -import com.adobe.images.JPGEncoder; -import flash.display.MovieClip; -import flash.display.Bitmap; -import org.phprpc.util.Base64; -import flash.net.URLLoader; -import flash.net.URLRequest; -import flash.net.URLVariables; -import flash.events.IOErrorEvent; -import flash.events.HTTPStatusEvent; - -btn1.enable(false); -combo1.prompt = " "; -mcVideo.theVideo.smoothing = true; - -var cams:Array = Camera.names; -var cam:Camera; -var hasCam:Boolean; -var snapshotCreated:Boolean; -var i:int; -var w:int = 640; -var h:int = 480; -var encodedBytes:ByteArray; -var encoder:JPGEncoder = new JPGEncoder(80); -var ul:URLLoader; -var mcSnapshot:MovieClip; -var sessionid:String = ""; -var identity:String; -var uploadPath:String = "http://moodle281.moodlefreak.com/blocks/mfavatar/upload.php"; - -var text_select_device:String = "Please select your camera device and resolution here:"; -var text_make_snapshot:String = "Save snapshot"; -var text_feed_field:String = "Your camera feed"; -var text_result_field:String = "Your result"; -var failed_saving:String = "Error saving your snapshot!!"; -var success_saving:String = "Snapshot saved!!!"; - -if (this.loaderInfo.parameters.sessionid) -{ - sessionid = this.loaderInfo.parameters.sessionid; -} -if (this.loaderInfo.parameters.identity) -{ - identity = this.loaderInfo.parameters.identity; -} -if (this.loaderInfo.parameters.uploadPath) -{ - uploadPath = this.loaderInfo.parameters.uploadPath; -} -//setting custom strings -if (this.loaderInfo.parameters.text_select_device) -{ - text_select_device = this.loaderInfo.parameters.text_select_device; -} -if (this.loaderInfo.parameters.text_make_snapshot) -{ - text_make_snapshot = this.loaderInfo.parameters.text_make_snapshot; -} -if (this.loaderInfo.parameters.text_feed_field) -{ - text_feed_field = this.loaderInfo.parameters.text_feed_field; -} -if (this.loaderInfo.parameters.text_result_field) -{ - text_result_field = this.loaderInfo.parameters.text_result_field; -} -if (this.loaderInfo.parameters.failed_saving) -{ - failed_saving = this.loaderInfo.parameters.failed_saving; -} - -if (this.loaderInfo.parameters.success_saving) -{ - success_saving = this.loaderInfo.parameters.success_saving; -} - -//init text -select_device.text = text_select_device; -feed_field.text = text_feed_field; -result_field.text = text_result_field; - -button_text.text = text_make_snapshot; -button_text.selectable = false; -button_text.mouseEnabled = false; - -for (i = 0; i < cams.length; i++) -{ - var o:Object = new Object(); - o.label = cams[i]; - combo1.addItem(o); -} - -combo1.addEventListener(Event.CHANGE, _combo1Change); -btn1.addEventListener(MouseEvent.CLICK, _btn1Click); - -function _combo1Change(e:Event):void -{ - if (combo1.selectedIndex > -1) - { - cam = Camera.getCamera(String(combo1.selectedIndex)); - mcVideo.theVideo.attachCamera(cam); - hasCam = true; - snapshotCreated = false; - check(); - } -} - -function check():void -{ - if (hasCam) - { - btn1.enable(); - cam.setMode(w, h, 25); - mcVideo.theVideo.width = w; - mcVideo.theVideo.height = h; - mcVideo.width = 320; - mcVideo.height = 240; - } - else - { - btn1.enable(false); - } - - //upload the snapshot - if (snapshotCreated) - { - _upload(); - } -} - -function _btn1Click(e:MouseEvent):void -{ - btn1.enable(false); - var bmp:BitmapData = new BitmapData(w, h); - bmp.draw(mcVideo); - encodedBytes = encoder.encode(bmp); - if (encodedBytes.length > 0) - { - mcSnapshot.removeChildren(); - var bitmp:Bitmap = new Bitmap(bmp, "auto", true); - mcSnapshot.addChild(bitmp); - mcSnapshot.width = 320; - mcSnapshot.height = 240; - snapshotCreated = true; - check(); - } - btn1.enable(true); -} - -function _upload():void -{ - btn1.enable(false); - var b64result:String = Base64.encode(encodedBytes); - ul = new URLLoader(); - - var v:URLVariables = new URLVariables(); - v.sesskey = sessionid; - v.file = b64result; - - var req:URLRequest = new URLRequest(uploadPath); - req.method = URLRequestMethod.POST; - req.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache')); - req.data = v; - - ul.dataFormat = URLLoaderDataFormat.TEXT; - ul.addEventListener(Event.COMPLETE, _snapshotSaved); - ul.addEventListener(IOErrorEvent.IO_ERROR, _snapshotError); - ul.addEventListener(HTTPStatusEvent.HTTP_STATUS, _snapshotStatus); - ul.load(req); - -} - -function _snapshotSaved(e:Event):void -{ - var data:Object = JSON.parse(ul.data); - - if(data.status == true) - { - theText.text = success_saving; - ExternalInterface.call("M.block_mfavatar.saved"); - btn1.enable(); - } - else - { - var error:String = failed_saving; - for (var s:String in data) { - - if(s == "error") - { - error = data[s]; - break; - } - else if(s == "errors") - { - error = data[s][0]; - break; - } - } - - theText.text = error; - ExternalInterface.call("M.block_mfavatar.error" , ul.data); - } -} - -function _snapshotError(e:IOErrorEvent):void -{ - ExternalInterface.call("M.block_mfavatar.error" , e.errorID); -} - -function _snapshotStatus(e:HTTPStatusEvent):void -{ - trace(e.status); - btn1.enable(); +import flash.display.SimpleButton; +import flash.display.DisplayObject; +import flash.media.Camera; +import flash.events.Event; +import flash.events.MouseEvent; +import flash.display.BitmapData; +import flash.utils.ByteArray; +import com.adobe.images.JPGEncoder; +import flash.display.MovieClip; +import flash.display.Bitmap; +import org.phprpc.util.Base64; +import flash.net.URLLoader; +import flash.net.URLRequest; +import flash.net.URLVariables; +import flash.events.IOErrorEvent; +import flash.events.HTTPStatusEvent; + +btn1.enable(false); +combo1.prompt = " "; +mcVideo.theVideo.smoothing = true; + +var cams:Array = Camera.names; +var cam:Camera; +var hasCam:Boolean; +var snapshotCreated:Boolean; +var i:int; +var w:int = 640; +var h:int = 480; +var encodedBytes:ByteArray; +var encoder:JPGEncoder = new JPGEncoder(80); +var ul:URLLoader; +var mcSnapshot:MovieClip; +var sessionid:String = ""; +var identity:String; +var uploadPath:String = "http://moodle281.moodlefreak.com/blocks/mfavatar/upload.php"; + +var text_select_device:String = "Please select your camera device and resolution here:"; +var text_make_snapshot:String = "Save snapshot"; +var text_feed_field:String = "Your camera feed"; +var text_result_field:String = "Your result"; +var failed_saving:String = "Error saving your snapshot!!"; +var success_saving:String = "Snapshot saved!!!"; + +if (this.loaderInfo.parameters.sessionid) +{ + sessionid = this.loaderInfo.parameters.sessionid; +} +if (this.loaderInfo.parameters.identity) +{ + identity = this.loaderInfo.parameters.identity; +} +if (this.loaderInfo.parameters.uploadPath) +{ + uploadPath = this.loaderInfo.parameters.uploadPath; +} +//setting custom strings +if (this.loaderInfo.parameters.text_select_device) +{ + text_select_device = this.loaderInfo.parameters.text_select_device; +} +if (this.loaderInfo.parameters.text_make_snapshot) +{ + text_make_snapshot = this.loaderInfo.parameters.text_make_snapshot; +} +if (this.loaderInfo.parameters.text_feed_field) +{ + text_feed_field = this.loaderInfo.parameters.text_feed_field; +} +if (this.loaderInfo.parameters.text_result_field) +{ + text_result_field = this.loaderInfo.parameters.text_result_field; +} +if (this.loaderInfo.parameters.failed_saving) +{ + failed_saving = this.loaderInfo.parameters.failed_saving; +} + +if (this.loaderInfo.parameters.success_saving) +{ + success_saving = this.loaderInfo.parameters.success_saving; +} + +//init text +select_device.text = text_select_device; +feed_field.text = text_feed_field; +result_field.text = text_result_field; + +button_text.text = text_make_snapshot; +button_text.selectable = false; +button_text.mouseEnabled = false; + +for (i = 0; i < cams.length; i++) +{ + var o:Object = new Object(); + o.label = cams[i]; + combo1.addItem(o); +} + +combo1.addEventListener(Event.CHANGE, _combo1Change); +btn1.addEventListener(MouseEvent.CLICK, _btn1Click); + +function _combo1Change(e:Event):void +{ + if (combo1.selectedIndex > -1) + { + cam = Camera.getCamera(String(combo1.selectedIndex)); + mcVideo.theVideo.attachCamera(cam); + hasCam = true; + snapshotCreated = false; + check(); + } +} + +function check():void +{ + if (hasCam) + { + btn1.enable(); + cam.setMode(w, h, 25); + mcVideo.theVideo.width = w; + mcVideo.theVideo.height = h; + mcVideo.width = 320; + mcVideo.height = 240; + } + else + { + btn1.enable(false); + } + + //upload the snapshot + if (snapshotCreated) + { + _upload(); + } +} + +function _btn1Click(e:MouseEvent):void +{ + btn1.enable(false); + var bmp:BitmapData = new BitmapData(w, h); + bmp.draw(mcVideo); + encodedBytes = encoder.encode(bmp); + if (encodedBytes.length > 0) + { + mcSnapshot.removeChildren(); + var bitmp:Bitmap = new Bitmap(bmp, "auto", true); + mcSnapshot.addChild(bitmp); + mcSnapshot.width = 320; + mcSnapshot.height = 240; + snapshotCreated = true; + check(); + } + btn1.enable(true); +} + +function _upload():void +{ + btn1.enable(false); + var b64result:String = Base64.encode(encodedBytes); + ul = new URLLoader(); + + var v:URLVariables = new URLVariables(); + v.sesskey = sessionid; + v.file = b64result; + + var req:URLRequest = new URLRequest(uploadPath); + req.method = URLRequestMethod.POST; + req.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache')); + req.data = v; + + ul.dataFormat = URLLoaderDataFormat.TEXT; + ul.addEventListener(Event.COMPLETE, _snapshotSaved); + ul.addEventListener(IOErrorEvent.IO_ERROR, _snapshotError); + ul.addEventListener(HTTPStatusEvent.HTTP_STATUS, _snapshotStatus); + ul.load(req); + +} + +function _snapshotSaved(e:Event):void +{ + var data:Object = JSON.parse(ul.data); + + if(data.status == true) + { + theText.text = success_saving; + ExternalInterface.call("M.block_mfavatar.saved"); + btn1.enable(); + } + else + { + var error:String = failed_saving; + for (var s:String in data) { + + if(s == "error") + { + error = data[s]; + break; + } + else if(s == "errors") + { + error = data[s][0]; + break; + } + } + + theText.text = error; + ExternalInterface.call("M.block_mfavatar.error" , ul.data); + } +} + +function _snapshotError(e:IOErrorEvent):void +{ + ExternalInterface.call("M.block_mfavatar.error" , e.errorID); +} + +function _snapshotStatus(e:HTTPStatusEvent):void +{ + trace(e.status); + btn1.enable(); } \ No newline at end of file diff --git a/blocks/mfavatar/source/snapshot.fla b/source/snapshot.fla similarity index 100% rename from blocks/mfavatar/source/snapshot.fla rename to source/snapshot.fla diff --git a/blocks/mfavatar/source/snapshot.swf b/source/snapshot.swf similarity index 100% rename from blocks/mfavatar/source/snapshot.swf rename to source/snapshot.swf diff --git a/blocks/mfavatar/styles.css b/styles.css similarity index 95% rename from blocks/mfavatar/styles.css rename to styles.css index eb1e78b..3d5628e 100644 --- a/blocks/mfavatar/styles.css +++ b/styles.css @@ -1,23 +1,23 @@ -#snapshotholder{ - width: 100%; - height: 420px; -} -#snapshotholder_webrtc video{ - width: 50%; - float: left; -} -#snapshotholder_webrtc hr{ - clear: both; -} -#snapshotholder_webrtc #previewholder{ - width: 50%; - float: left; - overflow: hidden; -} -#snapshotholder_webrtc #previewholder canvas#render { - display: none; -} -#snapshotholder_webrtc #previewholder canvas#preview { - width: 150px; - height: 150px; +#snapshotholder{ + width: 100%; + height: 420px; +} +#snapshotholder_webrtc video{ + width: 50%; + float: left; +} +#snapshotholder_webrtc hr{ + clear: both; +} +#snapshotholder_webrtc #previewholder{ + width: 50%; + float: left; + overflow: hidden; +} +#snapshotholder_webrtc #previewholder canvas#render { + display: none; +} +#snapshotholder_webrtc #previewholder canvas#preview { + width: 150px; + height: 150px; } \ No newline at end of file diff --git a/blocks/mfavatar/swf/expressInstall.swf b/swf/expressInstall.swf similarity index 100% rename from blocks/mfavatar/swf/expressInstall.swf rename to swf/expressInstall.swf diff --git a/blocks/mfavatar/swf/snapshot.swf b/swf/snapshot.swf similarity index 100% rename from blocks/mfavatar/swf/snapshot.swf rename to swf/snapshot.swf diff --git a/blocks/mfavatar/upload.php b/upload.php similarity index 96% rename from blocks/mfavatar/upload.php rename to upload.php index 72a29ee..0d2cd3b 100644 --- a/blocks/mfavatar/upload.php +++ b/upload.php @@ -1,89 +1,89 @@ -. - -/** - * snapshot upload handler - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ - -if (!defined('AJAX_SCRIPT')) { - define('AJAX_SCRIPT', true); -} -define('NO_DEBUG_DISPLAY', true); - -require('../../config.php'); -require_once("$CFG->libdir/gdlib.php"); -$PAGE->set_url('/blocks/mfavatar/upload.php'); - -require_login(get_site(), true, null, true, true); -$file = required_param('file', PARAM_RAW); -$sessionid = required_param('sesskey', PARAM_RAW); -$systemcontext = context_system::instance(); -$array = array('errors' => array(), 'status' => false); - -echo $OUTPUT->header(); // Send headers. - -if ($CFG->disableuserimages) { - - $array['errors'][] = get_string('failed:disableuserimages', 'block_mfavatar'); - -} elseif (!has_capability('moodle/user:editownprofile', $systemcontext)) { - - $array['errors'][] = get_string('failed:permission_editownprofile', 'block_mfavatar'); - -} elseif (!confirm_sesskey($sessionid)) { - - $array['errors'][] = get_string('failed:sesskey', 'block_mfavatar'); -} - -if(empty($array['errors'])) { - - if(stristr($file , 'base64,' )){ - //convert webrtc - $file = explode('base64,',$file); - $file = end($file); - } - - //decode - $file = base64_decode($file); - - if(empty($file)){ - $array['errors'][] = get_string('failed', 'block_mfavatar'); - die(json_encode($array)); - } - - $context = context_user::instance($USER->id, MUST_EXIST); - - $tempfile = tempnam(sys_get_temp_dir(), 'mfavatar'); - file_put_contents($tempfile, $file); - - $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $tempfile); - if ($newpicture != $USER->picture) { - $DB->set_field('user', 'picture', $newpicture, array('id' => $USER->id)); - $array['status'] = true; - } else { - $array['errors'][] = get_string('failed', 'block_mfavatar'); - } - - @unlink($tempfile); -} - +. + +/** + * snapshot upload handler + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ + +if (!defined('AJAX_SCRIPT')) { + define('AJAX_SCRIPT', true); +} +define('NO_DEBUG_DISPLAY', true); + +require('../../config.php'); +require_once("$CFG->libdir/gdlib.php"); +$PAGE->set_url('/blocks/mfavatar/upload.php'); + +require_login(get_site(), true, null, true, true); +$file = required_param('file', PARAM_RAW); +$sessionid = required_param('sesskey', PARAM_RAW); +$systemcontext = context_system::instance(); +$array = array('errors' => array(), 'status' => false); + +echo $OUTPUT->header(); // Send headers. + +if ($CFG->disableuserimages) { + + $array['errors'][] = get_string('failed:disableuserimages', 'block_mfavatar'); + +} elseif (!has_capability('moodle/user:editownprofile', $systemcontext)) { + + $array['errors'][] = get_string('failed:permission_editownprofile', 'block_mfavatar'); + +} elseif (!confirm_sesskey($sessionid)) { + + $array['errors'][] = get_string('failed:sesskey', 'block_mfavatar'); +} + +if(empty($array['errors'])) { + + if(stristr($file , 'base64,' )){ + //convert webrtc + $file = explode('base64,',$file); + $file = end($file); + } + + //decode + $file = base64_decode($file); + + if(empty($file)){ + $array['errors'][] = get_string('failed', 'block_mfavatar'); + die(json_encode($array)); + } + + $context = context_user::instance($USER->id, MUST_EXIST); + + $tempfile = tempnam(sys_get_temp_dir(), 'mfavatar'); + file_put_contents($tempfile, $file); + + $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $tempfile); + if ($newpicture != $USER->picture) { + $DB->set_field('user', 'picture', $newpicture, array('id' => $USER->id)); + $array['status'] = true; + } else { + $array['errors'][] = get_string('failed', 'block_mfavatar'); + } + + @unlink($tempfile); +} + echo json_encode($array); \ No newline at end of file diff --git a/blocks/mfavatar/version.php b/version.php similarity index 97% rename from blocks/mfavatar/version.php rename to version.php index beb4119..1d2bbc4 100644 --- a/blocks/mfavatar/version.php +++ b/version.php @@ -1,32 +1,32 @@ -. - -/** - * Version information for mfavatar block - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ - -defined('MOODLE_INTERNAL') || die(); -$plugin->release = '1.1.0'; -$plugin->maturity = MATURITY_STABLE; -$plugin->version = 2017020800; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2013101800; // Requires this Moodle version +. + +/** + * Version information for mfavatar block + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ + +defined('MOODLE_INTERNAL') || die(); +$plugin->release = '1.1.0'; +$plugin->maturity = MATURITY_STABLE; +$plugin->version = 2017020800; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2013101800; // Requires this Moodle version $plugin->component = 'block_mfavatar'; // Full name of the plugin (used for diagnostics) \ No newline at end of file diff --git a/blocks/mfavatar/view.php b/view.php similarity index 97% rename from blocks/mfavatar/view.php rename to view.php index d0c3b65..c7b5017 100644 --- a/blocks/mfavatar/view.php +++ b/view.php @@ -1,53 +1,53 @@ -. - -/** - * snapshot view page - * - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * - * @package block_mfavatar - * @copyright 2015 MoodleFreak.com - * @author Luuk Verhoeven - **/ -require_once(dirname(__FILE__) . '/../../config.php'); -require_once(dirname(__FILE__) . '/locallib.php'); -require_login(); - -$courseid = required_param('courseid', PARAM_INT); // if no courseid is given -$parentcourse = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); - -$context = context_course::instance($courseid); -$PAGE->set_course($parentcourse); -$PAGE->set_url('/blocks/mfavatar/view.php'); -$PAGE->set_heading($SITE->fullname); -$PAGE->set_pagelayout('incourse'); -$PAGE->set_title(get_string('snapshotpage', 'block_mfavatar')); -$PAGE->navbar->add(get_string('snapshotpage', 'block_mfavatar')); -$PAGE->requires->css('/blocks/mfavatar/styles.css'); - -block_mfavatar_add_javascript_module(); - -$renderer = $PAGE->get_renderer('block_mfavatar'); - -echo $OUTPUT->header(); - -if ($CFG->disableuserimages) { - print_error("disableuserimages", 'block_mfavatar'); -} - -echo $renderer->snapshot_tool(); +. + +/** + * snapshot view page + * + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * + * @package block_mfavatar + * @copyright 2015 MoodleFreak.com + * @author Luuk Verhoeven + **/ +require_once(dirname(__FILE__) . '/../../config.php'); +require_once(dirname(__FILE__) . '/locallib.php'); +require_login(); + +$courseid = required_param('courseid', PARAM_INT); // if no courseid is given +$parentcourse = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + +$context = context_course::instance($courseid); +$PAGE->set_course($parentcourse); +$PAGE->set_url('/blocks/mfavatar/view.php'); +$PAGE->set_heading($SITE->fullname); +$PAGE->set_pagelayout('incourse'); +$PAGE->set_title(get_string('snapshotpage', 'block_mfavatar')); +$PAGE->navbar->add(get_string('snapshotpage', 'block_mfavatar')); +$PAGE->requires->css('/blocks/mfavatar/styles.css'); + +block_mfavatar_add_javascript_module(); + +$renderer = $PAGE->get_renderer('block_mfavatar'); + +echo $OUTPUT->header(); + +if ($CFG->disableuserimages) { + print_error("disableuserimages", 'block_mfavatar'); +} + +echo $renderer->snapshot_tool(); echo $OUTPUT->footer(); \ No newline at end of file