CodeIgniter图像处理类的深入解析

所属分类: 网络编程 / PHP编程 阅读数: 599
收藏 0 赞 0 分享
image.php
复制代码 代码如下:

<?php
class Image extends Controller {
    function Image()
    {
    parent::Controller();  
    $this->load->library('image_lib');  
    }

    //缩略图
    function index(){
        echo '* 调整图像大小 <br>
            * 创建缩略图 <br>
            * 图像裁剪 <br>
            * 图像旋转 <br>
            * 添加图像水印 <br>
        ';
    }
    //缩略图
    function resize(){
    /*
    注意
    当$config['create_thumb']等于FALSE并且$config['new_image']没有指定时,会调整原图的大小
    当$config['create_thumb']等于TRUE并且$config['new_image']没有指定时,生成文件名为(原图名 _thumb.扩展名)
    当$config['create_thumb']等于FALSE并且$config['new_image']指定时,生成文件名为$config['new_image']的值
    当$config['create_thumb']等于TRUE并且$config['new_image']指定时,生成文件名为(原图名 _thumb.扩展名)
    */
        $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';//字体和投影距离(单位像素)。
        /* 图像水印参数设置 */
        /*
        $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();
    }

    //图像水印
    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_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';//字体和投影距离(单位像素)。
        */

        /* 图像水印参数设置 */
        $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();
    }
}
?>

更多精彩内容其他人还在看

php导出CSV抽象类实例

这篇文章主要介绍了php导出CSV抽象类及其用法示例,可实现循环导出功能,从而避免内存不足的问题,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现的zip文件内容比较类

这篇文章主要介绍了php实现的zip文件内容比较类及其用法,可实现比较两个zip文件的内容,返回新增、删除、及相同的文件列表,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

php实现获取及设置用户访问页面语言类

这篇文章主要介绍了php实现获取及设置用户访问页面语言类,可实现获取/设置用户访问的页面语言,如果用户没有设置访问语言,则读取Accept-Language,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件

最近做一个在线教学网的项目,需要实现上传任意格式视频自动为h264标准视频,使用html5播放。最终使用PHP+FFMPEG实现,在此将详细解决方案分享给大家!
收藏 0 赞 0 分享

PHP会话控制:Session与Cookie详解

这篇文章主要介绍了PHP会话控制:Session与Cookie详解,本文详细讲解了PHP中Session与Cookie的相关知识,涵盖面较广,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享

这篇文章主要介绍了PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享,这是一个比较常用的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP获取mysql数据表的字段名称和详细信息的方法

这篇文章主要介绍了PHP获取mysql数据表的字段名称和详细信息的方法,本文同时还给出了获取数据表结构、列出数据库数据表等方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP中的output_buffering详细介绍

这篇文章主要介绍了PHP中的output_buffering详细介绍,本文讲解了output buffering的一些高级用法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP错误Warning: Cannot modify header information - headers already sent by解决方法

这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP常用编译参数中文说明

这篇文章主要介绍了PHP常用编译参数中文说明,本文用详细的中文注解了PHP编译参数的作用,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多