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 smarty函数扩展
Mar 15 PHP
PHP编码转换
Nov 05 PHP
PHP中header和session_start前不能有输出原因分析
Jan 11 PHP
PHP常量使用的几个需要注意的地方(谨慎使用PHP中的常量)
Sep 12 PHP
PHP网站开发中常用的8个小技巧
Feb 13 PHP
浅析PHP中call user func()函数及如何使用call user func调用自定义函数
Nov 05 PHP
PHP引用的调用方法分析
Apr 25 PHP
PHP三种方式实现链式操作详解
Jan 21 PHP
PHP输出XML格式数据的方法总结
Feb 08 PHP
php实现微信企业号支付个人的方法详解
Jul 26 PHP
PHP安装memcache扩展的步骤讲解
Feb 14 PHP
php7新特性的理解和比较总结
Apr 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
剧场版动画《PSYCHO-PASS 3 FIRST INSPECTOR》3月27日日本上映!
2020/03/06 日漫
Banner程序
2006/10/09 PHP
使ecshop模板中可引用常量的实现方法
2011/06/02 PHP
不重新编译PHP为php增加openssl模块的方法
2011/06/14 PHP
php与python实现的线程池多线程爬虫功能示例
2016/10/12 PHP
Laravel 5+ .env环境配置文件详解
2020/04/06 PHP
Aliyun Linux 编译安装 php7.3 tengine2.3.2 mysql8.0 redis5的过程详解
2020/10/20 PHP
js实现文字在按钮上滚动的方法
2015/08/20 Javascript
confirm确认对话框的实现方法总结
2016/06/17 Javascript
Vue.js仿Metronic高级表格(二)数据渲染
2017/04/19 Javascript
浅谈AngularJs 双向绑定原理(数据绑定机制)
2017/12/07 Javascript
详解react native页面间传递数据的几种方式
2018/11/07 Javascript
js实现下拉框二级联动
2018/12/04 Javascript
vue请求本地自己编写的json文件的方法
2019/04/25 Javascript
vue路由跳转传递参数的方式总结
2020/05/10 Javascript
el-table树形表格表单验证(列表生成序号)
2020/05/31 Javascript
Python  __getattr__与__setattr__使用方法
2008/09/06 Python
python-str,list,set间的转换实例
2018/06/27 Python
python特性语法之遍历、公共方法、引用
2018/08/08 Python
python+Splinter实现12306抢票功能
2018/09/25 Python
Python 移动光标位置的方法
2019/01/20 Python
查看Python依赖包及其版本号信息的方法
2019/08/13 Python
python pillow模块使用方法详解
2019/08/30 Python
如何利用Python给自己的头像加一个小国旗(小月饼)
2020/10/02 Python
Lookfantastic香港官网:英国知名美妆购物网站
2018/06/19 全球购物
《得道多助,失道寡助》教学反思
2014/04/19 职场文书
竞选文艺委员演讲稿
2014/04/28 职场文书
软件项目开发计划书
2014/05/01 职场文书
保卫钓鱼岛口号
2014/06/20 职场文书
董事长助理工作职责范本
2014/07/01 职场文书
单位未婚证明范本
2014/11/25 职场文书
2014年社区卫生工作总结
2014/12/18 职场文书
2016年度继续教育学习心得体会
2016/01/19 职场文书
python实现简单的名片管理系统
2021/04/26 Python
详解python的内存分配机制
2021/05/10 Python
Golang 结构体数据集合
2022/04/22 Golang