File: /www/wwwroot/bb.cwoyt.com/Application/Home/Common/function.php
<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 赛脑 <2233759493@qq.com> <http://www.sn.com>
// +----------------------------------------------------------------------
/**
* 前台公共库文件
* 主要定义前台公共函数库
*/
/**
* 检测验证码
* @param integer $id 验证码ID
* @return boolean 检测结果
* @author 赛脑 <2233759493@qq.com>
*/
function check_verify($code, $id = 1){
$verify = new \COM\Verify();
return $verify->check($code, $id);
}
/**
* 获取列表总行数
* @param string $category 分类ID
* @param integer $status 数据状态
* @author 赛脑 <2233759493@qq.com>
*/
function get_list_count($category, $status = 1){
static $count;
if(!isset($count[$category])){
$count[$category] = D('Document')->listCount($category, $status);
}
return $count[$category];
}
/**
* 获取段落总数
* @param string $id 文档ID
* @return integer 段落总数
* @author 赛脑 <2233759493@qq.com>
*/
function get_part_count($id){
static $count;
if(!isset($count[$id])){
$count[$id] = D('Document')->partCount($id);
}
return $count[$id];
}
/**
* 获取导航URL
* @param string $url 导航URL
* @return string 解析或的url
* @author 赛脑 <2233759493@qq.com>
*/
function get_nav_url($url){
switch ($url) {
case 'http://' === substr($url, 0, 7):
break;
case '#' === substr($url, 0, 1):
break;
default:
$url = U($url);
break;
}
return $url;
}
//图片裁剪
function imagecropper($source_path,$filename = '', $target_width, $target_height){
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
// 源图过高
if ($source_ratio > $target_ratio)
{
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio)
{
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else
{
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime)
{
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
$imagefun = 'imagejpeg';
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
$imagefun = 'imagejpeg';
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
$imagefun = 'imagejpeg';
break;
default:
return false;
break;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁剪
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 缩放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
$imagefun($target_image,$filename,100); //图片保存
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
}
/**
* 图片裁剪缩略图函数
* @param $imgurl 图片路径
* @param $width 缩略图宽度
* @param $height 缩略图高度
* @param $smallpic 无图片是默认图片路径
*/
function thumb($imgurl,$width=100,$height=100) {
$getcwd = $_SERVER['CONTEXT_DOCUMENT_ROOT'];
$url = $getcwd.$imgurl;
$newimgurl = newimg($url,$width,$height);
$newimg = newimg($imgurl,$width,$height);
if(file_exists($newimgurl)) return $newimg;
imagecropper($url,$newimgurl,$width,$height);
return $newimg;
}
function newimg($img,$width,$height){
$img_info = parse_url($img);
/* 外部链接直接返回图片地址 */
if (!empty($img_info['host']) && $img_info['host'] != $_SERVER['HTTP_HOST']) {
return $img;
} else {
$pos = strrpos($img, '.');
$img = substr($img, 0, $pos) . '_' . $width . '_' . $height . '.jpg';
return $img;
}
}
/*
*获取IP地址城市
*ip = ip地址
*/
function GetIpLookup($ip = ''){
if(empty($ip)){
$ip = $_SERVER["REMOTE_ADDR"];
}
// $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$url='';
$ipinfo=json_decode(file_get_contents($url));
if($ipinfo->code=='1'){
return false;
}
$data = object_array($ipinfo->data);
$data['province'] = $data['region'];
return $data;
}
function object_array($array) {
if(is_object($array)) {
$array = (array)$array;
} if(is_array($array)) {
foreach($array as $key=>$value) {
$array[$key] = object_array($value);
}
}
return $array;
}
/*
*字符串截取
*
*/
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8'){
if($code == 'UTF-8') {
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen));
return join('', array_slice($t_string[0], $start, $sublen));
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i< $strlen; $i++){
if($i>=$start && $i< ($start+$sublen)){
if(ord(substr($string, $i, 1))>129){
$tmpstr.= substr($string, $i, 2);
}else{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
//if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
return $tmpstr;
}
}