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下使用strpos需要注意 === 运算符
Jul 17 PHP
php array的学习笔记
May 10 PHP
php header功能的使用
Oct 28 PHP
codeigniter上传图片不能正确识别图片类型问题解决方法
Jul 25 PHP
laravel 5 实现模板主题功能(续)
Mar 02 PHP
Yii2中YiiBase自动加载类、引用文件方法分析(autoload)
Jul 25 PHP
PHP将身份证正反面两张照片合成一张图片的代码
Apr 08 PHP
ThinkPHP下表单令牌错误与解决方法分析
May 20 PHP
php+lottery.js实现九宫格抽奖功能
Jul 21 PHP
浅谈PHP5.6 与 PHP7.0 区别
Oct 09 PHP
PHP中isset、empty的用法与区别示例详解
Nov 05 PHP
php解析非标准json、非规范json的方式实例
May 10 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 array_map()数组函数使用说明
2011/07/12 PHP
ThinkPHP模板判断输出Empty标签用法详解
2014/06/30 PHP
firefox浏览器下javascript 拖动层效果与原理分析代码
2007/12/04 Javascript
javascript中length属性的探索
2011/07/31 Javascript
js判断页面中是否有指定控件的简单实例
2014/03/04 Javascript
Javascript排序算法之合并排序(归并排序)的2个例子
2014/04/04 Javascript
node.js中的console.timeEnd方法使用说明
2014/12/09 Javascript
JavaScript中连接操作Oracle数据库实例
2015/04/02 Javascript
js实现缓冲运动效果的方法
2015/04/10 Javascript
JavaScript中String.prototype用法实例
2015/05/20 Javascript
Angular.js与Bootstrap相结合实现手风琴菜单代码
2016/04/13 Javascript
基于百度地图实现产品销售的单位位置查看功能设计与实现
2016/10/21 Javascript
jquery插件bootstrapValidator表单验证详解
2016/12/15 Javascript
jQuery实现联动下拉列表查询框
2017/01/04 Javascript
基于JavaScript实现拖动滑块效果
2017/02/16 Javascript
微信小程序scroll-x失效的完美解决方法
2018/07/18 Javascript
vue实现下拉菜单树
2020/10/22 Javascript
[02:23]1个至宝=115个英雄特效 最“绿”至宝拉比克“魔导师密钥”登场
2018/12/29 DOTA
python调用cmd复制文件代码分享
2013/12/27 Python
python实现根据窗口标题调用窗口的方法
2015/03/13 Python
解决pycharm运行出错,代码正确结果不显示的问题
2018/11/30 Python
Python面向对象之类和对象属性的增删改查操作示例
2018/12/14 Python
利用Pandas和Numpy按时间戳将数据以Groupby方式分组
2019/07/22 Python
解决django接口无法通过ip进行访问的问题
2020/03/27 Python
Tensorflow中k.gradients()和tf.stop_gradient()用法说明
2020/06/10 Python
Python实现Canny及Hough算法代码实例解析
2020/08/06 Python
解决Python3.8运行tornado项目报NotImplementedError错误
2020/09/02 Python
css3 利用transform打造走动的2D时钟
2020/10/20 HTML / CSS
UNIONBAY官网:美国青少年服装品牌
2019/03/26 全球购物
python re模块和正则表达式
2021/03/24 Python
地理科学专业毕业生求职信
2013/10/15 职场文书
前处理组长岗位职责
2014/03/01 职场文书
城市规划应届生推荐信
2014/09/08 职场文书
个人合伙协议书范本
2014/10/14 职场文书
个人创业事迹材料
2014/12/30 职场文书
向雷锋同志学习倡议书
2015/04/27 职场文书