php图片裁剪函数


Posted in PHP onOctober 31, 2018

本文实例为大家分享了php图片裁剪函数的具体代码,供大家参考,具体内容如下

/*
 * 图片裁剪工具
 * 将指定文件裁剪成正方形
 * 以中心为起始向四周裁剪
 * @param $src_path string 源文件地址
 * @param $des_path string 保存文件地址
 * @param $des_w double 目标图片宽度
 * */
function img_cut_square($src_path,$des_path,$des_w=100){
  $img_info = getimagesize($src_path);//获取原图像尺寸信息
  $img_width = $img_info[0];//原图宽度
  $img_height = $img_info[1];//原图高度
  $img_type = $img_info[2];//图片类型 1 为 GIF 格式、 2 为 JPEG/JPG 格式、3 为 PNG 格式
  if($img_type != 2 && $img_type != 3) return ;

  /*计算缩放尺寸*/
  if($img_height > $img_width){
    $scale_width = $des_w;//缩放宽度
    $scale_height = round($des_w / $img_width * $img_height);//缩放高度

    $src_y = round(($scale_height - $des_w)/2);
    $src_x = 0;
  }else{
    $scale_height = $des_w;
    $scale_width = round($des_w / $img_height * $img_width);

    $src_y = 0;
    $src_x = round(($scale_width - $des_w)/2);
  }

  $dst_ims = imagecreatetruecolor($scale_width, $scale_height);//创建真彩画布
  $white = imagecolorallocate($dst_ims, 255, 255, 255);
  imagefill($dst_ims, 0, 0, $white);
  if($img_type == 2){
    $src_im = @imagecreatefromjpeg($src_path);//读取原图像
  }else if($img_type == 3){
    $src_im = @imagecreatefrompng($src_path);//读取原图像
  }

  imagecopyresized($dst_ims, $src_im, 0, 0 ,0, 0 , $scale_width , $scale_height , $img_width,$img_height);//缩放图片到指定尺寸


  $dst_im = imagecreatetruecolor($des_w, $des_w);
//  $white = imagecolorallocate($dst_im, 255, 255, 255);
//  imagefill($dst_im, 0, 0, $white);
  imagecopy($dst_im, $dst_ims, 0, 0, $src_x, $src_y, $des_w, $des_w);//开始裁剪图片为正方形
// imagecopyresampled($dst_im, $src_im, $src_x, $src_y, 0, 0, $real_width, $real_width,$img_width,$img_height);
  if($img_type == 2) {
    imagejpeg($dst_im, $des_path);//保存到文件
  }else if($img_type == 3){
    imagepng($dst_im,$des_path);
  }
//  imagejpeg($dst_im);//输出到浏览器
  imagedestroy($dst_im);
  imagedestroy($dst_ims);
  imagedestroy($src_im);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
MySql中正则表达式的使用方法描述
Jul 30 PHP
网站用php实现paypal整合方法
Nov 28 PHP
header中Content-Disposition的作用与使用方法
Jun 13 PHP
深入php socket的讲解与实例分析
Jun 13 PHP
PHP正则提取不包含指定网址的图片地址的例子
Apr 21 PHP
php判断页面是否是微信打开的示例(微信打开网页)
Apr 25 PHP
destoon调用自定义模板及样式的公告栏
Jun 21 PHP
对PHP依赖注入的理解实例分析
Oct 09 PHP
PHP中如何使用Redis接管文件存储Session详解
Nov 28 PHP
PHP单例模式实例分析【防继承,防克隆操作】
May 22 PHP
laravel 操作数据库常用函数的返回值方法
Oct 11 PHP
详解no input file specified 三种解决方法
Nov 29 PHP
php+js实现裁剪任意形状图片
Oct 31 #PHP
workerman结合laravel开发在线聊天应用的示例代码
Oct 30 #PHP
golang实现php里的serialize()和unserialize()序列和反序列方法详解
Oct 30 #PHP
swoole_process实现进程池的方法示例
Oct 29 #PHP
PHP大文件分片上传的实现方法
Oct 28 #PHP
PHP array_reduce()函数的应用解析
Oct 28 #PHP
php 中phar包的使用教程详解
Oct 26 #PHP
You might like
destoon切换城市后实现logo旁边显示地区名称的方法
2014/08/21 PHP
crontab无法执行php的解决方法
2016/01/25 PHP
PHP实现十进制、二进制、八进制和十六进制转换相关函数用法分析
2017/04/25 PHP
Laravel学习教程之IOC容器的介绍与用例
2017/08/15 PHP
PHP类的自动加载机制实现方法分析
2019/01/10 PHP
offsetHeight在OnLoad中获取为0的现象
2013/07/22 Javascript
使用JS CSS去除IE链接虚线框的三种方法
2013/11/14 Javascript
聊一聊JS中的prototype
2016/09/29 Javascript
Angular2 PrimeNG分页模块学习
2017/01/14 Javascript
借助node实战JSONP跨域实例
2017/03/30 Javascript
layer弹窗插件操作方法详解
2017/05/19 Javascript
Vue封装一个简单轻量的上传文件组件的示例
2018/03/21 Javascript
JS加密插件CryptoJS实现的DES加密示例
2018/08/16 Javascript
angular4应用中输入的最小值和最大值的方法
2019/05/17 Javascript
Vue将props值实时传递 并可修改的操作
2020/08/09 Javascript
微信小程序实现简单购物车功能
2020/12/30 Javascript
python实现将文本转换成语音的方法
2015/05/28 Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
2018/01/16 Python
Python使用flask框架操作sqlite3的两种方式
2018/01/31 Python
Python扩展内置类型详解
2018/03/26 Python
python 寻找list中最大元素对应的索引方法
2018/06/28 Python
对Python通过pypyodbc访问Access数据库的方法详解
2018/10/27 Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
2020/02/25 Python
html5/css3响应式页面开发总结
2018/10/16 HTML / CSS
如何为DataGridView添加一个定制的Column Type
2014/01/21 面试题
静态成员和非静态成员的区别
2012/05/12 面试题
资产经营总监岗位职责
2013/12/04 职场文书
初一科学教学反思
2014/01/27 职场文书
借款协议书
2014/04/12 职场文书
竞选班长的演讲稿
2014/04/24 职场文书
语文教育专业求职信
2014/06/28 职场文书
学雷锋志愿者活动方案
2014/08/21 职场文书
2015年党性分析材料
2014/12/19 职场文书
幼儿园五一劳动节活动总结
2015/02/09 职场文书
2015大学迎新晚会策划书
2015/07/16 职场文书
婚礼必备主持词范本!
2019/07/23 职场文书