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('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; }
Related recommendations:
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!