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 相关文章推荐
DedeCMS 核心类TypeLink.class.php摘要笔记
Apr 07 PHP
PHP学习笔记之数组篇
Jun 28 PHP
PHP的SQL注入过程分析
Jan 06 PHP
使用array mutisort 实现按某字段对数据排序
Jun 18 PHP
php设置静态内容缓存时间的方法
Dec 01 PHP
php内存缓存实现方法
Jan 24 PHP
php利用事务处理转账问题
Apr 22 PHP
PHP 二级子目录(后台目录)设置二级域名
Mar 02 PHP
基于win2003虚拟机中apache服务器的访问
Aug 01 PHP
PHP自动生成缩略图函数的源码示例
Mar 18 PHP
php输出形式实例整理
May 05 PHP
THINKPHP5.1 Config的配置与获取详解
Jun 08 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 disk_free_space 返回目录可用空间
2010/05/10 PHP
PHP连接SQLServer2005 的问题解决方法
2010/07/19 PHP
windows下配置apache+php+mysql时出现问题的处理方法
2014/06/20 PHP
浅析PHP中strlen和mb_strlen的区别
2014/08/31 PHP
PHP生成条形码大揭秘
2015/09/24 PHP
几行代码轻松实现PHP文件打包下载zip
2017/03/01 PHP
ThinkPHP 3.2.2实现事务操作的方法
2017/05/05 PHP
yii2中LinkPager增加总页数和总记录数的实例
2017/08/28 PHP
PHP 范围解析操作符(::)用法分析【访问静态成员和类常量】
2020/04/14 PHP
ExtJS扩展 垂直tabLayout实现代码
2009/06/21 Javascript
js根据给定的日期计算当月有多少天实现思路及代码
2013/02/25 Javascript
在线所见即所得HTML编辑器的实现原理浅析
2015/04/25 Javascript
详解js中class的多种函数封装方法
2016/01/03 Javascript
JS在一定时间内跳转页面及各种刷新页面的实现方法
2016/05/26 Javascript
漫谈JS引擎的运行机制 你应该知道什么
2016/06/15 Javascript
js实现的在线调色板功能完整实例
2016/12/21 Javascript
浅谈Vue.js中的v-on(事件处理)
2017/09/05 Javascript
Node调用Java的示例代码
2017/09/20 Javascript
微信小程序 循环及嵌套循环的使用总结
2017/09/26 Javascript
详解微信小程序Page中data数据操作和函数调用
2017/09/27 Javascript
vue router自动判断左右翻页转场动画效果
2017/10/10 Javascript
微信小程序分享海报生成的实现方法
2018/12/10 Javascript
基于layui table返回的值的多级嵌套的解决方法
2019/09/19 Javascript
html5以及jQuery实现本地图片上传前的预览代码实例讲解
2021/03/01 jQuery
python判断windows系统是32位还是64位的方法
2015/05/11 Python
python Crypto模块的安装与使用方法
2017/12/21 Python
Python将视频或者动态图gif逐帧保存为图片的方法
2019/09/10 Python
关于tf.reverse_sequence()简述
2020/01/20 Python
如何在 Django 模板中输出 "{{"
2020/01/24 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
2020/06/02 Python
英国工艺品购物网站:Minerva Crafts
2018/01/29 全球购物
如何通过 CSS 写出火焰效果
2021/03/24 HTML / CSS
老师的检讨书
2014/02/23 职场文书
纺织工程专业推荐信
2014/09/08 职场文书
承德避暑山庄导游词
2015/02/03 职场文书
领导干部失职检讨书
2015/05/05 职场文书