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 相关文章推荐
分页显示Oracle数据库记录的类之一
Oct 09 PHP
PHP+MySQL 手工注入语句大全 推荐
Oct 30 PHP
单一index.php实现PHP任意层级文件夹遍历(Zjmainstay原创)
Jul 31 PHP
一个漂亮的php验证码类(分享)
Aug 06 PHP
yii框架表单模型使用及以数组形式提交表单数据示例
Apr 30 PHP
PHP微框架Dispatch简介
Jun 12 PHP
PHP准确取得服务器IP地址的方法
Jun 02 PHP
详解PHP中array_rand函数的使用方法
Sep 11 PHP
PHP中的use关键字及文件的加载详解
Nov 28 PHP
yii2.0整合阿里云oss的示例代码
Sep 19 PHP
Laravel中encrypt和decrypt的实现方法
Sep 24 PHP
PHP检测一个数组有没有定义的方法步骤
Jul 20 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
怎样在UNIX系统下安装php3
2006/10/09 PHP
php牛逼的面试题分享
2013/01/18 PHP
php统计时间和内存使用情况示例分享
2014/03/13 PHP
php导出csv数据在浏览器中输出提供下载或保存到文件的示例
2014/04/24 PHP
PHP异步进程助手async-helper
2018/02/05 PHP
jquery动画3.创建一个带遮罩效果的图片走廊
2012/08/24 Javascript
javascript特殊文本输入框网页特效
2016/09/13 Javascript
基于JavaScript实现前端文件的断点续传
2016/10/17 Javascript
浅谈es6中export和export default的作用及区别
2018/02/07 Javascript
jQuery高级编程之js对象、json与ajax用法实例分析
2019/11/01 jQuery
python发送邮件的实例代码(支持html、图片、附件)
2013/03/04 Python
在Python中使用poplib模块收取邮件的教程
2015/04/29 Python
Python ftp上传文件
2016/02/13 Python
总结Python编程中函数的使用要点
2016/03/20 Python
Python3实现发送QQ邮件功能(文本)
2017/12/15 Python
Python实现简易Web爬虫详解
2018/01/03 Python
python DataFrame 修改列的顺序实例
2018/04/10 Python
利用python GDAL库读写geotiff格式的遥感影像方法
2018/11/29 Python
Python 线程池用法简单示例
2019/10/02 Python
Python socket实现的文件下载器功能示例
2019/11/15 Python
PyCharm2020.1.2社区版安装,配置及使用教程详解(Windows)
2020/08/07 Python
如何基于Python实现word文档重新排版
2020/09/29 Python
CSS3 filter(滤镜)实现网页灰色或者黑色模式的代码
2020/11/30 HTML / CSS
HTML5的结构和语义(2):结构
2008/10/17 HTML / CSS
全球速卖通巴西站点:Aliexpress巴西
2016/08/24 全球购物
施华洛世奇西班牙官网:SWAROVSKI西班牙
2019/06/06 全球购物
Hotels.com越南:酒店预订
2019/10/29 全球购物
计算机毕业生自荐信范文
2014/03/23 职场文书
财政局党的群众路线教育实践活动整改方案
2014/09/21 职场文书
2015年环卫工作总结
2015/04/28 职场文书
学习计划是什么
2019/04/30 职场文书
python 下载文件的几种方式分享
2021/04/07 Python
MySQL系列之八 MySQL服务器变量
2021/07/02 MySQL
如何利用Python实现n*n螺旋矩阵
2022/01/18 Python
APP界面设计技巧和注意事项
2022/04/29 杂记
解决Oracle数据库用户密码过期
2022/05/11 Oracle