Author Archive

Get DooPHP running on Google App Engine

Google App Engine is a PaaS that enables you to build and host web apps on the same systems that power Google applications. App Engine offers fast development and deployment; simple administration, with no need to worry about hardware, patches or backups; and effortless scalability. With the latest Channel API it enables applications to send messages in real time without the use of polling. App engine currently only supports Python and Java and there are no PHP APIs available yet.

However, there’s a workaround to get PHP running on GAE.  With Quercus, you can run PHP on JVM which is supported in GAE infrastructure. Quercus is Caucho Technology’s fast, open-source, 100% Java implementation of the PHP language. Latest version of Quercus supports PHP 5.3.2 thus features like namespace and closure in PHP 5.3 are available. You can use Java classes from PHP(with Quercus) to access various API such as Mail, URL Fetch, XMPP, Task Queues, Google Accounts on App Engine.

Here is an example of DooPHP URI routing demo running on GAE

Now, there are a few steps to get you started with Quercus and GAE.

Step 1: Download and install Resin from http://www.caucho.com/download/

Step 2: Download and install Google App Engine SDK for Java from http://code.google.com/appengine/downloads.html#Google_App_Engine_SDK_for_Java

Step 3: Download this example doophp-gae.zip and upzip it to your preferred location

Step 4: Copy resin.jar

More »

Intro to DooPHP slides (PHP Malaysia meetup 2011)

For those who are looking for the presentation slides for DooPHP used in PHP Malaysia Meetup 2011. You can also click on the link to download the PDF or android apk

More »

Upload and Resize Pictures with DooGdImage

First you must have an upload form in your HTML, notice that the file input field in the form below is profile_pic

<form action="/uploadme" method="post" enctype="multipart/form-data" name="upload-photo">
 <input name="profile_pic" type="file" />
 <input type="submit" value="submit"/>
</form>

In Controller, create an instance of DooGdImage, along with the upload/source path and the path to save your resized pictures:

 Doo::loadHelper('DooGdImage');
 //upload/source path, and output saved path
 $gd = new DooGdImage('/var/www/uploaded/', '/var/www/resized_pic/');

Call uploadImage() to save the uploaded file with a new name. The example below save the picture with the date as file name, img_20100104203245.jpg

$uploadImg = $gd->uploadImage('profile_pic', 'img_' .date('Ymdhis'));

Before your resize the picture, you can set the quality, generated image type and file name suffix (optional)

 $gd->generatedQuality = 85;
 $uploadImage->generatedType="jpg";

 //This thumbnail is 46x46 pixels, resize adaptively (perfect 46x46 crop from center)
 //Pic name is img_201001011200_46x46.jpg
 $gd->thumbSuffix = '_46x46';
 $gd->adaptiveResize($uploadImg,46,46);

You can use createThumb/createSquare to resize the pictures too.

 //Resize propotionally (so will not be perfect 75x75 depends on the image ratio, no cropping done)
 //Pic name is img_201001011200_75x75.jpg
 $gd->thumbSuffix = '_75x75';
 $gd->createThumb($uploadImg, 75, 75);

Some updates*

You can validate if the uploaded images meet your requirements by using checkImageType(), checkImageSize() and checkImageExtension()

  • checkImageType() – check if image mime type is in the allowed list. Default: JPEGs, GIFs and PNGs
  • checkImageExtension() – check if image file extension is in the allowed list.
More »

Profiling and DB Profiling with DooPHP

If you have read the previous tutorial on logging you will find that profiling with DooPHP framework is relatively similar to the way you log messages.

Performance profiling can be used to measure the time & memory needed for the specified code blocks and find out what the performance bottleneck is. Instead of calling log() you change it to beginProfile() and endProfile(). We need to mark the beginning and the end of each code block by inserting the following methods:

Doo::logger()->beginProfile('block_id_here');
//...everything here will be profiled
Doo::logger()->endProfile('block_id_here');

Code blocks need to be nested properly. A code block cannot intersect with another. It must be either at a parallel level or be completely enclosed by the other code block.

All of the profiled results can be organized in category, simply pass in another parameter at the end of the method beginProfile():

//default category is 'application'
Doo::logger()->beginProfile('id', 'editpost');
//...everything here will be profiled
Doo::logger()->endProfile('id');

To retrieve the profiled results, you called getProfileResult(). You have to pass in the block ID as parameter and you will get an associative array which shows you the time and memory used when processing the code block.

Doo::logger()->beginProfile('block_id');
//...everything here will be profiled
Doo::logger()->endProfile('block_id');
$result = Doo::logger()->getProfileResult('block_id');

To view the profiled results, you just have to call showLogs(). By default it will return a neatly

More »