thinkPHP框架实现图像裁剪、缩放、加水印的方法

所属分类: 网络编程 / PHP编程 阅读数: 1045
收藏 0 赞 0 分享

本文实例讲述了thinkPHP框架实现图像裁剪、缩放、加水印的方法。分享给大家供大家参考,具体如下:

ThinkPHP 图片处理函数,需要文字水印字体,可在windows下 控制面板 > 大图标(右上角) > 字体 找到需要的字体

/**
* 图像的裁剪、缩放、加水印
* @param string $path    路径
* @param int $width   裁剪的宽度/限制的高度或宽度,当有$height值时此值为图片的宽度,否则为限制的宽度或高度
* @param int $height   [可选]裁剪的高度
* @param boolean $water   [可选]是否加水印
* @param int $word    [可选]水印文字
*/
function zoom_image($path,$width = 300,$height = null,$water = null,$word = 'water'){
  $image = new \Think\Image();
  $image->open($path);
  $imgWidth = $image->width();
  $imgHeight = $image->height();
  // 限制尺寸
  if($width and !$height){
    $maxSize = $width;
    // 宽度或高度大于规定尺寸时
    if($imgWidth > $maxSize or $imgHeight > $maxSize){
      $size = image_min_width($imgWidth,$imgHeight,$maxSize);
      $image->thumb($size['width'], $size['height']);
      $do = true;
      $dowater = true;
    }
  // 裁剪固定尺寸
  }else if($width and $height){
    $size = image_min_width($imgWidth,$imgHeight,$width);
    $image->thumb($size['width'], $size['height'])->crop($width, $height);
    $do = true;
    $dowater = true;
  }
  if($dowater and $water and $word){
    $image->text($word,'./Public/images/arial.ttf',20,'#dddddd', \Think\Image::IMAGE_WATER_SOUTHEAST,-10);
  }
  // 未操作则不保存
  if($do){
    $image->save($path);
  }
}

PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:

在线图片转换BASE64工具:
http://tools.jb51.net/transcoding/img2base64

ICO图标在线生成工具:
http://tools.jb51.net/aideddesign/ico_img

在线Email邮箱图标制作工具:
http://tools.jb51.net/email/emaillogo

在线图片格式转换(jpg/bmp/gif/png)工具:
http://tools.jb51.net/aideddesign/picext

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》、《smarty模板入门基础教程》及《PHP模板技术总结》。

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

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

用php实现像JSP,ASP里Application那样的全局变量

用php实现像JSP,ASP里Application那样的全局变量
收藏 0 赞 0 分享

自动分页的不完整解决方案

自动分页的不完整解决方案
收藏 0 赞 0 分享

FCKeditor的安装(PHP)

FCKeditor的安装(PHP)
收藏 0 赞 0 分享

mysql5详细安装教程

mysql5详细安装教程
收藏 0 赞 0 分享

isset和empty的区别

isset和empty的区别
收藏 0 赞 0 分享

php5.2时间相差8小时

php5.2时间相差8小时
收藏 0 赞 0 分享

安装APACHE

安装APACHE
收藏 0 赞 0 分享

PHP5 安装方法

PHP5 安装方法
收藏 0 赞 0 分享

PHP has encountered an Access Violation

PHP has encountered an Access Violation
收藏 0 赞 0 分享

MYSQL环境变量设置方法

本文介绍了mysql数据库中环境变量的设置方法,如何设置mysql数据库的环境变量,有需要的朋友参考下
收藏 0 赞 0 分享
查看更多