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 相关文章推荐
在PHP中养成7个面向对象的好习惯
Jan 28 PHP
PHP网站备份程序代码分享
Jun 10 PHP
ThinkPHP与PHPExcel冲突解决方法
Aug 08 PHP
php打开远程文件的方法和风险及解决方法
Nov 12 PHP
PHP使用pear自带的mail类库发邮件的方法
Jul 08 PHP
PHP扩展迁移为PHP7扩展兼容性问题记录
Feb 15 PHP
php中array_unshift()修改数组key注意事项分析
May 16 PHP
利用php操作memcache缓存的基础方法示例
Aug 02 PHP
Ajax+PHP实现的删除数据功能示例
Feb 12 PHP
PHP封装的mysqli数据库操作类示例
Feb 16 PHP
关于laravel 数据库迁移中integer类型是无法指定长度的问题
Oct 09 PHP
laravel 获取当前url的别名方法
Oct 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
使用PHPMYADMIN操作mysql数据库添加新用户和数据库的方法
2010/04/02 PHP
通过5个php实例细致说明传值与传引用的区别
2012/08/08 PHP
通过JS 获取Mouse Position(鼠标坐标)的代码
2009/09/21 Javascript
jquery 子窗口操作父窗口的代码
2009/09/21 Javascript
使用apply方法处理数组的三个技巧[译]
2012/09/20 Javascript
JS中toFixed()方法引起的问题如何解决
2012/11/20 Javascript
setInterval()和setTimeout()的用法和区别示例介绍
2013/11/17 Javascript
解析Javascript中大括号“{}”的多义性
2013/12/02 Javascript
jquery实现更改表格行顺序示例
2014/04/30 Javascript
NodeJS学习笔记之(Url,QueryString,Path)模块
2015/01/13 NodeJs
jQuery构造函数init参数分析续
2015/05/13 Javascript
jQuery模拟select实现下拉菜单功能
2016/06/20 Javascript
node学习记录之搭建web服务器教程
2017/02/16 Javascript
搭建element-ui的Vue前端工程操作实例
2018/02/23 Javascript
webpack4 css打包压缩问题的解决
2018/05/18 Javascript
vue2.x集成百度UEditor富文本编辑器的方法
2018/09/21 Javascript
30分钟用Node.js构建一个API服务器的步骤详解
2019/05/24 Javascript
Nautil 中使用双向数据绑定的实现
2019/10/02 Javascript
Vue混入mixins滚动触底的方法
2019/11/22 Javascript
js将日期格式转换为YYYY-MM-DD HH:MM:SS
2020/09/18 Javascript
让Python代码更快运行的5种方法
2015/06/21 Python
Python matplotlib 画图窗口显示到gui或者控制台的实例
2018/05/24 Python
解决新django中的path不能使用正则表达式的问题
2018/12/18 Python
Tensorflow实现酸奶销量预测分析
2019/07/19 Python
Django 开发环境与生产环境的区分详解
2019/07/26 Python
python模块hashlib(加密服务)知识点讲解
2019/11/25 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
2020/05/22 Python
美国最大的在线水培用品商店:GrowersHouse.com
2018/08/14 全球购物
劳资人员岗位职责
2013/12/19 职场文书
房地产资料员岗位职责
2014/07/02 职场文书
乡镇保密工作责任书
2014/07/28 职场文书
2015年管理人员工作总结
2015/05/13 职场文书
如何用python插入独创性声明
2021/03/31 Python
Python Django ORM连表正反操作技巧
2021/06/13 Python
【2·13】一图读懂中国无线电发展
2022/02/18 无线电
分享Python获取本机IP地址的几种方法
2022/03/17 Python