Cáculo de edad en base a una fecha dada:
Código PHP:
   function edad($edad){
list($anio,$mes,$dia) = explode("-",$edad);
$anio_dif = date("Y") - $anio;
$mes_dif = date("m") - $mes;
$dia_dif = date("d") - $dia;
if ($dia_dif < 0 || $mes_dif < 0)
$anio_dif--;
return $anio_dif;
} 
    Cortar imagen con los parámetros x1 x2 y1 y2:
Código PHP:
   function cut_image($src_img, $dst_img, $dst_width, $dst_height,$x, $y) {
    $result = false;
 
    if (file_exists ($src_img)) {
        list($width, $height, $type) = getimagesize($src_img);
 
        $image_tmp = imagecreatetruecolor($dst_width, $dst_height);
        if ($type == 1) {
            $source = imagecreatefromgif($src_img);
        }  elseif ($type == 2) {
            $source = imagecreatefromjpeg($src_img);
        }  elseif ($type == 3) {
            $source = imagecreatefrompng($src_img);
        }
 
        imagecopy($image_tmp, $source, 0, 0, $x, $y - $dst_height, $dst_width, $dst_height);
 
        if ($dst_height > 78 && $dst_width > 78){
            $image_tmp2 = $image_tmp;
            $image_tmp = imagecreatetruecolor(78, 78);
            imagecopyresampled($image_tmp, $image_tmp2, 0, 0, 0, 0, 78, 78, $dst_width, $dst_height);
        }
 
        $result = imagejpeg($image_tmp, $dst_img);
    }
 
    return $result;
} 
    Ordenar un array de objetos:
Código PHP:
   function ordenar_array_de_objetos($array_datos, $campo, $direccion = 'DESC') {
    usort($array_datos, create_function('$item1, $item2', 'return $item1->' . $campo . ' ' . ($direccion === 'ASC' ? '>' : '<') . ' $item2->' . $campo . ';'));
    return $array_datos;
} 
    Código PHP:
   function limpiar_array($data, $keys) {
    $result = array();
    foreach($keys as $key) {
        if(isset($data[$key]))
            $result[$key] = $data[$key];
    }
 
    return $result;
} 
    Código PHP:
   $post = limpiar_array($_POST, 'id', 'nombre', 'apellido');
$nombre = $post['nombre']; 
    $just_numbers obviamente es para solo número...
Código PHP:
   function randomText($length, $just_numbers = false) {
 
    $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
    if($just_numbers) {
        $key = "";
        for($i = 0; $i < $length; $i++) {
    
            $key .= $pattern{rand(0, 9)};
    
        }        
    } else {
        $key = "";
        for($i = 0; $i < $length; $i++) {
    
            $key .= $pattern{rand(0, 35)};
    
        }        
    }
 
    return $key;
 
} 
    $w y $h son los valores máximos, $lw :
Código PHP:
   function getNewSize($w, $h, $lw, $lh) {
    //obtain an new size from start, max dimesions
    if($w > $lw) {
        $percent = ($lw * 100) / $w;
        $w = $lw;
        $h = $h * ($percent / 100);
    }
    if($h > $lh) {
        $percent = ($lh * 100) / $h;
        $h = $lh;
        $w = $w * ($percent / 100);
    }
    return json_encode(array('w' => $w, 'h' => $h));
} 
    Código PHP:
   function validEmail($email) {
 
    return (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) ? true : false;
} 
    Escalar una imagen y guardarla con:
$scale es el valor porcentual de escalamiento
Código PHP:
   function resizeImage($image, $width, $height, $scale) {
    $newImageWidth = ceil($width * $scale);
    $newImageHeight = ceil($height * $scale);
    $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
    $source = imagecreatefromjpeg($image);
    imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
    imagejpeg($newImage, $image, 90);
    chmod($image, 0777);
    return $image;
} 
    Código PHP:
   function crearslug($str, $rep = '-') {
    $map = array(
        '/à|á|å|â/' => 'a',
        '/è|é|ê|ẽ|ë/' => 'e',
        '/ì|í|î/' => 'i',
        '/ò|ó|ô|ø/' => 'o',
        '/ù|ú|ů|û/' => 'u',
        '/ç/' => 'c',
        '/ñ/' => 'n',
        '/ä|æ/' => 'ae',
        '/ö/' => 'oe',
        '/ü/' => 'ue',
        '/Ä/' => 'Ae',
        '/Ü/' => 'Ue',
        '/Ö/' => 'Oe',
        '/ß/' => 'ss',
        '/[^\w\s]/' => ' ',
        '/\\s+/' => $rep,
        sprintf('/^[%s]+|[%s]+$/', preg_quote($rep, '/'), preg_quote($rep, '/')) => '',
    );
    $str = preg_replace(array_keys($map), array_values($map), $str);
    return $str;
} 
    
 


