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简单系统查询模块代码打包下载
Jun 07 PHP
PHP PDO函数库详解
Apr 27 PHP
体育彩票排列三组选三算法分享
Mar 07 PHP
PHP中file_get_contents高?用法实例
Sep 24 PHP
php输出金字塔的2种实现方法
Dec 16 PHP
Zend Guard使用指南及问题处理
Jan 07 PHP
将FCKeditor导入PHP+SMARTY的实现方法
Jan 15 PHP
php获取指定范围内最接近数的方法
Jun 02 PHP
ThinkPHP表单数据智能写入create方法实例分析
Sep 27 PHP
thinkphp项目部署到Linux服务器上报错“模板不存在”如何解决
Apr 27 PHP
thinkphp中的url跳转用法分析
Jul 12 PHP
PHP随机数函数rand()与mt_rand()的讲解
Mar 25 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+mysqli数据库连接的两种方式
2015/01/28 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
js常用函数 不错
2006/09/08 Javascript
JavaScript 序列化对象实现代码
2009/12/18 Javascript
超轻量级的基于jquery的三级展开列表
2011/04/26 Javascript
用IE重起计算机或者关机的示例代码
2014/03/10 Javascript
JavaScript支持的最大递归调用次数分析
2014/06/24 Javascript
JavaScript DOM事件(笔记)
2015/04/08 Javascript
javascript中call apply 的应用场景
2015/04/16 Javascript
javascript实现网页中涉及的简易运动(改变宽高、透明度、位置)
2015/11/29 Javascript
JavaScript实现图片自动加载的瀑布流效果
2016/04/11 Javascript
JavaScript中的ParseInt(&quot;08&quot;)和“09”返回0的原因分析及解决办法
2016/05/19 Javascript
jQuery插件FusionCharts实现的3D柱状图效果实例【附demo源码下载】
2017/03/03 Javascript
Vue使用vue-cli创建项目
2017/09/01 Javascript
Chrome调试折腾记之JS断点调试技巧
2017/09/11 Javascript
jQuery实现右侧抽屉式在线客服功能
2017/12/25 jQuery
JavaScript面向对象核心知识与概念归纳整理
2020/05/09 Javascript
前端使用crypto.js进行加密的函数代码
2020/08/16 Javascript
python基础教程之面向对象的一些概念
2014/08/29 Python
利用python程序生成word和PDF文档的方法
2017/02/14 Python
Python常见异常分类与处理方法
2017/06/04 Python
对Python 2.7 pandas 中的read_excel详解
2018/05/04 Python
Python使用win32com模块实现数据库表结构自动生成word表格的方法
2018/07/17 Python
PyCharm鼠标右键不显示Run unittest的解决方法
2018/11/30 Python
对python 读取线的shp文件实例详解
2018/12/22 Python
Python使用POP3和SMTP协议收发邮件的示例代码
2019/04/16 Python
Python描述数据结构学习之哈夫曼树篇
2020/09/07 Python
工作自我评价分享
2013/12/01 职场文书
高中军训感想800字
2014/02/23 职场文书
关爱留守儿童标语
2014/06/18 职场文书
2014财务年度工作总结
2014/11/11 职场文书
初三毕业评语
2014/12/26 职场文书
2015年打非治违工作总结
2015/04/02 职场文书
Python自然语言处理之切分算法详解
2021/04/25 Python
Python实现byte转integer
2021/06/03 Python
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
2022/04/29 Servers