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 相关文章推荐
聊天室php&mysql(三)
Oct 09 PHP
如何提高MYSQL数据库的查询统计速度 select 索引应用
Apr 11 PHP
php date与gmdate的获取日期的区别
Feb 08 PHP
PHP对象转换为数组函数(递归方法)
Feb 04 PHP
memcache命令启动参数中文解释
Jan 13 PHP
php调用nginx的mod_zip模块打包ZIP文件
Jun 11 PHP
PHP检测用户语言的方法
Jun 15 PHP
thinkPHP下ueditor的使用方法详解
Dec 26 PHP
php 实现Hash表功能实例详解
Nov 29 PHP
PHP面向对象五大原则之接口隔离原则(ISP)详解
Apr 04 PHP
php正确输出json数据的实例讲解
Aug 21 PHP
PHP设计模式之单例模式定义与用法分析
Mar 26 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
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
2015/05/11 PHP
如何在旧的PHP系统中使用PHP 5.3之后的库
2015/12/02 PHP
Redis构建分布式锁
2017/03/28 PHP
PHP的静态方法与普通方法用法实例分析
2019/09/26 PHP
基于laravel belongsTo使用详解
2019/10/18 PHP
始终在屏幕中间显示Div的代码(css+js)
2011/03/10 Javascript
用JS判别浏览器种类以及IE版本的几种方法小结
2011/08/02 Javascript
js中一维数组和二位数组中的几个问题示例说明
2014/07/17 Javascript
jQuery中change事件用法实例
2014/12/26 Javascript
在JavaScript应用中使用RequireJS来实现延迟加载
2015/07/01 Javascript
AngularJs 60分钟入门基础教程
2016/04/03 Javascript
jquery.Callbacks的实现详解
2016/11/30 Javascript
Vue.js系列之项目结构说明(2)
2017/01/03 Javascript
bootstrap 表单验证使用方法
2017/01/11 Javascript
jQuery上传多张图片带进度条样式(DEMO)
2017/03/02 Javascript
详解vue-loader在项目中是如何配置的
2018/06/04 Javascript
微信小程序获取当前位置和城市名
2019/11/13 Javascript
JS实现碰撞检测效果
2020/03/12 Javascript
基于Python os模块常用命令介绍
2017/11/03 Python
Python获取指定文件夹下的文件名的方法
2018/02/06 Python
详解使用python绘制混淆矩阵(confusion_matrix)
2019/07/14 Python
numpy求平均值的维度设定的例子
2019/08/24 Python
jenkins配置python脚本定时任务过程图解
2019/10/29 Python
Python 中 sorted 如何自定义比较逻辑
2021/02/02 Python
HTML5 video 上传预览图片视频如何设置、预览视频某秒的海报帧
2018/08/28 HTML / CSS
美国知名玩具品牌:Melissa & Doug
2016/08/16 全球购物
汉森冲浪板:Hansen Surfboards
2018/05/19 全球购物
简历中自我评价范文3则
2013/12/14 职场文书
四年级下册教学反思
2014/02/01 职场文书
机关领导查摆四风思想汇报
2014/09/13 职场文书
2015元旦节寄语
2014/12/08 职场文书
廉政承诺书
2015/01/19 职场文书
2015婚礼主持词开场白
2015/05/28 职场文书
2016年秋季开学典礼新闻稿
2015/11/25 职场文书
2019年思想汇报
2019/06/20 职场文书
Vue过滤器(filter)实现及应用场景详解
2021/06/15 Vue.js