You are here
CodeIgniter 的图像处理类
星期四, 2016-09-22 21:36 — adminshiping1
- <?php
- class Image extends Controller {
- function Image()
- {
- parent::Controller();
- $this->load->library('image_lib');
- }
- //实例
- function upload($room_id='',$uid='')
- {
- $file_forder = 'rooms/'.date('Y').'/';
-
- $file_path=dirname(__FILE__); //文件目录
- $file_path=str_replace('\\','/',substr($file_path,0,strpos($file_path,'application')))."uploadfiles/";
- if(!is_dir($file_path.$file_forder)){
- mkdir($file_path.$file_forder);
- }
- $file_forder.=date('m-d')."/";
-
- $file_name = date('YmdHis').rand(1000, 9999); //新文件名
-
- $config['upload_path'] = $file_path.$file_forder;//文件保存路径 这儿我用的是实际
-
- $config['allowed_types'] = 'jpg|jpeg|gif|png|bmp|jpe|swf'; //允许上传格式
- $config['max_size'] = '2048'; //允许上传大小
- $config['file_name'] = $file_name; //存放的文件名
- $config['encrypt_name'] = TRUE;
- $this->load->library('upload', $config);
-
- $field_name = 'Filedata'; //上传表单字段名
-
- if ($this->upload->do_upload($field_name))
- {
- $upimg_arr = $this->upload->data();
- $size_arr = getimagesize($upimg_arr['full_path']);
-
-
- if ($size_arr[1] > 400 || $size_arr[0] > 300) {
- $config2['image_library'] = 'gd2';
- $config2['source_image'] = $config['upload_path'].$this->upload->file_name;
- $config2['maintain_ratio'] = TRUE;
- $config2['create_thumb'] = TRUE;
- $config2['thumb_marker'] = '_mid';
- $config2['width'] = 400;
- $config2['height'] = 300;
- $this->image_lib->initialize($config2);
- if ( ! $this->image_lib->resize()){
- echo $this->image_lib->display_errors();
- }
- $this->image_lib->clear();
- $configc['source_image'] = $config['upload_path'].$this->upload->file_name;
- $configc['wm_type'] = 'overlay';
- $configc['quality'] = '80';
- $configc['wm_overlay_path'] = $file_path.'/shuiyin.png';
- $configc['wm_vrt_alignment'] = 'bottom';
- $configc['wm_hor_alignment'] = 'center';
- $configc['wm_padding'] = '-100';
- //$config['dynamic_output'] = 'true';
- $this->image_lib->initialize($configc);
- if(!$this->image_lib->watermark()){
- echo $this->image_lib->display_errors();
- }
- $this->image_lib->clear();
- }
- $data = $this->upload->data();
- $img = $file_forder.$data['file_name']; //返回给SWF AND JQUERY显示的IMAGE地址
- $smallimg = str_replace($this->upload->file_ext, '', $img)."_small".$this->upload->file_ext;
- $midimg = str_replace($this->upload->file_ext, '', $img)."_mid".$this->upload->file_ext;
- exit ('FILEID:'.'/uploadfiles/'.$img."|".$imgid);
- }
- }
- //缩略图
- function index(){
- echo '* 调整图像大小 <br>
- * 创建缩略图 <br>
- * 图像裁剪 <br>
- * 图像旋转 <br>
- * 添加图像水印 <br>
- ';
- }
- //缩略图
- function resize(){
-
- $config['image_library'] = 'gd2';//(必须)设置图像库
- $config['source_image'] = 'ptjsite/upload/55002.jpg';//(必须)设置原始图像的名字/路径
- $config['dynamic_output'] = FALSE;//决定新图像的生成是要写入硬盘还是动态的存在
- $config['quality'] = '90%';//设置图像的品质。品质越高,图像文件越大
- $config['new_image'] = 'ptjsite/upload/resize004.gif';//设置图像的目标名/路径。
- $config['width'] = 575;//(必须)设置你想要得图像宽度。
- $config['height'] = 350;//(必须)设置你想要得图像高度
- $config['create_thumb'] = TRUE;//让图像处理函数产生一个预览图像(将_thumb插入文件扩展名之前)
- $config['thumb_marker'] = '_thumb';//指定预览图像的标示。它将在被插入文件扩展名之前。例如,mypic.jpg 将会变成 mypic_thumb.jpg
- $config['maintain_ratio'] = TRUE;//维持比例
- $config['master_dim'] = 'auto';//auto, width, height 指定主轴线
- $this->image_lib->initialize($config);
- if (!$this->image_lib->resize())
- {
- echo $this->image_lib->display_errors();
- }else{
- echo "成功的";
- }
- }
- //图像裁剪
- function crop(){
- $config['image_library'] = 'gd2';//设置图像库
- $config['source_image'] = 'ptjsite/upload/004.gif';//(必须)设置原始图像的名字/路径
- $config['dynamic_output'] = FALSE;//决定新图像的生成是要写入硬盘还是动态的存在
- $config['quality'] = '90%';//设置图像的品质。品质越高,图像文件越大
- $config['new_image'] = 'ptjsite/upload/crop004.gif';//(必须)设置图像的目标名/路径。
-
- $config['width'] = 75;//(必须)设置你想要得图像宽度。
- $config['height'] = 50;//(必须)设置你想要得图像高度
- $config['maintain_ratio'] = TRUE;//维持比例
- $config['x_axis'] = '30';//(必须)从左边取的像素值
- $config['y_axis'] = '40';//(必须)从头部取的像素值
-
- $this->image_lib->initialize($config);
-
- if (!$this->image_lib->crop())
- {
- echo $this->image_lib->display_errors();
- }else{
- echo "成功的";
- }
- }
-
-
- //图像旋转
- function rotate(){
- $config['image_library'] = 'gd2';//(必须)设置图像库
- $config['source_image'] = 'ptjsite/upload/001.jpg';//(必须)设置原始图像的名字/路径
- $config['dynamic_output'] = FALSE;//决定新图像的生成是要写入硬盘还是动态的存在
- $config['quality'] = '90%';//设置图像的品质。品质越高,图像文件越大
- $config['new_image'] = 'ptjsite/upload/rotate001.jpg';//设置图像的目标名/路径
- $config['rotation_angle'] = 'vrt';//有5个旋转选项 逆时针90 180 270 度 vrt 竖向翻转 hor 横向翻转
- $this->image_lib->initialize($config);
-
- if ( ! $this->image_lib->rotate())
- {
- echo $this->image_lib->display_errors();
- }
- }
-
- //文字水印
- function watermark(){
- $config['image_library'] = 'gd2';//(必须)设置图像库
- $config['source_image'] = 'ptjsite/upload/003.jpg';//(必须)设置原图像的名字和路径. 路径必须是相对或绝对路径,但不能是URL.
- $config['dynamic_output'] = FALSE;//TRUE 动态的存在(直接向浏览器中以输出图像),FALSE 写入硬盘
- $config['quality'] = '90%';//设置图像的品质。品质越高,图像文件越大
- $config['new_image'] = 'ptjsite/upload/crop004.gif';//设置图像的目标名/路径。
-
- $config['wm_type'] = 'overlay';//(必须)设置想要使用的水印处理类型(text, overlay)
- $config['wm_padding'] = '5';//图像相对位置(单位像素)
- $config['wm_vrt_alignment'] = 'middle';//竖轴位置 top, middle, bottom
- $config['wm_hor_alignment'] = 'center';//横轴位置 left, center, right
- $config['wm_vrt_offset'] = '0';//指定一个垂直偏移量(以像素为单位)
- $config['wm_hor_offset'] = '0';//指定一个横向偏移量(以像素为单位)
-
- $config['wm_text'] = 'Copyright 2008 - John Doe';//(必须)水印的文字内容
- $config['wm_font_path'] = 'ptj_system/fonts/type-ra.ttf';//字体名字和路径
- $config['wm_font_size'] = '16';//(必须)文字大小
- $config['wm_font_color'] = 'FF0000';//(必须)文字颜色,十六进制数
- $config['wm_shadow_color'] = 'FF0000';//投影颜色,十六进制数
- $config['wm_shadow_distance'] = '3';//字体和投影距离(单位像素)。
-
-
- $this->image_lib->initialize($config);
- $this->image_lib->watermark();
- }
-
- //图像水印
- function watermark2(){
- $config['image_library'] = 'gd2';//(必须)设置图像库
- $config['source_image'] = 'ptjsite/upload/003.jpg';//(必须)设置原图像的名字和路径. 路径必须是相对或绝对路径,但不能是URL.
- $config['dynamic_output'] = FALSE;//TRUE 动态的存在(直接向浏览器中以输出图像),FALSE 写入硬盘
- $config['quality'] = '90%';//设置图像的品质。品质越高,图像文件越大
- $config['new_image'] = 'ptjsite/upload/crop004.gif';//设置图像的目标名/路径。
-
- $config['wm_type'] = 'overlay';//(必须)设置想要使用的水印处理类型(text, overlay)
- $config['wm_padding'] = '5';//图像相对位置(单位像素)
- $config['wm_vrt_alignment'] = 'middle';//竖轴位置 top, middle, bottom
- $config['wm_hor_alignment'] = 'center';//横轴位置 left, center, right
- $config['wm_vrt_offset'] = '0';//指定一个垂直偏移量(以像素为单位)
- $config['wm_hor_offset'] = '0';//指定一个横向偏移量(以像素为单位)
-
-
-
-
- $config['wm_overlay_path'] = 'ptjsite/upload/overlay.png';//水印图像的名字和路径
- $config['wm_opacity'] = '50';//水印图像的透明度
- $config['wm_x_transp'] = '4';//水印图像通道
- $config['wm_y_transp'] = '4';//水印图像通道
-
- $this->image_lib->initialize($config);
- $this->image_lib->watermark();
- }
- }
- ?>
来自
http://blog.sina.com.cn/s/blog_812573180101aysq.html