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边学边教》(04.编写简易的通讯录――视频教程1)
Dec 13 PHP
解析PHP处理换行符的问题 \r\n
Jun 13 PHP
基于xcache的配置与使用详解
Jun 18 PHP
php面向对象中static静态属性和静态方法的调用
Feb 08 PHP
php结合md5实现的加密解密方法
Jan 25 PHP
PHP实现四种基础排序算法的运行时间比较(推荐)
Aug 11 PHP
PHP7新增运算符用法实例分析
Sep 26 PHP
Laravel基础-关于引入公共文件的两种方式
Oct 18 PHP
Laravel框架实现即点即改功能的方法分析
Oct 31 PHP
PHP学习记录之常用的魔术常量详解
Dec 12 PHP
php多进程并发编程防止出现僵尸进程的方法分析
Feb 28 PHP
详解PHP中curl_multi并发的实现
Jun 08 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
paypal即时到账php实现代码
2010/11/28 PHP
PHP企业级应用之常见缓存技术篇
2011/01/27 PHP
php使用百度天气接口示例
2014/04/22 PHP
php导出中文内容excel文件类实例
2015/07/06 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
Laravel5.1 框架控制器基础用法实例分析
2020/01/04 PHP
上传的js验证(图片/文件的扩展名)
2013/04/25 Javascript
js从10种颜色中随机取色实现每次取出不同的颜色
2013/10/23 Javascript
Flex通过JS获取客户端IP和计算机名的实例代码
2013/11/21 Javascript
JS实现的倒计时效果实例(2则实例)
2015/12/23 Javascript
用JS写的一个Ajax库(实例代码)
2016/08/06 Javascript
JS继承之借用构造函数继承和组合继承
2016/09/07 Javascript
非常优秀的JS图片轮播插件Swiper的用法
2017/01/03 Javascript
JS点击缩略图整屏居中放大图片效果
2017/07/04 Javascript
vue.js框架实现表单排序和分页效果
2017/08/09 Javascript
Puppeteer环境搭建的详细步骤
2018/09/21 Javascript
判断文字超过2行添加展开按钮,未超过则不显示,溢出部分显示省略号
2019/04/28 Javascript
帮你彻底搞懂JS中的prototype、__proto__与constructor(图解)
2019/08/23 Javascript
Vue Render函数原理及代码实例解析
2020/07/30 Javascript
使用wxpython实现的一个简单图片浏览器实例
2014/07/10 Python
python集合用法实例分析
2015/05/30 Python
python连接字符串的方法小结
2015/07/13 Python
Python输入二维数组方法
2018/04/13 Python
基于django2.2连oracle11g解决版本冲突的问题
2020/07/02 Python
利用SVG和CSS3来实现一个炫酷的边框动画
2015/07/22 HTML / CSS
德国柯吉澳趣味家居:Koziol
2017/08/24 全球购物
英国领先的运动营养品牌:Protein Dynamix
2018/01/02 全球购物
英国银首饰公司:e&e Jewellery
2021/02/11 全球购物
Java面试题:Java类的Main方法如果是Private将会怎么样
2016/08/18 面试题
现场施工员岗位职责
2014/03/10 职场文书
《画家乡》教学反思
2014/04/22 职场文书
好习惯伴我成长演讲稿
2014/05/21 职场文书
公司放假通知范文
2015/04/14 职场文书
2016十一国庆节慰问信
2015/12/01 职场文书
Python Pandas数据分析之iloc和loc的用法详解
2021/11/11 Python
Java实现给Word文件添加文字水印
2022/02/15 Java/Android