006-PHP常用函數封裝

來源:互聯網
上載者:User
本篇文章介紹的內容是PHP常用函數封裝 ,現在分享給大家,有需要的朋友可以參考一下


<?php/** * 擷取來訪者的真實IP */function getRealIp() {    static $realip = null;    if($realip !== null) {        return $realip;    }    if(getenv('REMOTE_ADDR')) {        $realip = getenv('REMOTE_ADDR');    } else if(getenv('HTTP_CLIENT_IP')) {        $realip = getenv('HTTP_CLIENT_IP');    } else if (getenv('HTTP_X_FROWARD_FOR')) {        $realip = getenv('HTTP_X_FROWARD_FOR');    }    return $realip;}/** * 產生隨機字串 * @param int $num 產生的隨機字串的個數 * @return str 產生的隨機字串 * echo randStr(); */function randStr($num=6) {    $str = str_shuffle('abcedfghjkmnpqrstuvwxyzABCEDFGHJKMNPQRSTUVWXYZ23456789');    return substr($str, 0 , $num);}/** * 建立目錄 ROOT.'/upload/2015/01/25/qwefas.jpg' */function createDir() {    $path = '/upload/'.date('Y/m/d');    $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, '.');}/** * 產生縮圖 * * @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) . '/' . randStr() . '.png';    //擷取大圖和縮圖的絕對路徑    $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=>'imagecreatefromgif',        2=>'imagecreatefromjpeg',        3=>'imagecreatefrompng',        15=>'imagecreatefromwbmp'    );    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 ,  '<br>' , $zw , '<br>' , $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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.