PHP常用函数、代码整理
2017-01-13 00:00
633 查看
摘要: 新手笔记,大神请略过。
1、图片裁剪,生成缩略图
2、php正则匹配获取src
3、thinkphp3.2,计算指定工作日所需自然日数
4、最简单方式在线打开office文件
1、图片裁剪,生成缩略图
function get_pic($cid,$width='660',$height='360'){ $img = M('Picture')->where(array('id'=>$cid))->getField('path'); $img = '.'.$img; //截取 $imgname = substr($img,29,13); $path = './Uploads/thumb/'.$imgname; $filetype = substr($img,-4,4);;//图片类型 if(is_file($path.$filetype)){ //存在 return $path.$filetype; }else{ //不存在 if($filetype=='.jpg'||$filetype=='.JPG'){ $im = imagecreatefromjpeg($img);//参数是图片的存放路径 }else if($filetype=='.png'||$filetype=='.PNG'){ $im = imagecreatefrompng($img);//参数是图片的存放路径 }else if($filetype=='.gif'||$filetype=='.GIF'){ $im = imagecreatefromgif($img);//参数是图片的存放路径 } $maxwidth = $width;//设置图片的最大宽度 $maxheight = $height;//设置图片的最大高度 $name = $path;//图片的名称,随便取吧 resizeImage($im,$maxwidth,$maxheight,$name,$filetype);//调用裁剪函数 return $path.$filetype; } } function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) { $pic_width = imagesx($im); $pic_height = imagesy($im); if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight)) { if($maxwidth && $pic_width>$maxwidth) { $widthratio = $maxwidth/$pic_width; $resizewidth_tag = true; } if($maxheight && $pic_height>$maxheight) { $heightratio = $maxheight/$pic_height; $resizeheight_tag = true; } if($resizewidth_tag && $resizeheight_tag) { if($widthratio<$heightratio) $ratio = $widthratio; else $ratio = $heightratio; } if($resizewidth_tag && !$resizeheight_tag) $ratio = $widthratio; if($resizeheight_tag && !$resizewidth_tag) $ratio = $heightratio; $newwidth = $pic_width * $ratio; $newheight = $pic_height * $ratio; if(function_exists("imagecopyresampled")) { $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数 imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数 } else { $newim = imagecreate($newwidth,$newheight); imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height); } $name = $name.$filetype; imagejpeg($newim,$name); imagedestroy($newim); } else { $name = $name.$filetype; imagejpeg($im,$name); } }
2、php正则匹配获取src
此例以embed标签为例,将embed改为img即可获取img标签的src。 //正则匹配获取资源地址 $check = "<embed[^<>]*?\\ssrc=['\"]?(.*?)['\"].*?>"; preg_match($check,$info['content'],$out); //进行正则表达式匹配 $info['down_path'] = $out[1];// 取出地址
3、thinkphp3.2,计算指定工作日所需自然日数
//获取某天是不是休息日 //@param string $day 可以是单个日期,如20160512,返回0,1,2;可以为2个日期,返回他们之间的工作日数量,“201603,201604” //@return int 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2; //@author 矢志不渝 <745152620@qq.com> function is_holiday($day){ $ch = curl_init(); $url = 'http://apis.baidu.com/xiaogg/holiday/holiday?d='.$day; $header = array( 'apikey: 05112b654551389be96adc8eac1350ea', ); // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); return json_decode($res); } // 获取达到指定工作日需要的自然日数量 //@param int $days 工作日数 //@return int $num 自然日数 //@author 矢志不渝 <745152620@qq.com> function get_days($days){ $time = time();// 取当前时间戳,同时作为订单号 $today = date('Ymd');// 取当天的日期 $day = 0; // 工作日数 $num = 0; // 离下单时间的天数 while ( $day < $days) { $res = is_holiday($today); if($res==0){ $day++; } $today++; $num++; } return $num; }
4、最简单方式在线打开office文件
使用微软提供的Office Online平台只需要一个网址即可在线查看Xls,doc,PPT等文档 http://view.officeapps.live.com/op/view.aspx?src=要查看的文档地址[/code] 5、thinkphp3.2 麻烦的分页//分页显示 $p=$_GET['p']==''?'1':$_GET['p'];//获取传过来的页码,如果为空则给值1 $where = 'type_art <> 2 and qawh_document.status=1'; //筛选条件:文章类型不为2,且状态大于0 $total = $document->where($where)->count(); $Page = new \Think\Page($total,'16'); //分页跳转的时候保证查询条件 $Page->parameter['where'] = $where; $Page->setConfig('header','条记录'); $Page->setConfig('prev','上一页'); $Page->setConfig('next','下一页'); $Page->setConfig('first','首页'); $Page->setConfig('last','尾页'); $Page->rollPage = 5; $Page->lastSuffix = false; $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 共%TOTAL_ROW%条记录/共 %TOTAL_PAGE% 页'); $show= $Page->show();// 分页显示输出 //显示资源列表 $list=$document->where($where)->join('qawh_picture ON qawh_document.cover_id = qawh_picture.id')->field('qawh_document.id,title,path,link_id')->order('qawh_document.create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select(); $_p = $Page->show(); $this->assign('_page', $_p); $this->assign('list',$list);
相关文章推荐
- PHP项目开发中最常用的自定义函数整理
- 通达OA公共代码 php常用检测函数
- PHP数组常用函数分类整理
- PHP数组常用函数分类整理
- php中常用字符串处理代码片段整理
- php 常用函数整理
- PHP常用获取文件路径的函数集合整理
- PHP常用函数整理
- phpcms(3) V9 常用函数 及 代码整理(转)
- PHP项目开发中最常用的自定义函数整理
- php中常用的数组函数(整理)
- php GD库常用函数整理
- PHP常用函数整理
- PHP 采集常用函数整理
- PHP常用技巧总结(附函数代码)
- 综合整理了自己收藏的php几类常用函数
- php基础系列:php绘图技术常用函数整理
- PHP 常用函数整理
- 整理: php常用函数收集并附解释说明
- php 不常用的函数整理