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生成SessionID和图片校验码的思路和实现代码
Mar 10 PHP
探讨PHP函数ip2long转换IP时数值太大产生负数的解决方法
Jun 06 PHP
深入PHP许愿墙模块功能分析
Jun 25 PHP
php去除HTML标签实例
Nov 06 PHP
php使用Jpgraph绘制简单X-Y坐标图的方法
Jun 10 PHP
PHP实现简单实用的分页类代码
Apr 08 PHP
PHP使用栈解决约瑟夫环问题算法示例
Aug 27 PHP
Laravel框架集成UEditor编辑器的方法图文与实例详解
Apr 17 PHP
php使用curl伪造浏览器访问操作示例
Sep 30 PHP
基于laravel-admin 后台 列表标签背景的使用方法
Oct 03 PHP
在laravel5.2中实现点击用户头像更改头像的方法
Oct 14 PHP
php + ajax 实现的写入数据库操作简单示例
May 16 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
关于拼配咖啡,你要知道
2021/03/03 咖啡文化
PHP 和 MySQL 开发的 8 个技巧
2006/10/09 PHP
模拟OICQ的实现思路和核心程序(一)
2006/10/09 PHP
基于php下载文件的详解
2013/06/02 PHP
ThinkPHP令牌验证实例
2014/06/18 PHP
几个实用的PHP内置函数使用指南
2014/11/27 PHP
CodeIgniter钩子用法实例详解
2016/01/20 PHP
功能强大的PHP POST提交数据类
2016/07/15 PHP
thinkPHP5 ACL用户权限模块用法详解
2017/05/10 PHP
Laravel中的Blade模板引擎示例详解
2017/10/10 PHP
JavaScript 动态添加表格行 使用模板、标记
2009/10/24 Javascript
理解Javascript_09_Function与Object
2010/10/16 Javascript
jQuery+CSS 实现随滚动条增减的汽水瓶中的液体效果
2011/09/26 Javascript
多个checkbox被选中时如何判断是否有自己想要的
2014/09/22 Javascript
JS实现从连接中获取youtube的key实例
2015/07/02 Javascript
js窗口关闭提示信息(兼容IE和firefox)
2015/10/23 Javascript
BootStrap组件之进度条的基本用法
2017/01/19 Javascript
Vue Spa切换页面时更改标题的实例代码
2017/07/15 Javascript
vue axios请求拦截实例代码
2018/03/29 Javascript
js+css实现红包雨效果
2018/07/12 Javascript
微信小程序学习笔记之本地数据缓存功能详解
2019/03/29 Javascript
Element的el-tree控件后台数据结构的生成以及方法的抽取
2020/03/05 Javascript
微信小程序去除左上角返回键的实现方法
2020/03/06 Javascript
Python爬虫DNS解析缓存方法实例分析
2017/06/02 Python
对Python中的@classmethod用法详解
2018/04/21 Python
Python中py文件转换成exe可执行文件的方法
2019/06/14 Python
谈一谈数组拼接tf.concat()和np.concatenate()的区别
2020/02/07 Python
Python基于类路径字符串获取静态属性
2020/03/12 Python
Python图像阈值化处理及算法比对实例解析
2020/06/19 Python
Turnbull & Asser官网:英国皇室御用的顶级定制衬衫
2019/01/31 全球购物
Speedo速比涛德国官方网站:世界领先的泳装品牌
2019/08/26 全球购物
编辑硕士自荐信范文
2013/11/27 职场文书
文明城市标语
2014/06/16 职场文书
小学生志愿者活动方案
2014/08/23 职场文书
2014最新离职证明范本
2014/09/12 职场文书
八项规定自查自纠报告及整改措施
2014/10/26 职场文书