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 相关文章推荐
第十五节--Zend引擎的发展
Nov 16 PHP
php用数组返回无限分类的列表数据的代码
Aug 08 PHP
PHP获取文件绝对路径的代码(上一级目录)
May 29 PHP
php设计模式 DAO(数据访问对象模式)
Jun 26 PHP
PHP多线程之内部多线程实例分析
Mar 09 PHP
php生成固定长度纯数字编码的方法
Jul 09 PHP
PHP使用pear实现mail发送功能 windows环境下配置pear
Apr 15 PHP
PHP图片加水印实现方法
May 06 PHP
php微信公众平台开发之微信群发信息
Sep 13 PHP
浅析php-fpm静态和动态执行方式的比较
Nov 09 PHP
laravel实现分页样式替换示例代码(增加首、尾页)
Sep 22 PHP
微信小程序结合ThinkPHP5授权登陆后获取手机号
Nov 23 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
什么是调频(FM)、调幅(AM)、短波(SW)、长波(LW)
2021/03/01 无线电
Windows IIS PHP 5.2 安装与配置方法
2009/06/08 PHP
JS与PHP向函数传递可变参数的区别实例代码
2011/05/18 PHP
php5.3中连接sqlserver2000的两种方法(com与ODBC)
2012/12/29 PHP
迅速确定php多维数组的深度的方法
2014/01/07 PHP
PHP实现冒泡排序的简单实例
2016/05/26 PHP
PHP实现微信退款的方法示例
2019/03/26 PHP
Laravel5.0+框架邮件发送功能实现方法图文与实例详解
2019/04/23 PHP
Thinkphp整合阿里云OSS图片上传实例代码
2019/04/28 PHP
PHP连续签到功能实现方法详解
2019/12/04 PHP
Javascript表格翻页效果的具体实现
2013/10/05 Javascript
node.js中的fs.stat方法使用说明
2014/12/16 Javascript
js实现带农历和八字等信息的日历特效
2016/05/16 Javascript
JS实现iframe编辑器光标位置插入内容的方法(兼容IE和Firefox)
2016/06/24 Javascript
JS for...in 遍历语句用法实例分析
2016/08/24 Javascript
JS返回页面时自动回滚到历史浏览位置
2018/09/26 Javascript
Vue项目路由刷新的实现代码
2019/04/17 Javascript
微信小程序实现的绘制table表格功能示例
2019/04/26 Javascript
[02:53]2018年度DOTA2最佳战队-完美盛典
2018/12/17 DOTA
python之matplotlib学习绘制动态更新图实例代码
2018/01/23 Python
Python静态类型检查新工具之pyright 使用指南
2019/04/26 Python
基于python实现学生信息管理系统
2019/11/22 Python
Django密码存储策略分析
2020/01/09 Python
Python定义函数实现累计求和操作
2020/05/03 Python
Python Sqlalchemy如何实现select for update
2020/10/12 Python
python字符串拼接+和join的区别详解
2020/12/03 Python
CSS3实现可关闭的下拉手风琴菜单效果
2015/08/31 HTML / CSS
详解Html5页面实现下载文件(apk、txt等)的三种方式
2018/10/22 HTML / CSS
保密工作责任书
2014/04/16 职场文书
2014年个人年终总结
2015/03/09 职场文书
教师“一帮一”结对子活动总结
2015/05/07 职场文书
学校教学管理制度
2015/08/06 职场文书
检讨书范文
2019/04/16 职场文书
2019个人工作计划书的格式及范文!
2019/07/04 职场文书
千万级用户系统SQL调优实战分享
2022/03/03 MySQL
你知道Java Spring的两种事务吗
2022/03/16 Java/Android