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 相关文章推荐
与数据库连接
Oct 09 PHP
PHP实现采集程序原理和简单示例代码
Mar 18 PHP
PHP-MySQL教程归纳总结
Jun 07 PHP
php生成SessionID和图片校验码的思路和实现代码
Mar 10 PHP
php版小黄鸡simsimi聊天机器人接口分享
Jan 26 PHP
php下Memcached入门实例解析
Jan 05 PHP
php写入数据到CSV文件的方法
Mar 14 PHP
thinkPHP框架对接支付宝即时到账接口回调操作示例
Nov 14 PHP
PHP获取当前日期及本周一是几月几号的方法
Mar 28 PHP
php之可变函数的实例详解
Sep 13 PHP
PHP实现的解汉诺塔问题算法示例
Aug 06 PHP
PHP DB 数据库连接类定义与用法示例
Mar 11 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
上海牌131型七灯四波段四喇叭一级收音机
2021/03/02 无线电
五个PHP程序员工具
2008/05/26 PHP
浅析Yii中使用RBAC的完全指南(用户角色权限控制)
2013/06/20 PHP
PHP之APC缓存详细介绍 apc模块安装
2014/01/13 PHP
PHP数组函数知识汇总
2016/05/12 PHP
再谈IE中Flash控件的自动激活 ObjectWrap
2007/03/09 Javascript
解析dom中的children对象数组元素firstChild,lastChild的使用
2013/07/10 Javascript
简单实用的反馈表单无刷新提交带验证
2013/11/15 Javascript
js浏览器html5表单验证
2016/10/17 Javascript
关于使用js算总价的问题
2017/06/23 Javascript
基于Axios 常用的请求方法别名(详解)
2018/03/13 Javascript
浅谈针对Vue相同路由不同参数的刷新问题
2018/09/29 Javascript
vue-router重定向和路由别名的使用讲解
2019/01/19 Javascript
vue 中固定导航栏的实例代码
2019/11/01 Javascript
js实现多图和单图上传显示
2019/12/18 Javascript
vue计算属性+vue中class与style绑定(推荐)
2020/03/30 Javascript
VUE table表格动态添加一列数据,新增的这些数据不可以编辑(v-model绑定的数据不能实时更新)
2020/04/03 Javascript
vue+ElementUI 关闭对话框清空验证,清除form表单的操作
2020/08/06 Javascript
详解Vue的组件中data选项为什么必须是函数
2020/08/17 Javascript
python打开文件并获取文件相关属性的方法
2015/04/23 Python
python使用xmlrpclib模块实现对百度google的ping功能
2015/06/02 Python
Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
2016/06/14 Python
解决python2.7 查询mysql时出现中文乱码
2016/10/09 Python
Python实现二分查找与bisect模块详解
2017/01/13 Python
Python 自动刷博客浏览量实例代码
2017/06/14 Python
python-opencv在有噪音的情况下提取图像的轮廓实例
2017/08/30 Python
浅谈Python peewee 使用经验
2017/10/20 Python
Python pandas DataFrame操作的实现代码
2019/06/21 Python
Python上下文管理器全实例详解
2019/11/12 Python
python sklearn包——混淆矩阵、分类报告等自动生成方式
2020/02/28 Python
python 使用递归的方式实现语义图片分割功能
2020/07/16 Python
两种CSS3伪类选择器详细介绍
2013/12/24 HTML / CSS
Ryderwear美国官网:澳大利亚高端健身训练装备品牌
2018/04/24 全球购物
法制宣传标语
2014/06/23 职场文书
纪检干部个人对照检查材料
2014/09/23 职场文书
MySQL中正则表达式(REGEXP)使用详解
2022/07/07 MySQL