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 危险函数解释 分析
Apr 22 PHP
PHP 服务器配置(使用Apache及IIS两种方法)
Jun 01 PHP
用php或asp创建网页桌面快捷方式的代码
Mar 23 PHP
11个PHP 分页脚本推荐
Aug 15 PHP
基于php权限分配的实现代码
Apr 28 PHP
php的POSIX 函数以及进程测试的深入分析
Jun 03 PHP
ThinkPHP模板自定义标签使用方法
Jun 26 PHP
php CI框架插入一条或多条sql记录示例
Jul 29 PHP
PHP.ini安全配置检测工具pcc简单介绍
Jul 02 PHP
PHP 以POST方式提交XML、获取XML,解析XML详解及实例
Oct 26 PHP
ThinkPHP框架分布式数据库连接方法详解
Mar 14 PHP
thinkPHP中钩子的使用方法实例分析
Nov 16 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
初次接触php抽象工厂模式(Elgg)
2010/03/21 PHP
浅谈PHP调用Webservice思路及源码分享
2014/06/04 PHP
PHP中exec与system用法区别分析
2014/09/22 PHP
PHP 实现代码复用的一个方法 traits新特性
2015/02/22 PHP
Laravel 模型关联基础教程详解
2019/09/17 PHP
用JavaScript页面不刷新时全选择,全删除(GridView)
2009/04/14 Javascript
IE FF OPERA都可用的弹出层实现代码
2009/09/29 Javascript
js仿百度有啊通栏展示效果实现代码
2013/05/28 Javascript
JavaScript中使用Math.PI圆周率属性的方法
2015/06/14 Javascript
Jquery Mobile 自定义按钮图标
2015/11/18 Javascript
js实现div在页面拖动效果
2016/05/04 Javascript
将form表单通过ajax实现无刷新提交的简单实例
2016/10/12 Javascript
浅谈javascript中的 “ &amp;&amp; ” 和 “ || ”
2017/02/02 Javascript
JS实现向iframe中表单传值的方法
2017/03/24 Javascript
基于JavaScript实现图片连播和联级菜单实例代码
2017/07/28 Javascript
用vue2.0实现点击选中active其他选项互斥的效果
2018/04/12 Javascript
Vue中的字符串模板的使用
2018/05/17 Javascript
详解如何构建Promise队列实现异步函数顺序执行
2018/10/23 Javascript
JavaScript实现图片放大镜效果
2019/06/27 Javascript
jQuery设置下拉框显示与隐藏效果的方法分析
2019/09/15 jQuery
使用Python编写提取日志中的中文的脚本的方法
2015/04/30 Python
python thrift搭建服务端和客户端测试程序
2018/01/17 Python
python里 super类的工作原理详解
2019/06/19 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
Python如何实现后端自定义认证并实现多条件登陆
2020/06/22 Python
解析HTML5的存储功能和web SQL的相关操作方法
2016/02/19 HTML / CSS
纯HTML5+CSS3制作生日蛋糕(代码易懂)
2016/11/16 HTML / CSS
机关节能减排实施方案
2014/03/17 职场文书
服务型党组织建设典型材料
2014/05/07 职场文书
保护水资源的标语
2014/06/17 职场文书
计划生育责任书
2015/05/09 职场文书
毕业设计答辩开场白
2015/05/29 职场文书
演讲比赛通讯稿
2015/07/18 职场文书
浅谈MySQL表空间回收的正确姿势
2021/10/05 MySQL
前端实现滑动按钮AJAX与后端交互的示例代码
2022/02/24 Javascript
Nginx工作模式及代理配置的使用细节
2022/03/21 Servers