Gorilla3D Primative Teachings

Fact #2: Joseph says nothing is impossible, only woman are.
Search
Subscribe to Feed:

PHP & Firefox 3 AJAX Image Upload


Friday, August 15, 2008

PHP + Firefox3 AJAX Image Upload

Here is a sample dump of using firefox3's getAsDataURL to push to php and using GD to process it into an image and then save to file.

  1.  
  2. /* Ajax Functions */
  3. function Ajax(url, options) {
  4. if (typeof XMLHttpRequest!='undefined') {var page = new XMLHttpRequest();}
  5. else if (window.createRequest) { var page = window.createRequest();}
  6. else if (window.ActiveXObject) { var page = new ActiveXObject('MSXML2.XMLHTTP.3.0');}
  7. else { alert('Ooops, we failed to process your request. Tell us your browser please!'); }
  8. if (page) {
  9. if (options.method == 'post') {
  10. page.open('POST', url, true);
  11. page.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded") }
  12. else {
  13. page.open('GET', url, true);
  14. }
  15. page.onreadystatechange = function() {
  16. if (page.readyState == 4 && page.status == 200) {
  17. if ('function' == typeof options.onComplete) options.onComplete(page.responseText);
  18. }
  19. }
  20. if (options.method == 'post') {
  21. page.send(options.data);
  22. } else {
  23. page.send(null);
  24. }
  25. } else {
  26. alert('There has been a problem processing your request');
  27. }
  28. }
  29. function writeImage() {
  30. var imgdata = $('imagefile').files.item(0).getAsDataURL();
  31. var imgsize = $('imagefile').files.item(0).fileSize;
  32. var imgname = $('imagefile').files.item(0).fileName;
  33. Ajax("post.image.php", {"method":"post", "data": {"data":imgdata, "size":imgsize, "name":imgname}, "onComplete": function(image) { alert('uploaded'); }});
  34. }
  35.  


  1.  
  2. <input type="file" id="imagefile" name="imagefile" onchange="writeImage()"/>
  3.  


  1.  
  2. <?php
  3. $uploadfile = 'temp.jpg';
  4. //-- Parse the data
  5. $img_str = $_POST['data'];
  6. $img_str = explode('base64,', $img_str);
  7. $img_str = $img_str[1];
  8. $img_str = base64_decode($img_str);
  9. //-- Create image from string
  10. $image = imagecreatefromstring($img_str);
  11. //-- Save to file
  12. imagejpeg($image, $uploadfile, 85);
  13. ?>
  14.  


0 comments

Modular C++ Web Server / Framework


Sunday, August 10, 2008

I am working on a modular cpp web server to allow compile c++ shared / dynamic libraries. This way if anyone is ever wanting to challenge themselves, they can do so by writing a module for the deamon using the framework.

Bare-Bone sample library
  1.  
  2. #include <stdio.h>
  3. #include "HttpModule.h"
  4.  
  5. class hello : public HttpModule {
  6. public:
  7. static char *get() {
  8. char *temp = "Hello World from a c++ sample";
  9. return temp;
  10. }
  11. };
  12.  
  13. // the class factories
  14. extern "C" HttpModule* create() {
  15. return new hello;
  16. }
  17.  
  18. extern "C" void destroy(HttpModule* p) {
  19. delete p;
  20. }
  21.  

Compile the dynamic lib
  1.  
  2. # compile into a shared library
  3. g++ -fPIC -c hello.cpp
  4. g++ -shared -o libhello.so hello.o
  5. # Using g++ on intel mac
  6. # g++ -dynamiclib -o libhello.so hello.o
  7.  


How does one then use this library on the server, well... I'll eventually make a configuration file, but for now you ave to directly edit the webserver.
  1.  
  2. module_map http_modules [] = {
  3. {"/helloworld", "./libhello.so"}
  4. };
  5.  


That correlates to http://localhost/helloworld and outputs "Hello World from a c++ sample"

0 comments

Gorilla Db - (Improved) MySQLi Wrapper


Sunday, August 3, 2008

I've updated my mysqli wrapper to handle binds better and easier to new people trying to use prepared statements. I've also added Non-Filtered binds, because I'll also be release my Table class to help with simple queries you always end up running.

Db is a connection manager, it will use the connection you first create throughout your application. You can continue to call Db:init() and as long as a connection has already been made, you can run queries. Best of all It has prepared statements thanks to Wrapping MySQLi. For documentation and download please visit Gorilla Labs

  1.  
  2. //-- Prepared Statements
  3. // Execute a query
  4. $db->prepare('SELECT * FROM blog_entries WHERE id=?')->bind(23)->query();
  5. // or
  6. $db->prepare(SELECT * FROM blog_entries WHERE id=?);
  7. $db->bind(23);
  8. $db->query();
  9. // you can also stack binds
  10. $db->prepare('SELECT * FROM blog_entries WHERE id=? and title=?')
  11. ->bind(23)->bind('Hello World')->query();
  12. $db->prepare('SELECT * FROM blog_entries WHERE id=? and title=?')
  13. ->bind(array(23,'Hello World'))->query();
  14. $entry = $db->fetchOne();
  15.  
  16. // you can do can use them all in the same parameter
  17. $db->prepare('SELECT * FROM events WHERE name LIKE ? OR name LIKE ? and location LIKE ?')
  18. ->bind('%My%', '%Event%', '%San%')
  19. ->query();
  20.  
  21. // you can do can use an array and if they have keys you can assign you ?'s to them
  22. $db->prepare('SELECT * FROM events WHERE name LIKE ?arg1 OR name LIKE ?arg2 and location LIKE ?')
  23. ->bind(array('arg2'=>'%Event%'), '%San%', array('arg1'=>'%My%'))
  24. ->query();
  25.  
  26. // a cleaner example notice they can be in any order but you probably shouldn't
  27. $db->prepare('SELECT * FROM events WHERE name LIKE ?arg1 OR name LIKE ?arg2 and location LIKE ?')
  28. ->bind(array('arg2'=>'%Event%', '%San%', 'arg1'=>'%My%'))
  29. ->query();
  30.  
  31. // and yes keys can be used multiple times
  32. $db->prepare('SELECT * FROM events WHERE name LIKE ?arg1 OR name LIKE ?arg1 and location LIKE ?')
  33. ->bind(array('%San%', 'arg1'=>'%My%'))
  34. ->query();
  35.  


0 comments

Wordpress Plugin Development: Not so bad?


Saturday, August 2, 2008

When I had tried Drupal for size the admin area was so complex I'd never recommend it to a sane person. Wordpress has a much more simple admin area, but whats impressed me is how simple their API is. I wish it was more documented, but its given me a good run.

Wordpress's Front-end (What everyone else see) is crap. But its Back-end is not half bad. I get access to their editor, users and so on.

So later this week I'll post a really clean plugin so you can all agree with me :)

0 comments

Gorilla Db - MySQLi Wrapper


Monday, April 7, 2008

Db has been used in a number of my projects, the main focus of this class is to simplify the overall process of prepared statements. As this was migrated into my work place need for docs and log functions was created.

Db is a connection manager, it will use the connection you first create throughout your application. You can continue to call Db:init() and as long as a connection has already been made, you can run queries. Best of all It has prepared statements thanks to Wrapping MySQLi. For documentation and download please visit Gorilla Labs

  1.  
  2. $db = Db::init('localhost', 'root', 'pass', 'clients');
  3.  
  4. //-- Prepared Statements
  5. $rows = $db->query('SELECT * FROM blog_entries WHERE pub_date < ?', array('2008-04-01'));
  6.  
  7. echo 'Number of result: ' . $db->count();
  8. echo '<br/>';
  9.  
  10. echo 'SQL executed: ' . $db->debug();
  11. echo '<br/>';
  12.  
  13. $rows = $db->fetchAll();
  14. /* Output:
  15.  * Number of result: 22
  16.  * SQL executed: SELECT * FROM blog_entries WHERE pub_date < '2008-04-01'
  17.  */
  18.  


0 comments