Home  >  Article  >  Backend Development  >  006-PHP common function encapsulation

006-PHP common function encapsulation

不言
不言Original
2018-04-08 14:48:391334browse

This article introduces the common function encapsulation of PHP. Now I share it with you. Friends in need can refer to it


<?php

/**
 * 获取来访者的真实IP
 */
function getRealIp() {
    static $realip = null;
    if($realip !== null) {
        return $realip;
    }

    if(getenv(&#39;REMOTE_ADDR&#39;)) {
        $realip = getenv(&#39;REMOTE_ADDR&#39;);
    } else if(getenv(&#39;HTTP_CLIENT_IP&#39;)) {
        $realip = getenv(&#39;HTTP_CLIENT_IP&#39;);
    } else if (getenv(&#39;HTTP_X_FROWARD_FOR&#39;)) {
        $realip = getenv(&#39;HTTP_X_FROWARD_FOR&#39;);
    }

    return $realip;
}


/**
 * 生成随机字符串
 * @param int $num 生成的随机字符串的个数
 * @return str 生成的随机字符串
 * echo randStr();
 */
function randStr($num=6) {
    $str = str_shuffle(&#39;abcedfghjkmnpqrstuvwxyzABCEDFGHJKMNPQRSTUVWXYZ23456789&#39;);
    return substr($str, 0 , $num);
}


/**
 * 创建目录 ROOT.&#39;/upload/2015/01/25/qwefas.jpg&#39;
 */
function createDir() {
    $path = &#39;/upload/&#39;.date(&#39;Y/m/d&#39;);
    $fpath = ROOT . $path;
    if(is_dir($fpath) || mkdir($fpath , 0777 , true)) {
        return $path;
    } else {
        return false;
    }
}


/**
 * 获取文件后缀
 * @param str $filename 文件名
 * @return str 文件的后缀名,且带点.
 */
function getExt($filename) {
    return strrchr($filename, &#39;.&#39;);
}


/**
 * 生成缩略图
 *
 * @param str $oimg /upload/2016/01/25/asdfed.jpg
 * @param int $sw 生成缩略图的宽
 * @param int $sh 生成缩略图的高
 * @return str 生成缩略图的路径 /upload/2016/01/25/asdfed.png
 */
function makeThumb($oimg , $sw=200 , $sh = 200) {
    //缩略图存放的路径的名称
    $simg = dirname($oimg) . &#39;/&#39; . randStr() . &#39;.png&#39;;

    //获取大图和缩略图的绝对路径
    $opath = ROOT . $oimg;//原图的绝对路径
    $spath = ROOT . $simg;//最终生成的小图

    //创建小画布
    $spic = imagecreatetruecolor($sw, $sh);

    //创建白色
    $white = imagecolorallocate($spic, 255, 255, 255);
    imagefill($spic, 0, 0, $white);

    //获取大图信息
    list($bw , $bh ,$btype) = getimagesize($opath);

    //1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,
    //7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,
    //11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
    $map = array(
        1=>&#39;imagecreatefromgif&#39;,
        2=>&#39;imagecreatefromjpeg&#39;,
        3=>&#39;imagecreatefrompng&#39;,
        15=>&#39;imagecreatefromwbmp&#39;
    );
    if(!isset($map[$btype])) {
        return false;
    }

    //imagecreatefromjpeg(filename)
    $opic = $map[$btype]($opath);//大图资源

    //计算缩略比
    $rate = min($sw/$bw , $sh/$bh);
    $zw = $bw * $rate;//最终返回的小图宽
    $zh = $bh * $rate;//最终返回的缩略小图高

    //imagecopyresampled(dst_image, src_image, dst_x, dst_y,
    //src_x, src_y, dst_w, dst_h, src_w, src_h)
    //echo $rate ,  &#39;<br>&#39; , $zw , &#39;<br>&#39; , $zh ;exit();

    //拷贝图像并修改大小
    //imagecopyresampled($spic, $opic, 0, 0, 0, 0, $zw, $zh, $bw, $bh);
    imagecopyresampled($spic, $opic, ($sw-$zw)/2, ($sh-$zh)/2, 0, 0, $zw, $zh, $bw, $bh);

    //输出/保存图形
    imagepng($spic , $spath);

    //销毁画布(关闭画板)
    imagedestroy($spic);
    imagedestroy($opic);

    return $simg;
}


/**
 * 转义字符串{ 对POST,GET,COOKIE数组进行转义 }
 * @param $arr
 * @return mixed
 */
function _addslashes($arr){
    foreach ($arr as $k=>$v){
        if(is_string($v)){
            $arr[$k] = addslashes($v);
        }elseif(is_array($v)){
            $arr[$k] = _addslashes($v);
        }
    }
    return $arr;
}

Related recommendations:

004-PHP image upload example

005-PHP obtains the visitor’s real IP


The above is the detailed content of 006-PHP common function encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn