PHP实现图片不变型裁剪及图片按比例裁剪的方法


Posted in PHP onJanuary 14, 2016

本文实例讲述了PHP实现图片不变型裁剪及图片按比例裁剪的方法。分享给大家供大家参考,具体如下:

图片不变型裁剪

<?php
/**
 * imageCropper
 * @param string $source_path
 * @param string $target_width
 * @param string $target_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){
    // image-to-height
    $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){
    //image-to-widht
    $cropped_width = $source_height / $target_ratio;
    $cropped_height = $source_height;
    $source_x = ($source_width - $cropped_width) / 2;
    $source_y = 0;
  }else{
    //image-size-ok
    $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 ;
      break;
  }
  $target_image = imagecreatetruecolor($target_width, $target_height);
  $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
  // copy
  imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
  // zoom
  imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
  header('Content-Type: image/jpeg');
  imagejpeg($target_image);
  imagedestroy($source_image);
  imagedestroy($target_image);
  imagedestroy($cropped_image);
}
$filename = "8fcb7a0831b79c61.jpg";
imageCropper($filename,200,200);
?>

图片按比例裁剪

<?php
/**
 * imageZoom
 * @param string $file
 * @param double $zoom
 */
function imageZoom($filename,$zoom=0.6){
  //baseinfo
  $sourceImageInfo = getimagesize($filename);
  $sourceWidth = $sourceImageInfo[0];
  $sourceHeight = $sourceImageInfo[1];
  $sourceMine = $sourceImageInfo['mime'];
  $sourceRatio = $sourceWidth/$sourceHeight;
  $sourceX = 0;
  $sourceY = 0;
  //zoom
  $targetRatio = $zoom;
  //target-widht-height
  $targetWidth = $sourceWidth*$targetRatio;
  $targetHeight = $sourceHeight*$targetRatio;
  //init-params
  $sourceImage = null;
  switch($sourceMine){
    case 'image/gif':
      $sourceImage = imagecreatefromgif($filename);
      break;
    case 'image/jpeg':
      $sourceImage = imagecreatefromjpeg($filename);
      break;
    case 'image/png':
      $sourceImage = imagecreatefrompng($filename);
      break;
    default:
      return ;
      break;
  }
  //temp-target-image
  $tempSourceImage = imagecreatetruecolor($sourceWidth, $sourceHeight);
  $targetImage = imagecreatetruecolor($targetWidth,$targetHeight);
  //copy
  imagecopy($tempSourceImage, $sourceImage, 0, 0, $sourceX, $sourceY, $sourceWidth, $sourceHeight);
  //zoom
  imagecopyresampled($targetImage, $tempSourceImage, 0, 0, 0, 0, $targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
  //header
  header('Content-Type: image/jpeg');
  //image-loading
  imagejpeg($targetImage);
  //destroy
  imagedestroy($tempSourceImage);
  imagedestroy($sourceImage);
  imagedestroy($targetImage);
}
$filename = "8fcb7a0831b79c61.jpg";
imageZoom($filename);
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
用PHP 4.2书写安全的脚本
Oct 09 PHP
透析PHP的配置文件php.ini
Oct 09 PHP
默默小谈PHP&amp;MYSQL分页原理及实现
Jan 02 PHP
php下MYSQL limit的优化
Jan 10 PHP
PHP实现下载功能的代码
Sep 29 PHP
php 批量替换html标签的实例代码
Nov 26 PHP
CI框架Session.php源码分析
Nov 03 PHP
PHP数组的定义、初始化和数组元素的显示实现代码
Nov 05 PHP
PHP实现蛇形矩阵,回环矩阵及数字螺旋矩阵的方法分析
May 29 PHP
PHP文件类型检查及fileinfo模块安装使用详解
May 09 PHP
php操作redis常见方法示例【key与value操作】
Apr 14 PHP
Thinkphp5框架中引入Markdown编辑器操作示例
Jun 03 PHP
详解HTTP Cookie状态管理机制
Jan 14 #PHP
在php中设置session用memcache来存储的方法总结
Jan 14 #PHP
thinkphp实现图片上传功能
Jan 13 #PHP
PHP实现伪静态方法汇总
Jan 13 #PHP
微信公众号支付之坑:调用支付jsapi缺少参数 timeStamp等错误解决方法
Jan 12 #PHP
优化WordPress中文章与评论的时间显示
Jan 12 #PHP
win平台安装配置Nginx+php+mysql 环境
Jan 12 #PHP
You might like
用缓存实现静态页面的测试
2006/12/06 PHP
php检测文件编码的方法示例
2014/04/25 PHP
Centos6.5和Centos7 php环境搭建方法
2016/05/27 PHP
Apache站点配置SSL强制跳转443
2021/03/09 Servers
js获取变量
2006/08/24 Javascript
var与Javascript变量隐式声明
2009/09/17 Javascript
js注意img图片的onerror事件的分析
2011/01/01 Javascript
将list转换为json失败的原因
2013/12/17 Javascript
DOM节点深度克隆函数cloneNode()用法实例
2015/01/12 Javascript
jquery使用slideDown实现模块缓慢拉出效果的方法
2015/03/27 Javascript
JavaScript定义函数的三种实现方法
2017/09/23 Javascript
JavaScript callback回调函数用法实例分析
2018/05/08 Javascript
解决layui 复选框等内置控件不显示的问题
2018/08/14 Javascript
JavaScript学习笔记之图片库案例分析
2019/01/08 Javascript
详解如何更好的使用module vuex
2019/03/27 Javascript
vue-cli webpack配置文件分析
2019/05/20 Javascript
JavaScript判断浏览器运行环境的详细方法
2019/06/30 Javascript
jQuery实现的图片点击放大缩小功能案例
2020/01/02 jQuery
vue页面引入three.js实现3d动画场景操作
2020/08/10 Javascript
vue3.0 加载json的方法(非ajax)
2020/10/26 Javascript
Python注释详解
2016/06/01 Python
Python编写登陆接口的方法
2017/07/10 Python
python logging日志模块以及多进程日志详解
2018/04/18 Python
Python爬虫包BeautifulSoup异常处理(二)
2018/06/17 Python
英国网上购买门:Direct Doors
2018/06/07 全球购物
美国在线眼镜店:GlassesShop
2018/11/15 全球购物
Daisy London官网:英国最大的首饰集团IBB旗下
2019/02/28 全球购物
美国折扣香水网站:The Perfume Spot
2020/12/12 全球购物
几道Web/Ajax的面试题
2016/11/05 面试题
五一家具促销方案
2014/01/10 职场文书
大学生职业生涯规划书
2014/03/14 职场文书
读后感作文评语
2014/12/25 职场文书
军训决心书范文
2015/09/22 职场文书
《普罗米修斯》教学反思
2016/02/22 职场文书
管理者们如何制定2019年的工作计划?
2019/07/01 职场文书
微信小程序和php的登录实现
2021/04/01 PHP