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 相关文章推荐
在数据量大(超过10万)的情况下
Jan 15 PHP
php+mysql分页代码详解
Mar 27 PHP
40个迹象表明你还是PHP菜鸟
Sep 29 PHP
PHP中运用jQuery的Ajax跨域调用实现代码
Feb 21 PHP
PHP设计模式之装饰者模式
Feb 29 PHP
php gzip压缩输出的实现方法
Apr 27 PHP
PHP获取昨天、今天及明天日期的方法
Feb 03 PHP
php实现批量修改文件名称的方法
Jul 23 PHP
PHP7安装Redis扩展教程【Linux与Windows平台】
Sep 30 PHP
php 解析xml 的四种方法详细介绍
Oct 26 PHP
php生成微信红包数组的方法
Sep 05 PHP
PHP设计模式之组合模式定义与应用示例
Feb 01 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/04 无线电
php框架Phpbean说明
2008/01/10 PHP
PHP延迟静态绑定示例分享
2014/06/22 PHP
php快速排序原理与实现方法分析
2016/05/26 PHP
php mysql操作mysql_connect连接数据库实例详解
2016/12/26 PHP
php+redis在实际项目中HTTP 500: Internal Server Error故障排除
2017/02/05 PHP
php使用curl获取header检测开启GZip压缩的方法
2018/08/15 PHP
Apache站点配置SSL强制跳转443
2021/03/09 Servers
javascript 常用方法总结
2009/06/03 Javascript
JQuery AJAX提交中文乱码的解决方案
2010/07/02 Javascript
Javascript学习笔记-详解in运算符
2011/09/13 Javascript
仅IE支持clearAttributes/mergeAttributes方法使用介绍
2012/05/04 Javascript
JavaScript对表格或元素按文本,数字或日期排序的方法
2015/05/26 Javascript
js使用cookie记录用户名的方法
2015/11/26 Javascript
CSS中position属性之fixed实现div居中
2015/12/14 Javascript
js验证框架实现代码分享
2016/05/18 Javascript
BackBone及其实例探究_动力节点Java学院整理
2017/07/14 Javascript
通俗易懂地解释JS中的闭包
2017/10/23 Javascript
JS/jQuery实现DIV延时几秒后消失或显示的方法
2018/02/12 jQuery
详解Vue+axios+Node+express实现文件上传(用户头像上传)
2018/08/10 Javascript
在小程序Canvas中使用measureText的方法示例
2018/10/19 Javascript
VUE2.0+ElementUI2.0表格el-table实现表头扩展el-tooltip
2018/11/30 Javascript
推荐一个基于Node.js的表单验证库
2019/02/15 Javascript
Python中查看文件名和文件路径
2017/03/31 Python
基于Python实现的微信好友数据分析
2018/02/26 Python
Python高级特性与几种函数的讲解
2019/03/08 Python
python爬取盘搜的有效链接实现代码
2019/07/20 Python
详解利用python识别图片中的条码(pyzbar)及条码图片矫正和增强
2020/11/17 Python
设计师家具购买和委托在线市场:Viyet
2016/11/16 全球购物
缓解脚、腿和背部疼痛:Z-CoiL鞋
2019/03/12 全球购物
4s店机修工岗位职责
2013/12/20 职场文书
军训教官感言
2014/03/02 职场文书
2015高三毕业寄语赠言
2015/02/27 职场文书
2015年教研室工作总结范文
2015/05/23 职场文书
JavaScript使用canvas绘制坐标和线
2021/04/28 Javascript
JS实现扫雷项目总结
2021/05/19 Javascript