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 相关文章推荐
玩转虚拟域名◎+ .
Oct 09 PHP
PHP无限分类的类
Jan 02 PHP
php 301转向实现代码
Sep 18 PHP
php从数据库查询结果生成树形列表的方法
Apr 17 PHP
PHP中$_SERVER使用说明
Jul 05 PHP
PHP中创建和验证哈希的简单方法实探
Jul 06 PHP
PHP实现GIF图片验证码
Nov 04 PHP
使用php+swoole对client数据实时更新(一)
Jan 07 PHP
PHP curl 或 file_get_contents 获取需要授权页面的方法
May 05 PHP
PHP编程获取各个时间段具体时间的方法
May 26 PHP
PHP实现求两个字符串最长公共子串的方法示例
Nov 17 PHP
PHP常量及变量区别原理详解
Aug 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
单一index.php实现PHP任意层级文件夹遍历(Zjmainstay原创)
2012/07/31 PHP
php伪静态之APACHE篇
2014/06/02 PHP
php socket客户端及服务器端应用实例
2014/07/04 PHP
jq选项卡鼠标延迟的插件实例
2013/05/13 Javascript
Asp.Net alert弹出提示信息的几种方法总结
2014/01/29 Javascript
JS将制定内容复制到剪切板示例代码
2014/02/11 Javascript
JS实现往下不断流动网页背景的方法
2015/02/27 Javascript
JavaScript动态添加列的方法
2015/03/25 Javascript
JS跨域解决方案之使用CORS实现跨域
2016/04/14 Javascript
ES6中Proxy与Reflect实现重载(overload)的方法
2017/03/30 Javascript
JS实现为动态添加的元素增加事件功能示例【基于事件委托】
2018/03/21 Javascript
JavaScript面向对象的程序设计(犯迷糊的小羊)
2018/05/27 Javascript
详解vue axios二次封装
2018/07/22 Javascript
如何使用electron-builder及electron-updater给项目配置自动更新
2018/12/24 Javascript
vue+eslint+vscode配置教程
2019/08/09 Javascript
JS动态显示倒计时效果
2019/12/12 Javascript
vue+vant实现商品列表批量倒计时功能
2020/01/13 Javascript
js实现消灭星星(web简易版)
2020/03/24 Javascript
[46:47]完美世界DOTA2联赛PWL S2 FTD vs Magma 第二场 11.20
2020/11/23 DOTA
理解python正则表达式
2016/01/15 Python
Python中str.format()详解
2017/03/12 Python
Python 调用 Windows API COM 新法
2019/08/22 Python
解决Django migrate不能发现app.models的表问题
2019/08/31 Python
Python3如何判断三角形的类型
2020/04/12 Python
详解python中groupby函数通俗易懂
2020/05/14 Python
用python查找统一局域网下ip对应的mac地址
2021/01/13 Python
Timberland澳大利亚官网:全球领先的户外品牌
2019/12/10 全球购物
汇智创新科技发展有限公司
2015/12/06 面试题
会计专业毕业自荐书范文
2014/02/08 职场文书
中青班党性分析材料
2014/02/16 职场文书
管理部副部长岗位职责范文
2014/03/09 职场文书
公司放假通知范文
2015/04/14 职场文书
企业财务管理制度范本
2015/08/04 职场文书
2016幼儿园毕业感言
2015/12/08 职场文书
小学英语教学反思范文
2016/02/15 职场文书
手残删除python之后的补救方法
2021/06/26 Python