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 相关文章推荐
PHP5 面向对象(学习记录)
Dec 02 PHP
Notice: Undefined index: page in E:\PHP\test.php on line 14
Nov 02 PHP
php数据入库前清理 注意php intval与mysql的int取值范围不同
Dec 12 PHP
3个PHP多维数组转为一维数组的方法实例
Mar 13 PHP
PHP高级编程实例:编写守护进程
Sep 02 PHP
ThinkPHP实现动态包含文件的方法
Nov 29 PHP
php使用递归计算文件夹大小
Dec 24 PHP
php验证手机号码
Nov 11 PHP
PHP函数import_request_variables()用法分析
Apr 02 PHP
详解PHP使用日期时间处理器Carbon人性化显示时间
Aug 10 PHP
PDO::prepare讲解
Jan 29 PHP
windows系统php环境安装swoole具体步骤
Mar 04 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
ASP知识讲座四
2006/10/09 PHP
php+js实现异步图片上传实例分享
2014/06/02 PHP
php+xml实现在线英文词典之添加词条的方法
2015/01/23 PHP
PHP入门教程之数学运算技巧总结
2016/09/11 PHP
用js实现的检测浏览器和系统的函数
2009/04/09 Javascript
讨论javascript(一)工厂方式 js面象对象的定义方法
2009/12/15 Javascript
Extjs学习笔记之一 初识Extjs之MessageBox
2010/01/07 Javascript
IE6下CSS图片缓存问题解决方法
2010/12/09 Javascript
js登录弹出层特效
2014/03/07 Javascript
三种检测iPhone/iPad设备方向的方法
2014/04/23 Javascript
借助JavaScript脚本判断浏览器Flash Player信息的方法
2014/07/09 Javascript
js获取UserControl内容为拼html时提供方便
2014/11/02 Javascript
javascript实现随机显示星星特效
2016/01/28 Javascript
微信小程序 开发之滑块视图容器(swiper)详解及实例代码
2017/02/22 Javascript
详解Vue使用命令行搭建单页面应用
2017/05/24 Javascript
vue中的非父子间的通讯问题简单的实例代码
2017/07/19 Javascript
简单快速的实现js计算器功能
2017/08/17 Javascript
详解.vue文件中监听input输入事件(oninput)
2017/09/19 Javascript
Vue核心概念Getter的使用方法
2019/01/18 Javascript
9102了,你还不会移动端真机调试吗
2019/03/25 Javascript
angular 服务随记小结
2019/05/06 Javascript
layui递归实现动态左侧菜单
2019/07/26 Javascript
react基本安装与测试示例
2020/04/27 Javascript
python使用post提交数据到远程url的方法
2015/04/29 Python
python计算无向图节点度的实例代码
2019/11/22 Python
为什么称python为胶水语言
2020/06/16 Python
阿里云:Aliyun.com
2017/02/15 全球购物
中药专业自荐信范文
2014/03/18 职场文书
爱情保证书大全
2014/04/29 职场文书
社会实践先进工作者事迹材料
2014/05/06 职场文书
环境保护与污染治理求职信
2014/07/16 职场文书
上班迟到检讨书300字
2014/10/18 职场文书
2014年文员工作总结
2014/11/18 职场文书
班主任2015新年寄语
2014/12/08 职场文书
2014年学生管理工作总结
2014/12/20 职场文书
大学学生会竞选稿
2015/11/19 职场文书