0

0

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

php中文网

php中文网

发布时间:2016-12-05 13:28:26

|

1604人浏览过

|

来源于php中文网

原创

本文实例讲述了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代码:
















(最佳大小为 80 X 80 像素)

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

Endel.io
Endel.io

Endel是一款可以创造个性化舒缓声音的应用程序,可帮助您集中注意力、放松身心和入睡。

下载

立即学习PHP免费学习笔记(深入)”;

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

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
vlookup函数使用大全
vlookup函数使用大全

本专题整合了vlookup函数相关 教程,阅读专题下面的文章了解更多详细内容。

26

2025.12.30

金山文档相关教程
金山文档相关教程

本专题整合了金山文档相关教程,阅读专题下面的文章了解更多详细操作。

28

2025.12.30

PS反选快捷键
PS反选快捷键

本专题整合了ps反选快捷键介绍,阅读下面的文章找到答案。

25

2025.12.30

表格中一行两行的方法
表格中一行两行的方法

本专题整合了表格中一行两行的相关教程,阅读专题下面的文章了解更多详细内容。

3

2025.12.30

cpu温度过高解决方法大全
cpu温度过高解决方法大全

本专题整合了cpu温度过高相关教程,阅读专题下面的文章了解更多详细内容。

5

2025.12.30

ASCII码介绍
ASCII码介绍

本专题整合了ASCII码相关内容,阅读专题下面的文章了解更多详细内容。

31

2025.12.30

GPS是什么
GPS是什么

本专题整合了GPS相关内容,阅读专题下面的文章了解更多详细内容。

3

2025.12.30

wifi拒绝接入
wifi拒绝接入

本专题整合了wifi拒绝接入相关教程,阅读下面的文章了解更多详细方法。

9

2025.12.30

丰网速运介绍
丰网速运介绍

本专题整合了丰网速运查询入口以及相关内容,阅读专题下面的文章了解更多内容。

3

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Django 教程
Django 教程

共28课时 | 2.6万人学习

React 教程
React 教程

共58课时 | 3.1万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号