Archive for the ‘Tutorials’ Category

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 »

Using DooPHP Logging Tools

DooPHP comes with its own profiler and logging tool by default. The class that handle this in the framework is DooLog (dooframework/app/logging/DooLog.php)

There are a few methods in this file where you can use for profiling and logging queries or important message in your application. You can log a message by calling:

Doo::logger()->log('Something fishy here!', DooLog::Alert);

Or you can use the Alias methods instead of passing the log level:

Doo::logger()->alert('Something fishy here!');
Doo::logger()->emerg('message...');
Doo::logger()->crit('message...');
Doo::logger()->err('message...');
Doo::logger()->warn('message...');
Doo::logger()->notice('message...');
Doo::logger()->info('message...');
Doo::logger()->trace('message...');

All of the log messages can be organized by category, simply pass in another parameter at the end of the methods:

Doo::logger()->log('Something fishy here!', DooLog::ALERT, 'editpost');
Doo::logger()->emerg('message...', 'editpost');
Doo::logger()->alert('message...', 'editpost');

To view the logged messages, you just have to call showLogs(). By default DooLog will return a neatly formatted XML log which can be filtered by level or category. You can get a plain text log if you need so:

//display all logs
echo Doo::logger()->showLogs();

//display plain text log
echo Doo::logger()->showLogs(false);

//display only logs in category editpost
echo Doo::logger()->showLogs(true, null, 'editpost');

//display only logs in level Alert and category editpost
echo Doo::logger()->showLogs(true, DooLog::ALERT, 'editpost');

When you have to write the log messages into file, all you have to do is call writeLogs(). Similar to showLogs(), it writes the XML string to file by default.


//Creates a log file
More »

DooFlashMessenger

Hi guys, DooFlashMessenger is here, in this tutorial I will explain you how can you use it with just 3 lines of code. Same thing you have in class comments.

Well lets begin, we start with calling class:

$flash = new DooFlashMessenger();

Now we must give access to DooFlashMessenger from view, we do that like this:

DooController::view()->flashMessenger = $flash;

When we can access from view to flashMessenger we can display messages from flashMessenger, we do that with displayMesssages() method.
Here is the code you need to have in your template file:

$flash->displayMessages();

Thats about it, now you just need to add some message to flash messenger, message will be stored in session, with next execution of DooFlashMessenger class messages that are stored in session will be printed with displayMessages() method. Calling displayMessages() method will echo all messages that are stored.

Adding messages to flashMessenger is done by calling method addMessage:

$flash->addMessage("This is just test message");

That’s it if you have any questions please do ask.

More »