BigToach.com

PHP Snippets

1 ... « 7 8 9 10 11 »

gradient_text

Information

(PHP Version >= 3.0.6)
Functions used in this snippet: str_replace, strlen, hexdec, substr, dechex, implode.

Description

str gradient_text( str str, str color1, str color2, str html )

Outputs a string as a gradient from color color1 to color color2. It outputs the colors based on the tag of type html.

Snippet

<?php
function gradient_text($str$color1$color2$html='span'){
    
$color1 str_replace('#'''$color1);
    
$color2 str_replace('#'''$color2);
    
$color1 strlen($color1) === $color1{0} . $color1{0} . $color1{1} . $color1{1} . $color1{2} . $color1{2} : $color1;
    
$color2 strlen($color2) === $color2{0} . $color2{0} . $color2{1} . $color2{1} . $color2{2} . $color2{2} : $color2;
    
$endstr '<' $html ' style="color:#' $color1 ';">' $str{0} . '</' $html '>';
    
$colors[0] = hexdec(substr($color202));
    
$colors[1] = hexdec(substr($color222));
    
$colors[2] = hexdec(substr($color242));
    
$colors[3] = hexdec(substr($color102));
    
$colors[4] = hexdec(substr($color122));
    
$colors[5] = hexdec(substr($color142));
    for(
$i=1$i<strlen($str) - 1$i++){
        if(
$str{$i} != ' '){
            
$color = array();
            
$color[] = dechex(($colors[0] - $colors[3]) / strlen($str) * $i $colors[3]);
            
$color[] = dechex(($colors[1] - $colors[4]) / strlen($str) * $i $colors[4]);
            
$color[] = dechex(($colors[2] - $colors[5]) / strlen($str) * $i $colors[5]);
            
$color[0] = strlen($color[0]) < $color[0] . $color[0] : $color[0];
            
$color[1] = strlen($color[1]) < $color[1] . $color[1] : $color[1];
            
$color[2] = strlen($color[2]) < $color[2] . $color[2] : $color[2];
            
$endstr .= '<' $html ' style="color:#' implode($color,'') . '">' $str{$i} . '</' $html '>';
        }else{
            
$endstr .= ' ';
        }
    }
    
$endstr .= '<' $html ' style="color:#' $color2 ';">' $str{(strlen($str)-1)} . '</' $html '>';
    return 
$endstr;
}
?>

Example

gradient_text Can be used in the following way:

<?php
echo gradient_text('red will turn into blue''#FF0000''#0000FF');
// out puts "red will turn into blue" as a gradient from red to blue

echo gradient_text('red will turn into blue''f00''00f''font');
// outputs the same as above, but uses a font tag instead of span
// ( i dunno why you would do this but just in case you really want to go against good coding standards)
?>

Added on Jul 5th at 4 am by Scott - 0 Comments


get_world_pop

Information

(PHP Version >= 3.0.12)
Functions used in this snippet: date, strtotime, time, number_format.

Description

str get_world_pop( [int seconds[, bool english]] )

calling get_world_pop with no arguements will return a formatted string of the estimated current world population at that second. If you supply the seconds parameter it will supply the world population for the date as long as it is between the years of 2005 and 2048. If the english value is set to false the formatted string will have periods instead of commas seperating thousands and hundreds etc.

Snippet

<?php
function get_world_pop($seconds=NULL,$english=true){
    
$pops = array(
            
"2005" => array( "start" => 6451058790"added" => 74427813),
            
"2006" => array( "start" => 6525486603"added" => 74629207),
            
"2007" => array( "start" => 6600115810"added" => 74940532),
            
"2008" => array( "start" => 6675056342"added" => 75228043),
            
"2009" => array( "start" => 6750284385"added" => 75466071),
            
"2010" => array( "start" => 6825750456"added" => 75688866),
            
"2011" => array( "start" => 6901439322"added" => 75802963),
            
"2012" => array( "start" => 6977242285"added" => 75615963),
            
"2013" => array( "start" => 7052858248"added" => 75167390),
            
"2014" => array( "start" => 7128025638"added" => 74490498),
            
"2015" => array( "start" => 7202516136"added" => 73766792),
            
"2016" => array( "start" => 7276282928"added" => 73055605),
            
"2017" => array( "start" => 7349338533"added" => 72230253),
            
"2018" => array( "start" => 7421568786"added" => 71289623),
            
"2019" => array( "start" => 7492858409"added" => 70235773),
            
"2020" => array( "start" => 7563094182"added" => 69180831),
            
"2021" => array( "start" => 7632275013"added" => 68144353),
            
"2022" => array( "start" => 7700419366"added" => 67028250),
            
"2023" => array( "start" => 7767447616"added" => 65862519),
            
"2024" => array( "start" => 7833310135"added" => 64679285),
            
"2025" => array( "start" => 7897989420"added" => 63596858),
            
"2026" => array( "start" => 7961586278"added" => 62635490),
            
"2027" => array( "start" => 8024221768"added" => 61689308),
            
"2028" => array( "start" => 8085911076"added" => 60746079),
            
"2029" => array( "start" => 8146657155"added" => 59800227),
            
"2030" => array( "start" => 8206457382"added" => 58925303),
            
"2031" => array( "start" => 8265382685"added" => 58132001),
            
"2032" => array( "start" => 8323514686"added" => 57333600),
            
"2033" => array( "start" => 8380848286"added" => 56512857),
            
"2034" => array( "start" => 8437361143"added" => 55666984),
            
"2035" => array( "start" => 8493028127"added" => 54846652),
            
"2036" => array( "start" => 8547874779"added" => 54058633),
            
"2037" => array( "start" => 8601933412"added" => 53249683),
            
"2038" => array( "start" => 8655183095"added" => 52414904),
            
"2039" => array( "start" => 8707597999"added" => 51542658),
            
"2040" => array( "start" => 8759140657"added" => 50686755),
            
"2041" => array( "start" => 8809827412"added" => 49847392),
            
"2042" => array( "start" => 8859674804"added" => 48957732),
            
"2043" => array( "start" => 8908632536"added" => 48019825),
            
"2044" => array( "start" => 8956652361"added" => 47040969),
            
"2045" => array( "start" => 9003693330"added" => 46074635),
            
"2046" => array( "start" => 9049767965"added" => 45123225),
            
"2047" => array( "start" => 9094891190"added" => 44148176),
            
"2048" => array( "start" => 9139039366"added" => 43161841));
            
    
$yearly 31535999// seconds in a year usually
    
    
if($seconds !== NULL){
        
$year date('Y',$seconds);    
        
$thisyear $seconds strtotime('jan 1 ' $year ' 00:00');
    }else{
        
$year date('Y');    
        
$thisyear time() - strtotime('jan 1 ' $year ' 00:00');
    }
    
$sep $english ',' '.';
    return 
number_format(($pops[$year]['start'] + (($thisyear $yearly) * $pops[$year]['added'])),0,'.',$sep);
}
?>

Example

get_world_pop Can be used in the following way:

<?php
echo get_world_pop(); 
// returns the current world population formatted like 6,578,968,451

echo get_world_pop(strtotime('July 6 2010'));
// returns the world estimated world populating on july 6 2010

echo get_world_pop(NULL,false); 
// returns the current world population formatted like 6.578.968.451
?>

Added on Jul 5th at 1 am by Scott - 0 Comments


upload_files

Information

(PHP Version >= 4.0.3)
Functions used in this snippet: strlen, is_array, is_dir, is_writable, isset, sizeof, in_array, substr, strtolower, strrpos, move_uploaded_file.

Description

array upload_files( str form, str location, mixed types )

Upload any file that matches types filetypes, and send the files from the form name form to the file location of location.

form is the name of the form from the page where the uploads are coming from. This is the only required arguement to the function.

location defaults to the current working directory, but it can be set to any other directory that can be written to.

types are filetype extensions that are allowed. These must be lower case. The types can be either a array or a str. If you want to set types but leave the default location you can set location to NULL

The function returns an array of what happened with the upload. The returned values are mimetype, error code, whether the file was uploaded, file matches the types, filename, and filesize.

Note: This only checks file extensions. You may want to add additional checks of mimetypes to restrict data being loaded onto your server.

Snippet

<?php
function upload_files($form$location='./'$types=array()){
    
$ret = array();
    
$location $location === NULL || $location === '' './' $location;
    
$location $location{(strlen($location) - 1)} === '/' $location $location '/';
    
$types is_array($types) ? $types : array($types);
    if(!
is_dir($location) || !is_writable($location) || !isset($_FILES[$form])){
        return;
    }
    if(
is_array($_FILES[$form]['error'])){
        foreach(
$_FILES[$form]['error'] as $key=>$value){
            
$ret[$key] = array('uploaded'=>false'error'=>$value'typematch'=>false'name'=>$_FILES[$form]['name'][$key], 'mime'=>$_FILES[$form]['type'][$key], 'size'=>$_FILES[$form]['size'][$key]);
            if(
$value == UPLOAD_ERR_OK) {
                
$ret[$key]['uploaded']  = true;
                if(
sizeof($types) === || in_array(substr($_FILES[$form]['name'][$key], strtolower(strrpos($_FILES[$form]['name'][$key], '.')+1)), $types)){
                    
move_uploaded_file$_FILES[$form]['tmp_name'][$key], $location $_FILES[$form]['name'][$key]);
                    
$ret[$key]['typematch'] = true;
                }
            }
        }
    }else{
        
$ret = array('uploaded'=>false'error'=>$_FILES[$form]['error'], 'typematch'=>false'name'=>$_FILES[$form]['name'], 'mime'=>$_FILES[$form]['type'], 'size'=>$_FILES[$form]['size']);
        if(
$_FILES[$form]['error'] == UPLOAD_ERR_OK) {
            
$ret['uploaded'] = true;
            if(
sizeof($types) === || in_array(strtolower(substr($_FILES[$form]['name'], strrpos($_FILES[$form]['name'], '.')+1)), $types)){
                
move_uploaded_file$_FILES[$form]['tmp_name'], $location $_FILES[$form]['name']);
                
$ret['typematch'] = true;
            }
        }
    }
    return 
$ret;
}
?>

Example

upload_files Can be used in the following way:

<?php
// make form as a single file upload with formname 'file'
$files upload_files('file');
// uploads the single file to the current directory no matter the filetype

// make form as a single upload with formname 'image'
$files upload_files('image''./images/', array('gif''jpg''jpeg''png'));
// uploads the single file to the images directory with file extensions of gif, jpg, jpeg, or png

// make a form as an array upload with formname 'images'
$files upload_files('images',NULL,'png');
// uploads multiple files to the current directory as long as the file extension is png
?>

Added on Jun 29th at 3 am by Scott - 2 Comments
Updated on Jun 29th at 3 am.


site_is_up

Information

(PHP Version >= 3)
Functions used in this snippet: fsockopen, str_replace, fclose.

Description

bool site_is_up( str site, int port0, int timeout )

checks site on port port to see if the site is accepting connections (whether the site is up or not). If site is down it will take [i]timeout[i] seconds to timeout. Returns true on success and false on failure.

Snippet

<?php
function site_is_up($site,$port=80,$timeout=1){
    
$port $port === NULL 80 $port;
    
$fp = @fsockopen(str_replace('http://','',$site), $port$errno$errstr$timeout);
    if(
$fp === false){
        return 
false;
    }
    
fclose($fp);
    return 
true;
}
?>

Example

site_is_up Can be used in the following way:

<?php
if(site_is_up('www.bigtoach.com')){
    echo 
'Site is up!!';
}else{
    echo 
'Site is down.';
}
// self explanitory I hope

if(site_is_up('bigtoach.com',NULL,5)){ echo 'UP!'; }
// check bigtoach.com with a 5 second timeout (still on port 80)

if(site_is_up('bigtoach.com',2082)){ echo 'UP!'; }
// check bigtoach.com on port 2082
?>

Added on Jun 27th at 6 pm by Scott - 0 Comments
Updated on Jun 27th at 6 pm.


resize

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: file_exists, substr, strrpos, stristr, imagecreatefromgif, imagecreatefromjpeg, imagecreatefrompng, imageSX, imageSY, ceil, imageCreatetruecolor, imagecreatetrueColor, imagecopyresized, isset, strtolower, imagecopy, imagedestroy.

Description

int resize( str image, int x [, int y [, str wm [, str wml]]])

Uses image to resize based on the size specification of x and y. If y is set to NULL the image will be resized to have its largest dimension be x pixels high/wide.
If wm is specified, it will be used as a watermark image to be placed over the resized image in the region wml. Where the first character places the watermark in the t - Top or b - Bottom and the second character places the watermark on the l - Left or r - Right side of the image.

Snippet

<?php
function resize($image,$x,$y=NULL,$wm=NULL,$wml='br'){
    if(!
file_exists($image)){
        return 
false;
    }
    
$images = array();
    if(
$wm !== '' && $wm !== NULL && file_exists($wm)){
        
$images['wmimg'] = $wm;
    }
    
$images['img'] = $image;
    foreach(
$images as $key=>$value){
        
$type substr($value,strrpos($value,'.'));
        if(
stristr($type,'i')){
            $
$key imagecreatefromgif($value);
        }
        if(
stristr($type,'j')){
            $
$key imagecreatefromjpeg($value);
        }
        if(
stristr($type,'n')){
            $
$key imagecreatefrompng($value);
        }
    }
    
$size = array();
    if(
$y === '' || $y === NULL){
        
$size['x'] = imageSX($img);
        
$size['y'] = imageSY($img);
        if(
$size['x'] >= $size['y']){
            
$size['dest_x'] = $x;
            
$size['dest_y'] = ceil($size['y'] * ($x $size['x']));
        }else{
            
$size['dest_y'] = $x;
            
$size['dest_x'] = ceil($size['x'] * ($x $size['y']));
        }
        
$dest imageCreatetruecolor($size['dest_x'],$size['dest_y']);
    }else{
        
$dest imagecreatetrueColor($x,$y);
        
$size['x'] = imageSX($img);
        
$size['y'] = imageSY($img);
        
$size['dest_x'] = $x;
        
$size['dest_y'] = $y;
    }
    
imagecopyresized($dest$img0000$size['dest_x'], $size['dest_y'], $size['x'], $size['y']);
    if(isset(
$wmimg)){
        
$size['wmx'] = imageSX($wmimg);
        
$size['wmy'] = imageSY($wmimg);
        
$size['wmh'] = strtolower($wml{0}) === 'b' ? ($size['dest_y'] - $size['wmy'] - 2) : 2;
        
$size['wmw'] = strtolower($wml{1}) === 'r' ? ($size['dest_x'] - $size['wmx'] - 2) : 2;
        
imagecopy($dest$wmimg$size['wmw'], $size['wmh'], 00$size['wmx'], $size['wmy']);
        
imagedestroy($wmimg);
    }
    
imagedestroy($img);
    return 
$dest;
}
?>

Example

resize Can be used in the following way:

<?php
header
('Content-type: image/jpeg'); // set your header for jpeg image
imagejpeg(resize('imagefile.png',100));
// outputs imagefile.png as a jpeg constrained to 100 px with its ratio kept the same

header('Content-type: image/png'); // set your header for png image
imagepng(resize('filename.png',100,100));
// outputs filename.png as a png and as a 100X100 image

header('Content-type: image/png'); // set your header for png image
imagepng(resize('imagefile.jpg',100,NULL,'logo.png','bl'));
// outputs imagefile.jpg as a png constrained to 100 px with its ratio kept the same
// it also adds logo.png on top of the image on the bottom left of the image
?>

Added on Jun 26th at 11 pm by Scott - 1 Comment
Updated on Jun 27th at 5 pm.


1 ... « 7 8 9 10 11 »