PHP Snippets
1 ... « 3 4 5 6 7 » ... 10image_borders
Information
(PHP Version >= 4.1.6)Functions used in this snippet: is_array, strtolower, substr, strrpos, imagesx, imagesy, imagecreate, imagecopy, min.
Description
int image_borders( int img, array borders )
Adds borders to the image img. The borders array should have the indexes tl, tt, tr, rr, br, bb, bl, ll correspoding to that area's border. (see the example below for a better explanation)
Note: This function is fairly slow, so it would be best to apply it once, then save it for future showings.
Also, the wider the top(tt) and bottom(bb) and the longer the left(ll) and right(rr) borders are the faster the function will perform. They CAN be longer and wider than the actual image itself.
Snippet
<?php
function image_borders($img, $borders){
if(!is_array($borders)){
return $img;
}
$border_data = array();
$types = array('jpeg'=>'imagecreatefromjpeg', 'jpg'=>'imagecreatefromjpeg', 'gif'=>'imagecreatefromgif', 'png'=>'imagecreatefrompng');
foreach($borders as $key=>$value){
$border_data[$key]['img'] = $types[strtolower(substr($value, strrpos($value, '.') + 1))]($value);
$border_data[$key]['x'] = imagesx($border_data[$key]['img']);
$border_data[$key]['y'] = imagesy($border_data[$key]['img']);
}
$x = imagesx($img);
$y = imagesy($img);
$dest = imagecreate(($x + $border_data['ll']['x'] + $border_data['rr']['x']), ($y + $border_data['tt']['y'] + $border_data['bb']['y']));
$max_x = $x + $border_data['ll']['x'];
$actual_x = $border_data['ll']['x'];
$ttx = $border_data['ll']['x'];
$bbx = $border_data['ll']['x'];
while($actual_x < $max_x){
if($ttx < $max_x){
imagecopy($dest, $border_data['tt']['img'], $ttx, 0, 0, 0, $border_data['tt']['x'], $border_data['tt']['y']);
$ttx += $border_data['tt']['x'];
}
if($bbx < $max_x){
imagecopy($dest, $border_data['bb']['img'], $bbx, ($border_data['ll']['y'] + $y), 0, 0, $border_data['bb']['x'], $border_data['bb']['y']);
$bbx += $border_data['bb']['x'];
}
$actual_x = min($ttx, $bbx);
}
$max_y = $y + $border_data['tt']['y'];
$actual_y = $border_data['tt']['y'];
$lly = $border_data['tt']['y'];
$rry = $border_data['rr']['y'];
while($actual_y < $max_y){
if($lly < $max_y){
imagecopy($dest, $border_data['ll']['img'], 0, $lly, 0, 0, $border_data['ll']['x'], $border_data['ll']['y']);
$lly += $border_data['ll']['y'];
}
if($rry < $max_y){
imagecopy($dest, $border_data['rr']['img'], ($border_data['ll']['x'] + $x), $rry, 0,0, $border_data['rr']['x'], $border_data['rr']['y']);
$rry += $border_data['rr']['y'];
}
$actual_y = min($lly, $rry);
}
imagecopy($dest, $border_data['tl']['img'], 0, 0, 0, 0, $border_data['tl']['x'], $border_data['tl']['y']);
imagecopy($dest, $border_data['tr']['img'], ($x + $border_data['tl']['x']), 0, 0, 0, $border_data['tr']['x'], $border_data['tr']['y']);
imagecopy($dest, $border_data['bl']['img'], 0, ($y + $border_data['tl']['y']), 0, 0, $border_data['bl']['x'], $border_data['bl']['y']);
imagecopy($dest, $border_data['br']['img'], ($x + $border_data['tl']['x']), ($y + $border_data['tl']['y']), 0, 0, $border_data['br']['x'], $border_data['br']['y']);
imagecopy($dest, $img, $border_data['ll']['x'], $border_data['tt']['y'], 0, 0, $x, $y);
return $dest;
}
?>
Example
image_borders Can be used in the following way:
<?php
$img = imagecreatefromjpeg('baseimage.jpg');
// create the base image
$borders = array( 'tl'=>'topleft.jpg',
'tt'=>'top.jpg',
'tr'=>'topright.jpg',
'rr'=>'right.jpg',
'br'=>'bottomright.jpg',
'bb'=>'bottom.jpg',
'bl'=>'bottomleft.jpg',
'll'=>'left.jpg');
// create your array with borders
// Note, the array indexes must be the same as above
$dest = image_borders($img, $borders);
// actually make the image
header('Content-type: image/png');
// send a header
imagepng($dest);
// send it to the browser
?>
age_days
Information
(PHP Version >= 3.0.6)Functions used in this snippet: str_replace, date, substr, strlen.
Description
int age_days( mixed age[, mixed now] )
Takes a date and translates it into how many days it has been since the current time. If the optional now parameter is set, the age will be in days since now.
The date must be formatted in one of the following ways.
01/05/83
010583
01051983
01-05-83
01 05 1983
01,05,83
The year can either be 2 or 4 digits, but it must go month then year and each must be 2 digits long.
Snippet
<?php
function age_days($age, $now=NULL){
$time = array();
$age = str_replace(array('-', '\\', '/', ' ', ','), '', $age);
if($now === NULL){
$time['cd'] = date('d');
$time['cm'] = date('m');
$time['cy'] = date('Y');
}else{
$now = str_replace(array('-', '\\', '/', ' ', ','), '', $now);
$time['cm'] = substr($now, 0, 2);
$time['cd'] = substr($now, 2, 2);
$time['cy'] = substr($now, 4);
$time['cy'] = strlen($time['cy']) > 3 ? $time['cy'] : ($time['cy'] > date('y') ? '19' . $time['cy'] : '20' . $time['cy']);
}
$time['bm'] = substr($age, 0, 2);
$time['bd'] = substr($age, 2, 2);
$time['by'] = substr($age, 4);
$time['by'] = strlen($time['by']) > 3 ? $time['by'] : ($time['by'] > date('y') ? '19' . $time['by'] : '20' . $time['by']);
$time['fd'] = $time['cd'] - $time['bd'];
if($time['fd'] < 0){
$time['fd'] = 31 + $time['fd'];
$time['cm']--;
}
$time['fm'] = $time['cm'] - $time['bm'];
if($time['fm'] < 0){
$time['fm'] = 12 + $time['fm'];
$time['cy']--;
}
$time['fy'] = $time['cy'] - $time['by'];
return (($time['fy'] * 365) + ($time['fm'] * 31) + $time['fd']);
}
?>
Example
age_days Can be used in the following way:
<?php
echo age_days('110583'); // age in days (as of 07/13/05 it is 7921)
echo age_days('11-05-1983', '07-13-2005'); // 7921
?>
dir_recurse
Information
(PHP Version >= 3)Functions used in this snippet: opendir, readdir, is_dir, dir_recurse, filesize, closedir, isset.
Description
array dir_recurse( str dir, bool filesize )
Recursively find all files and folders underneath the folder dir.
If the optional filesize is set to true the array will have the indexes be the filenames, and the values be the filsize of the filenames.
Snippet
<?php
function dir_recurse($dir, $filesize=false){
if ($fh = opendir($dir)) {
while(false !== ($file = readdir($fh))){
if($file != "." && $file != "..") {
if(is_dir($dir . '/' . $file)){
$data[$file] = dir_recurse($dir . '/' . $file, $filesize);
}else{
if($filesize){
$data[$file] = filesize($dir . '/' . $file);
}else{
$data[] = $file;
}
}
}
}
closedir($fh);
}
return (isset($data) ? $data : array());
}
?>
Example
dir_recurse Can be used in the following way:
<?php
print_r(dir_recurse('.'));
// returns the directory structure of the directory that the script is in
print_r(dir_recurse('some/path/to/dir'));
// returns dir structure of some/path/to/dir
print_r(dir_recurse('.', true));
// returns filesizes of all files above the current working directory.
// if you have PHP 5 and you want to easily show the dir folder size you can do the following
function dummy_func($value){
global $_dummy_var;
$_dummy_var += $value;
}
array_walk_recursive(dir_recurse('.'), 'dummy_func');
echo 'That directory is ' . $_dummy_var . ' Bytes big';
?>
random_sig
Information
(PHP Version >= 4.3.0)Functions used in this snippet: array_rand, getimagesize, header, image_type_to_mime_type, readfile, is_numeric.
Description
mixed random_sig( bool image )
If the optional image arguement is left as true, random_sig will output a random image. If image is set to false it will output the html tags for an image, and link if a link is supplied.
(note you cannot have more than one image pointing to the same link)
Snippet
<?php
function random_sig($image=true){
$array = array( 'http://www.google.com' => 'google.gif',
'someimage.jpg',
'anotherimage.png');
$rand = array_rand($array);
$size = getimagesize($array[$rand]);
if($image){
header('Content-type: ' . image_type_to_mime_type($size[2]));
readfile($array[$rand]);
exit;
}else{
if(!is_numeric($rand)){
return '<a href="' . $rand . '"><img src="' . $array[$rand] . '" alt="" /></a>';
}else{
return '<img src="' . $array[$rand] . '" alt="" />';
}
}
return false;
}
?>
Example
random_sig Can be used in the following way:
<?php
echo random_sig(false); // outputs <img src="" /> etc
random_sig(); // outputs an actual image.
// the above can be used like <img src="your_file_name.php" />
?>
piglatin
Information
(PHP Version >= 3)Functions used in this snippet: strlen, explode, strpos, substr.
Description
str piglatin( str str[, bool from] )
Translates str into piglatin. If from is set to true, str will be translated from piglatin into plaintext.
Snippet
<?php
function piglatin($str, $from=false){
$vowels = 'aeiouyAEIOUY';
$letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$end .= '';
$sur = 'ay';
$surlen = strlen($sur);
$words = explode(' ', $str);
if(!$from){
foreach($words as $word){
if(strpos($letters, $word{0})){
if(strpos($vowels, $word{0}) || strlen($word) < 3){
$end .= $word . 'w' . $sur . ' ';
}else{
$end .= substr($word, 1) . $word{0} . $sur . ' ';
}
}
}
}else{
foreach($words as $word){
$wordlen = strlen($word);
if((substr($word, ($wordlen - $surlen - 1)) == 'w' . $sur && strpos($vowels, $word{0})) || $wordlen < (4 + $surlen)){
$end .= substr($word, 0, ($wordlen - $surlen - 1)) . ' ';
}else{
if(substr($word, -2) == $sur){
$end .= $word{($wordlen - $surlen - 1)} . substr($word, 0, ($wordlen - $surlen - 1)) . ' ';
}else{
$end .= $word . ' ';
}
}
}
}
return $end;
}
?>
Example
piglatin Can be used in the following way:
<?php
echo piglatin('oh my goodness its piglatin');
//ohway myway oodnessgay itsway iglatinpay
echo piglatin('ohway myway oodnessgay itsway iglatinpay', true);
// oh my goodness its piglatin
?>
1 ... « 3 4 5 6 7 » ... 10