Archive

Archive for the ‘PHP’ Category

Simple Logger

March 17th, 2009

If you are programming in PHP, I bet you are using “prints and dies” to debug your code. I know its the easiest way to debug any web based program. But some time you “die” just don’t work, you need to know much more about programs flow and what’s happening under the hood.

There are testing and debugging tools available in the open source community like PHPUnit but you need to invest little more time to understand and use.

So as usual I tried simple logging to debug and keep track of “Prints”. Its simple and need no extra knowledge or installation procedure. Just include the class and use it to log the data you want to see after program is executed.
Read more…

PHP , , ,

Online Mp3 Cutter

March 10th, 2009

Playing with music file was never so easy. I bumped in to a library getId3 that reads different file format you and put all the needed file headers and required information in front of you.

It’s a PHP library and supports Ogg, WMA, WMV, ASF, WAV, AVI, AAC, VQF, FLAC, MusePack, Real, QuickTime, Monkey’s Audio, MIDI and more file formats. You can find more info about supported files on their official site.

As usual I am wrapping it in to a simple class, that may help. The wrapper class supports two functionality

  1. Extract
  2. Join

Let see how “Extract” function is used.

1
2
3
4
5
6
7
8
9
10
11
12
13
include('mp3.class.php');
 
$filename = "music.mp3";
$start = 15; //start time marker in seconds
$end = 60; //end time marker in seconds
 
$mp3 = new Mp3($filename);
$extract = $mp3->extract($start,$end);
if($extract===false)
{
	die("Error!");
}
print "File created : $extract";

As you can see you need to specify only two time lines and you will get what you want.
Read more…

PHP ,

PHP wrapper for DBA (dbm-style Database Abstraction) Functions

March 4th, 2009

The DBA functions provide a single, uniform interface to a wide variety of dbm-style databases. In broad way you can think of dbm style databases as simple stored as key-value pairs.

It is not always necessary to use always RDBMS for database oerations, dba will do perfectly fine for small data sets. Specailly I found dba useful when you are doing network trips just to fetch small chunk of data. Here DBA files can come handy, if need to summerize this , it is like

  1. create dba
  2. propagate it over network
  3. access it locally

I tried to develop a simple wrapper for DBA, lets see how that can be used

1
2
3
4
5
$dba = new DbaWrapper("programminglanguages.db","n");
$dba->insert(1001,"PHP");
$dba->insert(1002,"Java");
$dba->insert(1003,"C++");
$dba->close();

The code above creates a database with name ‘programminglanguages’ and inserts few key and values. Following code snippet shows how we can access any key from dba

1
2
3
$dba = new DbaWrapper("programminglanguages.db","r");
print $dba->fetch(1002);
$dba->close();

As you can see you need to specify mode while opening dba database. Following are most commonly used modes -

  • r for read access,
  • w for read/write access to an already existing database,
  • c for read/write access and database creation if it doesn’t currently exist
  • n for create, truncate and read/write access.

Now here is the simple wrapper class -
Read more…

PHP , ,

Generate image thumbnails in PHP

February 17th, 2009

Thumbnails proves to be very useful in presenting various online and print media. Here I would like to share a simple function that extracts thumbnail from image. I used this a lot for simple presentations.

Supported formats : TIFF, JPEG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function Thumbnail($file,$print=true)
{
   $thumbimage = @exif_thumbnail($file, $width, $height, $type);
   if($thumbimage!==false) 
   {
	 if($print)
	 {
	   header('Content-type: ' .image_type_to_mime_type($type));
	   print $thumbimage;
   	 }		   
   	 else
   	 {
	   $handle = fopen ($path_to_new_file, 'w');
	   fwrite($handle, $thumbimage);
	   fclose($handle);
   	 }
   }
   else
   {
	   print "Could not generate thumbnail.";
   }	   
}

Usage:

1
2
$file = "myimage.jpg";   
Thumbnail($file);

Function checks if it can get thumbnail and accordingly sets heigh, width and type parameters. As you can see function can either print the thumbnail or create new image file.

PHP , ,

Website counters

February 8th, 2009

Website counters proves to be one of the best tools that web masters can use to analyze the site performance. There are numerous free and commercial web counter services available on net. Few to list are as -

But, I preferred to give it a try using simple image functions that PHP provides. It’s not that fancy but it serves the purpose. Here are some image samples that I created using this custom class.
Counter  counter2  counter3  counter4
Read more…

PHP ,