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 相关文章推荐
URL Rewrite的设置方法
Jan 02 PHP
利用PHP和AJAX创建RSS聚合器的代码
Mar 13 PHP
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
Nov 07 PHP
php实现压缩多个CSS与JS文件的方法
Nov 11 PHP
PHP微信开发之文本自动回复
Jun 23 PHP
Mac版PhpStorm之XAMPP整合apache服务器配置的图文教程详解
Oct 13 PHP
php PDO异常处理详解
Nov 20 PHP
利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
Jun 27 PHP
PHP高精确度运算BC函数库实例详解
Aug 15 PHP
php 读写json文件及修改json的方法
Mar 07 PHP
PHP获取文件扩展名的常用方法小结【五种方式】
Apr 27 PHP
PHP实现转盘抽奖算法分享
Apr 15 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实现mysql数据库备份类
2008/03/20 PHP
PHP的消息通信机制测试实例
2016/11/10 PHP
php实现微信扫码支付
2017/03/26 PHP
浅谈laravel框架与thinkPHP框架的区别
2019/10/23 PHP
事件冒泡是什么如何用jquery阻止事件冒泡
2013/03/20 Javascript
js实现网页自动刷新可制作节日倒计时效果
2014/05/27 Javascript
JavaScript错误处理
2015/02/03 Javascript
jQuery中toggle()函数的使用实例
2015/04/17 Javascript
Javascript实现div的toggle效果实例分析
2015/06/09 Javascript
JS实现超简单的鼠标拖动效果
2015/11/02 Javascript
jquery表单插件Autotab使用方法详解
2016/06/24 Javascript
整理关于Bootstrap过渡动画的慕课笔记
2017/03/29 Javascript
解决ztree搜索中多级菜单展示不全问题
2017/07/05 Javascript
9102年webpack4搭建vue项目的方法步骤
2019/02/20 Javascript
webpack的tree shaking的实现方法
2019/09/18 Javascript
JS造成内存泄漏的几种情况实例分析
2020/03/02 Javascript
[11:57]《一刀刀一天》第十七期:TI中国军团加油!
2014/05/26 DOTA
[03:42]2016国际邀请赛中国区预选赛首日现场玩家采访
2016/06/26 DOTA
SVM基本概念及Python实现代码
2017/12/27 Python
Python基础教程之利用期物处理并发
2018/03/29 Python
python 列表、字典和集合的添加和删除操作
2019/12/16 Python
python常用运维脚本实例小结
2020/02/14 Python
突破canvas语法限制 让他支持链式语法
2012/12/24 HTML / CSS
英国布鲁姆精品店:Bloom Boutique
2018/03/01 全球购物
static全局变量与普通的全局变量有什么区别
2014/05/27 面试题
教师岗位职责
2013/11/17 职场文书
村级环境卫生整治方案
2014/05/04 职场文书
区域销售主管岗位职责
2014/06/15 职场文书
ktv周年庆活动方案
2014/08/18 职场文书
离婚协议书怎么写
2014/09/12 职场文书
三峡导游词
2015/01/31 职场文书
《百分数的认识》教学反思
2016/02/19 职场文书
PyQt5 显示超清高分辨率图片的方法
2021/04/11 Python
pytest配置文件pytest.ini的详细使用
2021/04/17 Python
修改并编译golang源码的操作步骤
2021/07/25 Golang
解决IIS7下无法绑定https主机的问题
2022/04/29 Servers