php等比例缩放图片及剪切图片代码分享

 更新时间:2016年02月13日 10:18:25   投稿:hebedich  
这篇文章给大家分享的是使用php实现的等比例缩放图片及剪切图片的代码,非常的简单实用,附上用法,有需要的小伙伴可以参考下。

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获取汉字首字母并且分组排序详解

    这篇文章主要给大家介绍了关于如何利用PHP获取汉字首字母并且分组排序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用php具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • PHP 实现字符串翻转(包含中文汉字)的实现代码

    PHP 实现字符串翻转(包含中文汉字)的实现代码

    本篇文章主要介绍了PHP 实现字符串翻转(包含中文汉字)的实现代码,在PHP面试题中经常遇到,有兴趣的可以了解一下。
    2017-04-04
  • Symfony生成二维码的方法

    Symfony生成二维码的方法

    这篇文章主要介绍了Symfony生成二维码的方法,实例分析了采用google开放api和PHP类库phpqrcode两种方法,并结合实例详细说明了Symfony下使用EndroidQrCodeBundle生成二维码的具体步骤与实现方法,需要的朋友可以参考下
    2016-02-02
  • WordPress后台中实现图片上传功能的实例讲解

    WordPress后台中实现图片上传功能的实例讲解

    这篇文章主要介绍了WordPress后台中实现图片上传功能的实例讲解,包括多个图片上传表单功能的实现,需要的朋友可以参考下
    2016-01-01
  • php微信开发之音乐回复功能

    php微信开发之音乐回复功能

    这篇文章主要为大家详细介绍了php微信开发之音乐回复功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • 升级 PHP7.1 后 openssl 解密 mcrypt AES 数据不兼容问题的处理方法

    升级 PHP7.1 后 openssl 解密 mcrypt AES 数据不兼容问题的处理方法

    这篇文章主要介绍了升级 PHP7.1 后 openssl 解密 mcrypt AES 数据不兼容问题的处理方法,需要的朋友可以参考下
    2018-07-07
  • PHPMYADMIN 简明安装教程 推荐

    PHPMYADMIN 简明安装教程 推荐

    简单的说,phpmyadmin就是一种mysql的管理工具,安装该工具后,即可以通过web形式直接管理mysql数据,而不需要通过执行系统命令来管理,非常适合对数据库操作命令不熟悉的数据库管理者,下面我就说下怎么安装该工具
    2010-03-03
  • Yii安装与使用Excel扩展的方法

    Yii安装与使用Excel扩展的方法

    这篇文章主要介绍了Yii安装与使用Excel扩展的方法,简单分析了Yii中Excel扩展的下载、安装及相关使用技巧,需要的朋友可以参考下
    2016-07-07
  • php权限调整强制用户退出的解决步骤

    php权限调整强制用户退出的解决步骤

    这篇文章主要介绍了php权限调整强制用户退出的解决步骤,当用户登录时,将用户的登录状态和其他相关信息存储在服务器端,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-09-09
  • yii2简单使用less代替css示例

    yii2简单使用less代替css示例

    本篇文章主要介绍了yii2简单使用less代替css示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03

最新评论