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 相关文章推荐
一个简单的MySQL数据浏览器
Oct 09 PHP
php 常用类汇总 推荐收藏
May 13 PHP
php自定义函数之递归删除文件及目录
Aug 08 PHP
深入PHP中的HashTable结构详解
Jun 13 PHP
php生成随机颜色方法汇总
Dec 03 PHP
WordPress中用于获取文章作者与分类信息的方法整理
Dec 17 PHP
PHP操作mysql数据库分表的方法
Jun 09 PHP
php+mysql+jquery实现简易的检索自动补全提示功能
Apr 15 PHP
YII2框架中excel表格导出的方法详解
Jul 21 PHP
php nginx 实时输出的简单实现方法
Jan 21 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
Mar 26 PHP
Laravel框架源码解析之反射的使用详解
May 14 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
从零开始的异世界生活:第二季延期后,B站上架了第二部剧场版
2020/05/06 日漫
php中文本数据翻页(留言本翻页)
2006/10/09 PHP
PHP使用PDO连接ACCESS数据库
2015/03/05 PHP
基于PHP实现用户注册登录功能
2016/10/14 PHP
深入解析Laravel5.5中的包自动发现Package Auto Discovery
2017/09/13 PHP
ThinkPHP实现转换数据库查询结果数据到对应类型的方法
2017/11/16 PHP
准确获得页面、窗口高度及宽度的JS
2006/11/26 Javascript
详解JavaScript中的4种类型识别方法
2015/09/14 Javascript
AngularJS全局scope与Isolate scope通信用法示例
2016/11/22 Javascript
jQuery ajax实现省市县三级联动
2021/03/07 Javascript
基于JavaScript实现验证码功能
2017/04/01 Javascript
详解webpack babel的配置
2018/01/09 Javascript
vue2.0+vue-dplayer实现hls播放的示例
2018/03/02 Javascript
vue src动态加载请求获取图片的方法
2018/10/17 Javascript
axios携带cookie配置详解(axios+koa)
2018/12/28 Javascript
解决 viewer.js 动态更新图片导致无法预览的问题
2019/05/14 Javascript
vue的滚动条插件实现代码
2019/09/07 Javascript
javascript使用正则表达式实现注册登入校验
2020/09/23 Javascript
原生js实现购物车
2020/09/23 Javascript
详解vue3.0 的 Composition API 的一种使用方法
2020/10/26 Javascript
Python读写Excel文件方法介绍
2014/11/22 Python
python获取外网ip地址的方法总结
2015/07/02 Python
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
2016/05/27 Python
Python实现简单过滤文本段的方法
2017/05/24 Python
Pycharm设置界面全黑的方法
2018/05/23 Python
使用 Django Highcharts 实现数据可视化过程解析
2019/07/31 Python
Python turtle库绘制菱形的3种方式小结
2019/11/23 Python
使用tensorflow实现矩阵分解方式
2020/02/07 Python
CSS3实现王者匹配时的粒子动画效果
2019/04/12 HTML / CSS
临床医学专业学生的自我评价分享
2013/11/21 职场文书
2014的自我评价
2014/01/13 职场文书
坚定理想信念心得体会
2014/03/11 职场文书
年度考核自我鉴定
2014/03/19 职场文书
保卫钓鱼岛口号
2014/06/20 职场文书
企业战略合作意向书
2015/05/08 职场文书
2019个人工作自我评价范文(3篇)
2019/09/19 职场文书