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的FTP学习(二)[转自奥索]
Oct 09 PHP
Oracle 常见问题解答
Oct 09 PHP
十天学会php之第六天
Oct 09 PHP
8个出色的WordPress SEO插件收集
Feb 26 PHP
php中json_decode()和json_encode()的使用方法
Jun 04 PHP
php页面缓存ob系列函数介绍
Oct 18 PHP
解析PHP将对象转换成数组的方法(兼容多维数组类型)
Jun 21 PHP
PHP过滤黑名单关键字的方法
Dec 01 PHP
Linux系统递归生成目录中文件的md5的方法
Jun 29 PHP
Joomla语言翻译类Jtext用法分析
May 05 PHP
PHP list() 将数组中的值赋给变量的简单实例
Jun 13 PHP
设定php简写功能的方法
Nov 28 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脚本的10个技巧(4)
2006/10/09 PHP
php购物网站支付paypal使用方法
2010/11/28 PHP
php面向对象中static静态属性与方法的内存位置分析
2015/02/08 PHP
php单文件版在线代码编辑器
2015/03/12 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
2015/04/07 PHP
php 无限分类 树形数据格式化代码
2016/10/11 PHP
PHP判断访客是否手机端(移动端浏览器)访问的方法总结【4种方法】
2019/03/27 PHP
javascript sudoku 数独智力游戏生成代码
2010/03/27 Javascript
JQuery实现简单时尚快捷的气泡提示插件
2012/12/20 Javascript
jquery.post用法之type设置问题
2014/02/24 Javascript
父页面显示遮罩层弹出半透明状态的dialog
2014/03/04 Javascript
快速学习jQuery插件 Cookie插件使用方法
2015/12/01 Javascript
Bootstrap实现响应式导航栏效果
2015/12/28 Javascript
利用fecha进行JS日期处理
2016/11/21 Javascript
使用Promise链式调用解决多个异步回调的问题
2017/01/15 Javascript
angularjs+bootstrap实现自定义分页的实例代码
2017/06/19 Javascript
node.js环境搭建图文详解
2018/09/19 Javascript
vue中组件的3种使用方式详解
2019/03/23 Javascript
Vue运用transition实现过渡动画
2019/05/06 Javascript
React中Ref 的使用方法详解
2020/04/28 Javascript
[01:11:15]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[38:42]完美世界DOTA2联赛循环赛 Matador vs Forest BO2第二场 11.05
2020/11/05 DOTA
Python线程指南详细介绍
2017/01/05 Python
Ubuntu下创建虚拟独立的Python环境全过程
2017/02/10 Python
浅谈Pandas中map, applymap and apply的区别
2018/04/10 Python
Python实现统计给定字符串中重复模式最高子串功能示例
2018/05/16 Python
Python中Qslider控件实操详解
2021/02/20 Python
HTML5的Video标签有部分MP4无法播放的问题解析(多图)
2017/08/18 HTML / CSS
加拿大当代时尚服饰、配饰和鞋类专业零售商和制造商:LE CHÂTEAU
2017/10/06 全球购物
旷课检讨书大全
2014/01/21 职场文书
电钳工人个人求职信
2014/05/10 职场文书
邀请函格式范文
2015/02/02 职场文书
我的长征观后感
2015/06/09 职场文书
2016年国庆节67周年活动总结
2016/04/01 职场文书
java设计模式--建造者模式详解
2021/07/21 Java/Android
Android RecyclerView实现九宫格效果
2022/06/28 Java/Android