June 30, 2009, 2:03 am
Very simple handy little function to generate a random code of number and lower and upper case letters.
Straight forward stuff…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| function makecode($chars = 6)
{
for($i=0; $i<=($chars-1); $i++)
{
$r0 = rand(0,1);
$r1 = rand(0,2); // frequency of lower case
if($r0==0)
$r .= chr(rand(ord('A'),ord('Z')));
elseif($r0==1)
$r .= rand(0,9);
if($r1==0)
$r = strtolower($r);
}
return $r;
} |
$r0 controls if the next digit is a number or a letter
$1 controls if teh letter will be upper or lower. Set it to 0 if you want it to always be lower or change the range to what ever suits you.
Also a number maybe passed to the function to change how many digits in the code to make.
June 30, 2009, 1:42 am
Every now and then we need a simple function that could take any number in bytes taken from filesize() or other source and convert it for better reading. Here are some functions that do the exact same thing but with different methods.
Method 1: Very simple straight forward stuff. Can be customized to different sizes with different limits.
1
2
3
4
5
6
7
8
| function convert_size($num)
{
if ($num >= 1073741824) $num = round($num / 1073741824 * 100) / 100 .' gb';
else if ($num >= 1048576) $num = round($num / 1048576 * 100) / 100 .' mb';
else if ($num >= 1024) $num = round($num / 1024 * 100) / 100 .' kb';
else $num .= ' b';
return $num;
} |
Method 2: (best way) More advance using a loop to count size depth. Very small and compact all the way to YB. Unlike method 1 it runs by a single constant.
1
2
3
4
5
6
7
8
9
10
| function convert_size($size){
$i=0;
$iec = array(" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
while (($size/1024)>1)
{
$size=round($size/1024,2);
$i++;
}
return substr($size,0,strpos($size,'.')+4).$iec[$i];
} |
As you see if you wish to have a large range conversion using the first method then you will have lots of IF statements unlike method two. Iif you are using method one you have more simple flexibility to have different off-sets for sizes.
Method two uses a single while loop to take the size to its lowest by every 1024 and each time it loops it reduces a single multiple of 1024 and counts how many times it reduces it, and at the end shows the corresponding unit size from the array using that count. The array must have the unit sizes in correct order. Good thing about this is if you give it a number that is like 7^20 (that’s 20 zeroes), then it still works just fine as long as the array has that many unit sizes.
June 30, 2009, 1:18 am
It is very simple to show page load times in PHP.
Method 1:
Place this at the very begin of your PHP code:
1
| define('STARTING_MICROTIME', get_microtime()); |
Depending on how you like to organize your code have those functions some where, they are pretty straight forward:
1
2
3
4
5
6
7
8
9
| function execution_time()
{
return sprintf("%01.4f", get_microtime() - STARTING_MICROTIME);
}
function get_microtime()
{
$time = explode(' ', microtime());
return doubleval($time[0]) + $time[1];
} |
And finally just use the execution_time() function to show the total time elapsed since the script started.
Method 2:
Place this at very beginning of your code.
1
| define('PAGE_LOAD_START', microtime(true)); |
And this where you want to display the render time.
1
| <?php echo round((microtime(true) - PAGE_LOAD_START), 4); ?> |
This way is easier IMO. Using constants is not necessary, it just avoids problems in namespaces if your code is OOP like.
June 6, 2009, 2:45 pm
Log time ago I wrote this function for my friend’s gallery website. The function is straight forward and uses the GD library of PHP. You may resize the image by giving it the maximum width and height you want the thumbnail to have. The function acts as an extension of the GD library so to speak, so you have to pass the image resource. An additional argument may be passed stating if you want the thumbnail to be square or not, by default it is false. If it is true the width:height ratio will not be change, it only fills the empty space with black color and places the thumbnail image in the center of the square.
imagethumb(image resource, max width, max height, fill to make square)
1 – (GD image resource) Straight forward teh image resource loaded by GD library.
2 – (int) maximum width of thumbnail
3 – (int) maximum width of thumbnail
4 – (true/false) make thumbnail square
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
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
52
53
54
55
56
| function imagethumb($old_im, $limit_w = 100, $limit_h = 100, $fill = false)
{
//get original dimensions
$old_w = imagesx($old_im);
$old_h = imagesy($old_im);
//first resize by width if overflow else set old to new
if($old_w > $limit_w)
{
//get reduction percent
$reduce_percent = ($limit_w / $old_w) * 100;
//set new w and h
$new_w = ($old_w / 100) * $reduce_percent;
$new_h = ($old_h / 100) * $reduce_percent;
}
else
{
//set as the old dimensions
$new_w = $old_w;
$new_h = $old_h;
}
//second resize by height if still overflows
if($new_h > $limit_h)
{
//get reduction percent
$reduce_percent = ($limit_h / $new_h) * 100;
//set new w and h
$new_w = ($new_w / 100) * $reduce_percent;
$new_h = ($new_h / 100) * $reduce_percent;
}
if($fill == true)
{
//height offset picture, to keep centered
if($new_h < $limit_h)
$h_offset = floor(($limit_h - $new_h)/2);
else
$h_offset = 0;
//width offset picture, to keep centered
if($new_w < $limit_w)
$w_offset = floor(($limit_w - $new_w)/2);
else
$w_offset = 0;
//make new image and copy to it
$new_im = imagecreatetruecolor($limit_w, $limit_h);
}
else
$new_im = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($new_im, $old_im, $w_offset, $h_offset, 0, 0, $new_w, $new_h, $old_w, $old_h);
return $new_im;
} |
January 2, 2009, 2:34 pm
At some point I needed to paginate my results of a large database for a more friendly view. Now I use PHP so this tutorial will fully explain how to paginate using php and SQL database, or just an array with all the data. That way you can use the technique with any other database you like. I will go over the basic mechanics and the add nice things on top like pagination and sorting at the same time and how to build a wicked page navigation for the listing. And also I shall show you how to make a so called server side pagination/sorting using Ajax JavaScript at the end. Continue reading ‘PHP Pagination Guide’ »
January 2, 2009, 4:12 am
My first website was the most confusing piece of code ever. During my time of writing it I learned some usual things and the common used things. I’ll share some of the nice ways to organize your website structure. Those structures may be simple but effective for your own use. Since I am a PHP power user, I’ll make it a PHP based tutorial. In my point of view, having all pages loaded from index is the best for massive websites. That is because you do not share the internal folders with users and everybody uses one single page for viewing, meaning you can password protect everything else on your server. Continue reading ‘PHP Website Structure and Design’ »