php等比例缩放图片及剪切图片代码分享


Posted in PHP onFebruary 13, 2016

php等比例缩放图片及剪切图片代码分享

/**
 * 图片缩放函数(可设置高度固定,宽度固定或者最大宽高,支持gif/jpg/png三种类型)
 * Author : Specs
 *
 * @param string $source_path 源图片
 * @param int $target_width 目标宽度
 * @param int $target_height 目标高度
 * @param string $fixed_orig 锁定宽高(可选参数 width、height或者空值)
 * @return string
 */
function myImageResize($source_path, $target_width = 200, $target_height = 200, $fixed_orig = ''){
  $source_info = getimagesize($source_path);
  $source_width = $source_info[0];
  $source_height = $source_info[1];
  $source_mime = $source_info['mime'];
  $ratio_orig = $source_width / $source_height;
  if ($fixed_orig == 'width'){
    //宽度固定
    $target_height = $target_width / $ratio_orig;
  }elseif ($fixed_orig == 'height'){
    //高度固定
    $target_width = $target_height * $ratio_orig;
  }else{
    //最大宽或最大高
    if ($target_width / $target_height > $ratio_orig){
      $target_width = $target_height * $ratio_orig;
    }else{
      $target_height = $target_width / $ratio_orig;
    }
  }
  switch ($source_mime){
    case 'image/gif':
      $source_image = imagecreatefromgif($source_path);
      break;
    
    case 'image/jpeg':
      $source_image = imagecreatefromjpeg($source_path);
      break;
    
    case 'image/png':
      $source_image = imagecreatefrompng($source_path);
      break;
    
    default:
      return false;
      break;
  }
  $target_image = imagecreatetruecolor($target_width, $target_height);
  imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height);
  //header('Content-type: image/jpeg');
  $imgArr = explode('.', $source_path);
  $target_path = $imgArr[0] . '_new.' . $imgArr[1];
  imagejpeg($target_image, $target_path, 100);
}

用法:

  1. myImageResize($filename, 200, 200); //最大宽高
  2. myImageResize($filename, 200, 200, 'width'); //宽度固定
  3. myImageResize($filename, 200, 200, 'height'); //高度固定

剪切图片为固定大小:

function imagecropper($source_path, $target_width, $target_height){
  $source_info = getimagesize($source_path);
  $source_width = $source_info[0];
  $source_height = $source_info[1];
  $source_mime = $source_info['mime'];
  $source_ratio = $source_height / $source_width;
  $target_ratio = $target_height / $target_width;
  
  // 源图过高
  if ($source_ratio > $target_ratio){
    $cropped_width = $source_width;
    $cropped_height = $source_width * $target_ratio;
    $source_x = 0;
    $source_y = ($source_height - $cropped_height) / 2;
  }elseif ($source_ratio < $target_ratio){ // 源图过宽
    $cropped_width = $source_height / $target_ratio;
    $cropped_height = $source_height;
    $source_x = ($source_width - $cropped_width) / 2;
    $source_y = 0;
  }else{ // 源图适中
    $cropped_width = $source_width;
    $cropped_height = $source_height;
    $source_x = 0;
    $source_y = 0;
  }
  
  switch ($source_mime){
    case 'image/gif':
      $source_image = imagecreatefromgif($source_path);
      break;
    
    case 'image/jpeg':
      $source_image = imagecreatefromjpeg($source_path);
      break;
    
    case 'image/png':
      $source_image = imagecreatefrompng($source_path);
      break;
    
    default:
      return false;
      break;
  }
  
  $target_image = imagecreatetruecolor($target_width, $target_height);
  $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  
  // 裁剪
  imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  // 缩放
  imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  $dotpos = strrpos($source_path, '.');
  $imgName = substr($source_path, 0, $dotpos);
  $suffix = substr($source_path, $dotpos);
  $imgNew = $imgName . '_small' . $suffix;
  imagejpeg($target_image, $imgNew, 100);
  imagedestroy($source_image);
  imagedestroy($target_image);
  imagedestroy($cropped_image);
}
PHP 相关文章推荐
php4的session功能评述(三)
Oct 09 PHP
PHP笔记之:基于面向对象设计的详解
May 14 PHP
php中利用explode函数分割字符串到数组
Feb 08 PHP
通过curl模拟post和get方式提交的表单类
Apr 23 PHP
PHP函数eval()介绍和使用示例
Aug 20 PHP
php输入流php://input使用浅析
Sep 02 PHP
详细解读PHP的Yii框架中登陆功能的实现
Aug 21 PHP
php微信开发之批量生成带参数的二维码
Jun 26 PHP
php简单实现多维数组排序的方法
Sep 30 PHP
php禁用cookie后session设置方法分析
Oct 19 PHP
Yii2框架实现登录、退出及自动登录功能的方法详解
Oct 24 PHP
PHP利用Mysql锁解决高并发的方法
Sep 04 PHP
PHP信号量基本用法实例详解
Feb 12 #PHP
PHP消息队列用法实例分析
Feb 12 #PHP
PHP共享内存用法实例分析
Feb 12 #PHP
PHP连接MSSQL方法汇总
Feb 05 #PHP
Symfony2开发之控制器用法实例分析
Feb 05 #PHP
Symfony2实现在doctrine中内置数据的方法
Feb 05 #PHP
PHP MYSQL实现登陆和模糊查询两大功能
Feb 05 #PHP
You might like
php中static静态变量的使用方法详解
2010/06/04 PHP
PHP登录验证码的实现与使用方法
2016/07/07 PHP
javascript中处理时间戳为日期格式的方法
2014/01/02 Javascript
原生js和jquery中有关透明度设置的相关问题
2014/01/08 Javascript
jquery实现页面虚拟键盘特效
2015/08/08 Javascript
javascript与jquery中的this关键字用法实例分析
2015/12/24 Javascript
AngularGauge 属性解析详解
2016/09/06 Javascript
Jquery 整理元素选取、常用方法一览表
2016/11/26 Javascript
js CSS3实现卡牌旋转切换效果
2017/07/04 Javascript
ES7中利用Await减少回调嵌套的方法详解
2017/11/01 Javascript
为什么使用koa2搭建微信第三方公众平台的原因
2018/05/16 Javascript
详解微信小程序调起键盘性能优化
2018/07/24 Javascript
在vue中实现点击选择框阻止弹出层消失的方法
2018/09/15 Javascript
JQuery获取元素尺寸、位置及页面滚动事件应用示例
2019/05/14 jQuery
Vue源码分析之Vue实例初始化详解
2019/08/25 Javascript
JS使用H5实现图片预览功能
2019/09/30 Javascript
vue 实现购物车总价计算
2019/11/06 Javascript
vue新建项目并配置标准路由过程解析
2019/12/09 Javascript
Javascript ParentNode和ChildNode接口原理解析
2020/03/16 Javascript
vue制作抓娃娃机的示例代码
2020/04/17 Javascript
python实现自动发送邮件发送多人、群发、多附件的示例
2018/01/23 Python
python验证码识别教程之利用滴水算法分割图片
2018/06/05 Python
Python面向对象程序设计示例小结
2019/01/30 Python
实现CSS3中的border-radius(边框圆角)示例代码
2013/07/19 HTML / CSS
HTML5 canvas基本绘图之图形组合
2016/06/27 HTML / CSS
沙特阿拉伯家用电器和电子产品购物网站:Sheta and Saif
2020/04/03 全球购物
高级Java程序员面试要点
2013/08/02 面试题
网上书店创业计划书
2014/01/12 职场文书
仓库文员岗位职责
2014/04/06 职场文书
社区党建工作汇报材料
2014/08/14 职场文书
第二批党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
学校法制宣传日活动总结
2014/11/01 职场文书
辞职离别感言
2015/08/04 职场文书
《赵州桥》教学反思
2016/02/17 职场文书
带你了解Java中的ForkJoin
2022/04/28 Java/Android
python如何读取和存储dict()与.json格式文件
2022/06/25 Python