CI框架封装的常用图像处理方法(缩略图,水印,旋转,上传等)


Posted in PHP onNovember 22, 2016

本文实例讲述了CI框架封装的常用图像处理方法。分享给大家供大家参考,具体如下:

其实微信手机端上图时,列表图最好是缩略图,节省流量,这不,又被移动坑了一把,话费签一分就停机,流量欠到90块才停机,我也是醉了。。。

不说废话了,下面是用CI 的内置处理图像的库写的,小弟不才,遗漏之处敬请指出,谢谢。

/**
* 生成缩略图
* @param  $path 原图的本地路径
* @return null 创建一个 原图_thumb.扩展名 的文件
*
*/
public function dealthumb($path){
    $config['image_library'] = 'gd2';
    $config['source_image'] = $path;
    $config['create_thumb'] = TRUE;
    //生成的缩略图将在保持纵横比例 在宽度和高度上接近所设定的width和height
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 80;
    $config['height'] = 80;
    $this->load->library('image_lib', $config);
    $this->image_lib->resize();
    $this->image_lib->clear();
}
/*
* 处理图像旋转
*/
public function transroate($path,$imgpath){
    $this->load->library('image_lib');
    //(必须)设置图像库
    $config['image_library'] = 'gd2';
    $newname = time().'_rote.jpg';
    //设置图像的目标名/路径
    $config['new_image'] =$imgpath.$newname;
    //(必须)设置原始图像的名字/路径
    $config['source_image'] = $path;
    //决定新图像的生成是要写入硬盘还是动态的存在
    $config['dynamic_output'] = FALSE;
    //设置图像的品质。品质越高,图像文件越大
    $config['quality'] = '90%';
    //有5个旋转选项 逆时针90 180 270 度 vrt 竖向翻转 hor 横向翻转
    $config['rotation_angle'] = 'vrt';
    $this->image_lib->initialize($config);
    if(@$this->image_lib->rotate()){
      $this->image_lib->clear();
      return $config['new_image'];
    }else{
      $this->image_lib->clear();
      return '';
    }
}
/**
* 处理图像水印
*/
public function overlay($path,$imgpath){
    $this->load->library('image_lib');
    $newname = time().'_over.jpg';
    //设置新图像名称
    $config['new_image'] =$imgpath.$newname;
    //调用php gd库 绘图
    $config['image_library'] = 'gd2';
    //源图像 本地地址
    $config['source_image'] = $path;
    //覆盖文字
    $config['wm_text'] = 'Copyright 2015 - Friker';
    //覆盖类型 文字/图像
    $config['wm_type'] = 'text';
    //文字字体类型
    //$config['wm_font_path'] = 'C:\Windows\Fonts\vrinda.ttf';
    //字体大小
    $config['wm_font_size'] = '16';
    //字体颜色
    $config['wm_font_color'] = 'ff0000';
    //垂直方向距离顶端距离
    $config['wm_vrt_alignment'] = '20';
    //水平方向距离左端距离
    $config['wm_hor_alignment'] = 'center';
    //padding
    $config['wm_padding'] = '20';
    $this->image_lib->initialize($config);
    if($this->image_lib->watermark()){
      $this->image_lib->clear();
      return $config['new_image'];
    }else{
      $this->image_lib->clear();
      return '';
    }
}
/**
*  处理图片上传
*  文件上传类 通过前台 上传文件
*/
public function uploadfile(){
    //文件上传部分
    // 处理文件
    // $data = '';
    $this->load->helper('url');
    $formpic = key($_FILES);
    //文件处理部分
    if(false === empty($_FILES[$formpic]['tmp_name'])){
      //设置文件上传的路径
      $upload['upload_path'] = "./public/img/";
      //限制文件上传的类型
      $upload['allowed_types'] = 'jpeg|jpg|gif|png';
      //限制文件上传的大小
      $upload['max_size'] = 2048;
      //设置文件上传的路径
      $upload['file_name'] = date('YmdHis', time()).rand(10000, 99999);
      //加载文件上传配置信息
      $this->load->library('upload', $upload);
      //处理文件上传
      $this->upload->do_upload($formpic);
      //返回文件上传信息
      $image = $this->upload->data();
      /*
       'file_name' => string '2015071702051718388.jpg' (length=23)
       'file_type' => string 'image/jpeg' (length=10)
       'file_path' => string 'E:/wamp/www/testci/public/img/' (length=30)
       'full_path' => string 'E:/wamp/www/testci/public/img/2015071702051718388.jpg' (length=53)
       'raw_name' => string '2015071702051718388' (length=19)
       'orig_name' => string '2015071702051718388.jpg' (length=23)
       'client_name' => string 'u=415761610,1548338330&fm=116&gp=0.jpg' (length=38)
       'file_ext' => string '.jpg' (length=4)
       'file_size' => float 3.74
       'is_image' => boolean true
       'image_width' => int 146
       'image_height' => int 220
       'image_type' => string 'jpeg' (length=4)
       'image_size_str' => string 'width="146" height="220"' (length=24)
       */
      //var_dump($image);
      //返回文件上传名字
      $data = $image['file_name'];
      $this->dealthumb($image['full_path']);
      $this->overlay($image['full_path'],$image['file_path']);
      $this->transroate($image['full_path'],$image['file_path']);//
      $thumbdata = '';
      //生成缩略图名称
      $pos = strripos($image['file_name'], ".");
      $newname = substr($image['file_name'], 0,$pos)."_thumb".substr($image['file_name'], $pos);
      if(file_exists($image['file_path'].$newname)){
        $thumbdata = $newname;
      }
    }
    //$dirroot = $_SERVER['DOCUMENT_ROOT'];
    //$this->dealthumb($dirroot."/public/img/".$data);
    //上传失败
    if(!$data){
      echo json_encode(array('status'=>0,'msg'=>"上传失败!"));
    }else{
    //上传成功
      echo json_encode(array(
        'name'=>$data,
        'pic'=>base_url()."public/img/".$data,
        'picthumb'=>$thumbdata == '' ?$data:$thumbdata
        ));
    }
}

下面是前端的基本html代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/public/stylesheets/bootstrap.min.css" />
<link rel="stylesheet" href="/public/stylesheets/bootstrap-responsive.min.css" />
<link rel="stylesheet" href="/public/stylesheets/matrix-style.css" />
<link rel="stylesheet" href="/public/stylesheets/matrix-media.css" />
<script type="text/javascript" src="/public/javascripts/jquery.min.js"></script>
<script type="text/javascript" src="/public/javascripts/jquery.form.js"></script>
<script type="text/javascript" src="/public/javascripts/jquery.validate.js"></script>
<style type="text/css">
body{background:#eeeeee; margin:0px;}
</style>
</head>
<body>
<div class="control-group">
  <label class="control-label"> 分享logo: </label>
  <div class="controls">
     <input type="file" name="sharepic" id="sharepic"/>
     <input type="hidden" name="act_sharepic" value="" id="act_sharepic"/>(<sapn class="fred">最佳大小为 80 X 80 像素</sapn>)
     <p style="margin:20px 0;"><img src="/public/img/default.png" alt="" id="sharepic_img"></p>
  </div>
</div>
<script type="text/javascript">
$(function () {
  /*****************图片上传部分开始 *******************/
  var act = "<form class='myupload' action='"+"<?php echo site_url('mytest/uploadfile');?>"+"' method='post' enctype='multipart/form-data'></form>";
  $("#sharepic").change(function(){
    $(this).wrap(act);
    $(this).parent(".myupload").ajaxSubmit({
      dataType: 'json',
      success: function(data) {
        var src = data.pic;
        //更改预览图像地址
        $('#sharepic_img').attr("src",src);
        $('#act_sharepic').val(data.name);
        $('#sharepic').unwrap();
      },
      error:function(xhr){
        alert(JSON.parse(xhr));
      }
    });
  });
})
</script>
</body>
</html>

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

PHP 相关文章推荐
杏林同学录(九)
Oct 09 PHP
攻克CakePHP系列三 表单数据增删改
Oct 22 PHP
win2003服务器使用WPS的COM组件的一些问题解决方法
Jan 11 PHP
php缓存技术详细总结
Aug 07 PHP
destoon实现调用热门关键字的方法
Jul 15 PHP
PHP向socket服务器收发数据的方法
Jan 24 PHP
PHP检测用户是否关闭浏览器的方法
Feb 14 PHP
PHP+Ajax简单get验证操作示例
Mar 02 PHP
Laravel中10个有用的用法小结
May 06 PHP
php创建多级目录与级联删除文件的方法示例
Sep 12 PHP
Thinkphp5 如何隐藏入口文件index.php(URL重写)
Oct 16 PHP
PHP对接阿里云虚拟号的实现(号码隐私保护)
Apr 06 PHP
php each 返回数组中当前的键值对并将数组指针向前移动一步实例
Nov 22 #PHP
CI框架中类的自动加载问题分析
Nov 21 #PHP
CI框架常用函数封装实例
Nov 21 #PHP
CI框架数据库查询缓存优化的方法
Nov 21 #PHP
CI框架AR数据库操作常用函数总结
Nov 21 #PHP
CI框架常用经典操作类总结(路由,伪静态,分页,session,验证码等)
Nov 21 #PHP
CI框架入门之MVC简单示例
Nov 21 #PHP
You might like
Memcache 在PHP中的使用技巧
2010/02/08 PHP
第七章 php自定义函数实现代码
2011/12/30 PHP
PHP实现的折半查找算法示例
2017/12/19 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
Opacity.js
2007/01/22 Javascript
类似GMAIL的Ajax信息反馈显示
2010/02/16 Javascript
说说JSON和JSONP 也许你会豁然开朗
2012/09/02 Javascript
关于jQuery对象数据缓存Cache原理以及jQuery.data详解
2013/04/07 Javascript
eclipse导入jquery包后报错的解决方法
2014/02/17 Javascript
写出高效jquery代码的19条指南
2014/03/19 Javascript
浅谈JSON.parse()和JSON.stringify()
2015/07/14 Javascript
JavaScript数据类型学习笔记
2016/01/25 Javascript
微信小程序使用第三方库Immutable.js实例详解
2016/09/27 Javascript
jquery判断类型是不是number类型的实例代码
2016/10/07 Javascript
浅谈react-router HashRouter和BrowserRouter的使用
2017/12/29 Javascript
解析vue路由异步组件和懒加载案例
2018/06/08 Javascript
深入理解 Koa 框架中间件原理
2018/10/18 Javascript
微信小程序日历效果
2018/12/29 Javascript
详解vue修改elementUI的分页组件视图没更新问题
2020/11/13 Javascript
Python函数参数类型*、**的区别
2015/04/11 Python
Python实现图片转字符画的示例
2017/08/22 Python
详解python OpenCV学习笔记之直方图均衡化
2018/02/08 Python
用Python写一个模拟qq聊天小程序的代码实例
2019/03/06 Python
详解python中TCP协议中的粘包问题
2019/03/22 Python
详解python内置常用高阶函数(列出了5个常用的)
2020/02/21 Python
opencv之颜色过滤只留下图片中的红色区域操作
2020/06/05 Python
python中复数的共轭复数知识点总结
2020/12/06 Python
CSS3结构性伪类选择器九种写法
2012/04/18 HTML / CSS
HTML5 canvas实现雪花飘落特效
2016/03/08 HTML / CSS
FILA斐乐中国官方商城:意大利运动品牌
2017/01/25 全球购物
澳大利亚冲浪和时尚服装网上购物:SurfStitch
2017/07/29 全球购物
印度尼西亚最大的电商平台:Tokopedia(印尼版淘宝)
2017/12/02 全球购物
Linux管理员面试经常问道的相关命令
2013/04/29 面试题
公安机关党的群众路线教育实践活动剖析材料
2014/10/10 职场文书
2015年秋季学校开学标语
2015/07/16 职场文书
关于SpringBoot 使用 Redis 分布式锁解决并发问题
2021/11/17 Redis