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 相关文章推荐
AJAX for PHP简单表数据查询实例
Jan 02 PHP
php中根据变量的类型 选择echo或dump
Jul 05 PHP
php创建基本身份认证站点的方法详解
Jun 08 PHP
php实现的用户查询类实例
Jun 18 PHP
PHP基于单例模式实现的数据库操作基类
Jan 15 PHP
php连接oracle数据库的方法(测试成功)
May 26 PHP
PHP strcmp()和strcasecmp()的区别实例
Nov 05 PHP
php基于dom实现的图书xml格式数据示例
Feb 03 PHP
Yii2学习笔记之汉化yii设置表单的描述(属性标签attributeLabels)
Feb 07 PHP
解决php 处理 form 表单提交多个 name 属性值相同的 input 标签问题
May 11 PHP
浅谈php的TS和NTS的区别
Mar 13 PHP
php7连接MySQL实现简易查询程序的方法
Oct 13 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获取谷歌PR值算法,附上php查询PR值代码示例
2011/12/25 PHP
基于AppServ,XAMPP,WAMP配置php.ini去掉警告信息(NOTICE)的方法详解
2013/05/07 PHP
phpize的深入理解
2013/06/03 PHP
CI(CodeIgniter)框架中URL特殊字符处理与SQL注入隐患分析
2019/02/28 PHP
比较详细的关于javascript 解析json的代码
2009/12/16 Javascript
event.srcElement 用法笔记e.target
2009/12/18 Javascript
JQuery中判断一个元素下面是否有内容或者有某个标签的判断代码
2012/02/02 Javascript
仿微博字符限制效果实现代码
2012/04/20 Javascript
jQuery调取jSon数据并展示的方法
2015/01/29 Javascript
使用Browserify配合jQuery进行编程的超级指南
2015/07/28 Javascript
AngularJS入门教程之服务(Service)
2016/07/27 Javascript
简单实现jQuery多选框功能
2017/01/09 Javascript
React创建组件的三种方式及其区别
2017/01/12 Javascript
JS实现汉字与Unicode码相互转换的方法详解
2017/04/28 Javascript
JS实现前端页面的搜索功能
2018/06/12 Javascript
关于vue-cli 3配置打包优化要点(推荐)
2019/04/22 Javascript
如何使用three.js 制作一个三维的推箱子游戏
2020/07/29 Javascript
python实现挑选出来100以内的质数
2015/03/24 Python
在Linux下使用Python的matplotlib绘制数据图的教程
2015/06/11 Python
python图书管理系统
2020/04/05 Python
python中返回矩阵的行列方法
2018/04/04 Python
python判断计算机是否有网络连接的实例
2018/12/15 Python
python实现文件的备份流程详解
2019/06/18 Python
Python二元赋值实用技巧解析
2019/10/25 Python
Jupyter notebook如何实现指定浏览器打开
2020/05/13 Python
Python+Opencv实现把图片、视频互转的示例
2020/12/17 Python
html2canvas截图空白问题的解决
2020/03/24 HTML / CSS
后勤主管工作职责
2013/12/07 职场文书
环境科学专业优秀毕业生自荐书
2014/02/03 职场文书
2014年三八妇女节活动方案
2014/02/28 职场文书
浪漫婚礼主持词
2014/03/14 职场文书
家长会主持词开场白
2014/03/18 职场文书
学生会竞选演讲稿学习部
2014/08/25 职场文书
2014年化工厂工作总结
2014/11/25 职场文书
2015暑期爱心支教策划书
2015/07/14 职场文书
证婚人婚礼致辞
2015/07/28 职场文书