Posts

Showing posts from July 7, 2010

PHP pagination class

[PHP]
class pagination
{
public function __construct()
{
}
public function calculate_pages(&$total_rows, $rows_per_page, &$page_num)
{
$arr = array();
// calculate last page
$last_page = ceil($total_rows / $rows_per_page);
// make sure we are within limits
$page_num = (int) $page_num;
if ($page_num < 1)
{
$page_num = 1;
}
elseif ($page_num > $last_page)
{
$page_num = $last_page;
}
$upto = ($page_num - 1) * $rows_per_page;
$arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
$arr['current'] = $page_num;
if ($page_num == 1)
$arr['previous'] = $page_num;
else
$arr['previous'] = $page_num - 1;
if ($page_num == $last_page)
$arr['next'] = $last_page;
else
$arr['next'] = $page_num + 1;
$arr['last'] = $last_page;
$arr['info'] = 'Page ('.$page_num.' of '.$last_page.')';
$arr['pages'] = $this->get_surrounding_pages($page_num, $last_p…

Display random image

[PHP]
# Init Array
$files = array();
# Get Folder
if($_GET['folder']) {
$folder = $_GET['folder'];
} else {
# Set Default Folder
$folder = '/img/';
}
# Set Full Path
$path = $_SERVER['DOCUMENT_ROOT'] . '/' . $folder;
# Open Directory
if($handle = opendir($path)) {
# Loop Through Directory
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file, -3) == 'gif' || substr($file, -3) == 'png' || substr($file, -3) == 'jpg' || substr($file, -4) == 'jpeg') $files[count($files)] = $file;
}
}
}
# Close Handle
closedir($handle);
# Init Random
$rand = rand(0, count($files)-1);
# Check Header Type
# GIF
if(substr($files[$random], -3) == 'gif') header("Content-type: image/gif");
# JPEG
elseif(substr($files[$random], -3) == 'jpg') header("Content-type: image/jpeg");
elseif(substr($files[$random], -4) == 'jpeg') …

Get text height, width

define("F_SIZE",8);define("F_FONT","arial.ttf");function get_bbox($text){returnimagettfbbox(F_SIZE,0, F_FONT,$text);}function text_height ($text){$box= get_bbox($text);$height=$box[3]-$box[5];return$height;}function text_width ($text){$box= get_bbox($text);$width=$box[4]-$box[6];return$width;}