侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

框架(架构)  /  管理员 发布于 7年前   369

本文实例讲述了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常见数据库操作技巧汇总》

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

您可能感兴趣的文章:

  • CI框架实现优化文件上传及多文件上传的方法
  • CI框架文件上传类及图像处理类用法分析
  • SWFUpload与CI不能正确上传识别文件MIME类型解决方法分享
  • php基于CodeIgniter实现图片上传、剪切功能
  • codeigniter上传图片不能正确识别图片类型问题解决方法
  • 2个Codeigniter文件批量上传控制器写法例子
  • Codeigniter实现多文件上传并创建多个缩略图
  • 使用CodeIgniter的类库做图片上传
  • 解决Codeigniter不能上传rar和zip压缩包问题
  • codeigniter教程之多文件上传使用示例
  • CodeIgniter上传图片成功的全部过程分享
  • CI(CodeIgniter)框架实现图片上传的方法


  • 上一条:
    Yii核心验证器api详解
    下一条:
    CI框架中类的自动加载问题分析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Filament v3.1版本发布(0个评论)
    • docker + gitea搭建一个git服务器流程步骤(0个评论)
    • websocket的三种架构方式使用优缺点浅析(0个评论)
    • ubuntu20.4系统中宿主机安装nginx服务,docker容器中安装php8.2实现运行laravel10框架网站(0个评论)
    • phpstudy_pro(小皮面板)中安装最新php8.2.9版本流程步骤(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-05
    • 2020-02
    • 2020-03
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-11
    • 2021-03
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-08
    • 2023-08
    • 2023-10
    • 2023-12
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客