PHP 图片裁剪类封装
PHP工具类 图片裁剪类封装
<?php
namespace App\Utils;/*** 图片裁剪工具类* @author 田小涛* @date 2020年7月23日* @comment**/
class ImageCropUtils
{private $sImage;private $dImage;private $src_file;private $dst_file;private $src_width;private $src_height;private $src_ext;private $src_type;private $mime;//上传基础路径处private $basisUploadPath;public function __construct( $file = null, $distFile = null ){$this->dst_file = $distFile;$this->basisUploadPath = storage_path( 'app/uploads/' );if( isset( $file ) && $file ){$this->src_file = $file;$this->init( $file );}}/*** 生成唯一的文件名称* @author Administrator* @datetime 2019年12月24日 上午11:44:02* @comment * * @param string $salt* @return string*/protected function getUniqueDiskName($salt = ''){if( empty( $salt ) ){$salt = mt_rand(1,1000000);}list($usec, $sec) = explode(" ", microtime());$micros = str_replace('.', '', ((float)$usec + (float)$sec)).$salt;return md5( $micros );}/*** 初始化参数* @author Administrator* @datetime 2020年7月22日 下午3:00:22* @comment * * @return boolean*/public function init( $url ){$strExt = $this->getImgExt( $url );$filename = $this->getUniqueDiskName( md5( $url ) );$path = date( 'y' ) . '/' . date( 'm' ) . '/' . date( 'd' ) . '/' . $filename;$dowRes = new \generalDowmload( $url, $path . $strExt );if( !empty( $dowRes ) && isset( $dowRes->basename ) ){if( isset( $dowRes->location ) && strlen( $dowRes->location ) ){$this->src_file = $dowRes->location;}else{$this->src_file = $this->basisUploadPath . $path . $strExt;}}else{return false;}if( !isset( $this->src_file ) || is_null( $this->src_file ) || !file_exists( $this->src_file ) ){return false;}$arrImageInfos = @getimagesize( $this->src_file );if( !isset( $arrImageInfos ) || empty( $arrImageInfos ) ){return false;}if( isset( $arrImageInfos[0] ) && $arrImageInfos[0] ){$this->src_width = $arrImageInfos[0];}if( isset( $arrImageInfos[1] ) && $arrImageInfos[1] ){$this->src_height = $arrImageInfos[1];}$this->src_type = 2;if( isset( $arrImageInfos[2] ) && $arrImageInfos[2] ){$this->src_type = $arrImageInfos[2];}if( isset( $arrImageInfos[ 'mime' ] ) ){$this->mime = $arrImageInfos[ 'mime' ];}switch( $this->src_type ) {case IMAGETYPE_JPEG :ini_set( 'gd.jpeg_ignore_warning', true );$this->sImage = @imagecreatefromjpeg( $this->src_file );$this->ext = 'jpg';if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 ){$this->mime = 'image/jpeg';}break;case IMAGETYPE_PNG :$this->sImage = @imagecreatefrompng( $this->src_file );$this->ext = 'png';if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 ){$this->mime = 'image/' . $this->ext;}break;case IMAGETYPE_GIF :$this->sImage = imagecreatefromgif( $this->src_file );$this->ext = 'gif';if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 ){$this->mime = 'image/' . $this->ext;;}break;case 18:$this->sImage = @imagecreatefromwebp( $this->src_file );$this->ext = 'webp';if( !isset( $this->mime ) || strlen( $this->mime ) <= 0 ){$this->mime = 'image/' . $this->ext;;}break;default:return false;}return true;}/*** 裁剪* @author Administrator* @datetime 2020年7月22日 下午3:07:36* @comment * * @param unknown $dst_width* @param unknown $dst_height* @param unknown $dst_x* @param unknown $dst_y* @param string $dst_file* @return boolean*/public function cutImage( $dst_width, $dst_height, $dst_x, $dst_y, $originWidth, $originHeight ){if( !$dst_width || !$dst_height ){return false;}# 创建画布时,判断最终需要的画布大小if ($originWidth && $originHeight){$dst_w = $originWidth;$dst_h = $originHeight;} else{$dst_w = $dst_width;$dst_h = $dst_height;}$this->dImage = imagecreatetruecolor( $dst_w, $dst_h ); //创建了目标文件的大小的画布$bg = imagecolorallocatealpha( $this->dImage, 255, 255, 255, 127 ); //给画布分配颜色imagefill( $this->dImage, 0, 0, $bg ); //给图像用颜色进行填充imagecolortransparent( $this->dImage, $bg ); //背景定义成透明色$ratio_w = 1.0 * $dst_width / $this->src_width; //横向缩放的比例$ratio_h = 1.0 * $dst_height / $this->src_height; //纵向缩放的比例//不进行缩放,直接对图像进行裁剪$ratio = 1.0;$tmp_w = (int)($dst_width / $ratio);$tmp_h = (int)($dst_height / $ratio);$tmp_img = imagecreatetruecolor( $dst_width, $dst_height ); //创建暂时保存的画布imagecopy( $tmp_img, $this->sImage, 0, 0, $dst_x,$dst_y, $dst_width, $dst_height ); //拷贝出图像的一部分,进行裁切imagecopyresampled( $this->dImage, $tmp_img, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h ); //把暂时缓存的图片,放到目标文件里面imagedestroy( $tmp_img );return true;}/*** 存储* @author Administrator* @datetime 2020年7月22日 下午3:15:52* @comment * * @param unknown $file* @return boolean*/public function save( $file = null ){if( !isset( $file ) || is_null( $file ) ){$this->dst_file = $this->src_file;}else{$this->dst_file = $this->basisUploadPath . '/'. $file;}try{switch( $this->src_type ){case IMAGETYPE_JPEG :@imagejpeg( $this->dImage, $this->dst_file, 98 );break;case IMAGETYPE_PNG :imagepng( $this->dImage, $this->dst_file );break;case IMAGETYPE_GIF :imagegif( $this->dImage, $this->dst_file );break;case 18:@imagejpeg( $this->dImage, $this->dst_file, 98 );break;default:return false;}}catch( \Exception $e ){}$strExt = $this->getImgExt( $this->dst_file );$tmpImageInfo = @getimagesize( $this->dst_file );$width = 0;$height = 0;if( isset( $tmpImageInfo[0] ) && $tmpImageInfo[0] > 0 ){$width = $tmpImageInfo[0];}if( isset( $tmpImageInfo[1] ) && $tmpImageInfo[1] > 0 ){$height = $tmpImageInfo[1];}$objRet = new \stdClass();$objRet->mime = $this->mime;$objRet->filename = basename( $this->dst_file );$objRet->ext = $strExt;$objRet->width = $width;$objRet->height = $height;return $objRet;}/*** 数据销毁* @author Administrator* @datetime 2020年7月22日 下午3:31:12* @comment **/public function destroy(){imagedestroy( $this->sImage);imagedestroy( $this->dImage );@unlink( $this->src_file );return true;}/*** 检索图集是否存在后缀* 若不存在-则使用默认(强制转换)* @author Administrator* @datetime 2018年11月27日 下午3:30:47* @comment** @param unknown $url* @return string|unknown*/protected function getImgExt( $url ){$iLastSlash = strrpos( $url, '/' );$strFileName = mb_substr( $url, intval($iLastSlash+1) );$strExt = strrchr( $strFileName, '.' );preg_match( "/(.*?)\?.*?/", $strExt, $matches );if( !empty( $matches ) && isset( $matches[1] ) ){$strExt = $matches[1];}if( false == $strExt || is_null( $strExt ) || strlen( $strExt ) <= 0 ){$strExt = '.jpg';}return $strExt;}}
相关文章:
PHP 图片裁剪类封装
PHP工具类 图片裁剪类封装 <?php namespace App\Utils;/*** 图片裁剪工具类* author 田小涛* date 2020年7月23日* comment**/ class ImageCropUtils {private $sImage;private $dImage;private $src_file;private $dst_file;private $src_width;private $src_height;priv…...

Android 14.0 SystemUI修改状态栏电池图标样式为横屏显示
1.概述 在14.0的系统rom产品定制化开发中,对于原生系统中SystemUId 状态栏的电池图标是竖着显示的,一般手机的电池图标都是横屏显示的 可以觉得样式挺不错的,所以由于产品开发要求电池图标横着显示和手机的样式一样,所以就得重新更换SystemUI状态栏的电池样式了 如图: 2.S…...

FPGA:图像数字细节增强算法(工程+仿真+实物,可用毕设)
目录 日常唠嗑一、视频效果二、硬件及功能1、硬件选择2、功能3、特点 未完、待续……四、工程设计五、板级验证六、工程获取 日常唠嗑 有2个多月没写文章了,又是老借口:“最近实在是很忙”🤣,不过说真,确实是比较忙&am…...
Android netty的使用
导入netty依赖 implementation io.netty:netty-all:4.1.107.Final使用netty 关闭netty /*** 关闭*/private void closeSocket() {LogUtils.i(TAG, "closeSocket");if (nettyManager ! null) {nettyManager.close();nettyManager null;}if (nettyExecutor ! null) {…...

苹果电脑启动磁盘是什么意思 苹果电脑磁盘清理软件 mac找不到启动磁盘 启动磁盘没有足够的空间来进行分区
当你一早打开苹果电脑,结果系统突然提示: “启动磁盘已满,需要删除部分文件”。你会怎么办?如果你认为单纯靠清理废纸篓或者删除大型文件就能释放你的启动磁盘上的空间,那就大错特错了。其实苹果启动磁盘的清理技巧有很…...

【Java SE】多态
🥰🥰🥰来都来了,不妨点个关注叭! 👉博客主页:欢迎各位大佬!👈 文章目录 1. 多态1.1 多态是什么1.2 多态的意义1.3 多态的实现条件 2. 重写2.1 重写的概念2.2 重写的规则2.3 重写与重…...
Yarn vs npm的大同小异Yarn是什么?
Yarn vs npm的大同小异&Yarn是什么? 一、Yarn、npm是什么?二、Yarn vs npm:特性差异总结 一、Yarn、npm是什么? npm是Node.js的包管理器,是由Chris Korda维护。 npm,它全称为Node Package Manager,是…...

1.Godot引擎|场景|节点|GDS|介绍
Godot介绍 Godot是一款游戏引擎 可以通过在steam商城免费下载 初学者和编程基础稍差的推荐学习使用GDScript,和python有些相似 Godot节点 Godot的开发思想——围绕节点 节点的特征与优势 最常用基本的开发组件大部分都具有具体的功能,如图片…...
springboot3 redis 实现分布式锁
分布式锁介绍 分布式锁是一种在分布式系统中用于控制不同节点上的进程或线程对共享资源进行互斥访问的技术机制。 在分布式环境中,多个服务可能同时访问和操作共享资源,如数据库、文件系统等。为了保持数据的一致性和完整性,需要确保在同一…...
2024年第十四届MathorCup数学应用挑战赛A题思路分享(妈妈杯)
A题 移动通信网络中PCI规划问题 物理小区识别码(PCI)规划是移动通信网络中下行链路层上,对各覆盖小区编号进行合理配置,以避免PCI冲突、PCI混淆以及PCI模3干扰等现象。PCI规划对于减少物理层的小区间互相干扰(ICI),增加物理下行控制信道(PDCCH)的吞吐量有着重要的作用,尤其…...

运动听歌哪款耳机靠谱?精选五款热门开放式耳机
随着人们对运动健康的重视,越来越多的运动爱好者开始关注如何在运动中享受音乐。开放式蓝牙耳机凭借其独特的设计,成为了户外运动的理想选择。它不仅让你在运动时能够清晰听到周围环境的声音,保持警觉,还能让你在需要时与他人轻松…...

Kubernetes学习笔记12
k8s核心概念:控制器: 我们删除Pod是可以直接删除的,如果生产环境中的误操作,Pod同样也会被轻易地被删除掉。 所以,在K8s中引入另外一个概念:Controller(控制器)的概念,…...
Qt Designer 控件箱中的控件介绍及布局比列分配
控件箱介绍 Qt Designer的控件箱(Widget Box)包含了各种常用的控件,用户可以通过拖放的方式将这些控件添加到窗体设计器中,用于构建用户界面。以下是一些常见控件箱中的控件及其功能的讲解: 1.基本控件&#…...
蓝桥集训之三国游戏
蓝桥集训之三国游戏 核心思想:贪心 将每个事件的贡献值求出 降序排序从大到小求和为正是即可 #include <iostream>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N 100010;int a[N],b[N],c[N];…...

MySQL知识整理
MySQL知识整理 基础第一讲:基础架构:一条SQL查询语句是如何执行的?架构尽量减少长连接的原因和方案为什么尽量不要依赖查询缓存 索引第四讲:深入浅出索引(上)第五讲:深入浅出索引(下…...
代码随想录算法训练营第36天| 435. 无重叠区间、 763.划分字母区间*、56. 合并区间
435. 无重叠区间 力扣题目链接 代码 示例代码 class Solution { public:// 按照区间右边界排序static bool cmp (const vector<int>& a, const vector<int>& b) {return a[1] < b[1];}int eraseOverlapIntervals(vector<vector<int>>&a…...

SpringBoot整合Nacos
文章目录 nacosnacos下载nacos启动nacos相关配置demo-dev.yamldemo-test.yamluser.yaml 代码pom.xmlUserConfigBeanAutoRefreshConfigExampleValueAnnotationExampleDemoApplicationbootstrap.yml测试结果补充.刷新静态配置 nacos nacos下载 下载地址 一键傻瓜试安装即可,官…...
vue3 浅学
一、toRefs 问题: reactive 对象取出的所有属性值都是⾮响应式的 解决: 利⽤ toRefs 可以将⼀个响应式 reactive 对象的所有原始属性转换为 响应式的 ref 属性 二、hook函数 将可复⽤的功能代码进⾏封装,类似与vue2混⼊。 三、ref:获取元素或者组件 let …...

三小时使用鸿蒙OS模仿羊了个羊,附源码
学习鸿蒙arkTS语言,决定直接通过实践的方式上手,而不是一点点进行观看视频再来实现。 结合羊了个羊的开发思路,准备好相应的卡片素材后进行开发。遇到了需要arkTS进行解决的问题,再去查看相应的文档。 首先需要准备卡片对应的图片…...

如何使用 ArcGIS Pro 制作热力图
热力图是一种用颜色表示数据密度的地图,通常用来显示空间分布数据的热度或密度,我们可以通过 ArcGIS Pro 来制作热力图,这里为大家介绍一下制作的方法,希望能对你有所帮助。 数据来源 教程所使用的数据是从水经微图中下载的POI数…...

SpringBoot-17-MyBatis动态SQL标签之常用标签
文章目录 1 代码1.1 实体User.java1.2 接口UserMapper.java1.3 映射UserMapper.xml1.3.1 标签if1.3.2 标签if和where1.3.3 标签choose和when和otherwise1.4 UserController.java2 常用动态SQL标签2.1 标签set2.1.1 UserMapper.java2.1.2 UserMapper.xml2.1.3 UserController.ja…...

python/java环境配置
环境变量放一起 python: 1.首先下载Python Python下载地址:Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个,然后自定义,全选 可以把前4个选上 3.环境配置 1)搜高级系统设置 2…...
Objective-C常用命名规范总结
【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名(Class Name)2.协议名(Protocol Name)3.方法名(Method Name)4.属性名(Property Name)5.局部变量/实例变量(Local / Instance Variables&…...

【机器视觉】单目测距——运动结构恢复
ps:图是随便找的,为了凑个封面 前言 在前面对光流法进行进一步改进,希望将2D光流推广至3D场景流时,发现2D转3D过程中存在尺度歧义问题,需要补全摄像头拍摄图像中缺失的深度信息,否则解空间不收敛…...

如何理解 IP 数据报中的 TTL?
目录 前言理解 前言 面试灵魂一问:说说对 IP 数据报中 TTL 的理解?我们都知道,IP 数据报由首部和数据两部分组成,首部又分为两部分:固定部分和可变部分,共占 20 字节,而即将讨论的 TTL 就位于首…...
Element Plus 表单(el-form)中关于正整数输入的校验规则
目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入(联动)2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

C++使用 new 来创建动态数组
问题: 不能使用变量定义数组大小 原因: 这是因为数组在内存中是连续存储的,编译器需要在编译阶段就确定数组的大小,以便正确地分配内存空间。如果允许使用变量来定义数组的大小,那么编译器就无法在编译时确定数组的大…...

Aspose.PDF 限制绕过方案:Java 字节码技术实战分享(仅供学习)
Aspose.PDF 限制绕过方案:Java 字节码技术实战分享(仅供学习) 一、Aspose.PDF 简介二、说明(⚠️仅供学习与研究使用)三、技术流程总览四、准备工作1. 下载 Jar 包2. Maven 项目依赖配置 五、字节码修改实现代码&#…...
jmeter聚合报告中参数详解
sample、average、min、max、90%line、95%line,99%line、Error错误率、吞吐量Thoughput、KB/sec每秒传输的数据量 sample(样本数) 表示测试中发送的请求数量,即测试执行了多少次请求。 单位,以个或者次数表示。 示例:…...

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement
Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...