当前位置: 首页 > news >正文

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个多月没写文章了&#xff0c;又是老借口&#xff1a;“最近实在是很忙”&#x1f923;&#xff0c;不过说真&#xff0c;确实是比较忙&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找不到启动磁盘 启动磁盘没有足够的空间来进行分区

当你一早打开苹果电脑&#xff0c;结果系统突然提示&#xff1a; “启动磁盘已满&#xff0c;需要删除部分文件”。你会怎么办&#xff1f;如果你认为单纯靠清理废纸篓或者删除大型文件就能释放你的启动磁盘上的空间&#xff0c;那就大错特错了。其实苹果启动磁盘的清理技巧有很…...

【Java SE】多态

&#x1f970;&#x1f970;&#x1f970;来都来了&#xff0c;不妨点个关注叭&#xff01; &#x1f449;博客主页&#xff1a;欢迎各位大佬!&#x1f448; 文章目录 1. 多态1.1 多态是什么1.2 多态的意义1.3 多态的实现条件 2. 重写2.1 重写的概念2.2 重写的规则2.3 重写与重…...

Yarn vs npm的大同小异Yarn是什么?

Yarn vs npm的大同小异&Yarn是什么&#xff1f; 一、Yarn、npm是什么&#xff1f;二、Yarn vs npm&#xff1a;特性差异总结 一、Yarn、npm是什么&#xff1f; npm是Node.js的包管理器&#xff0c;是由Chris Korda维护。 npm,它全称为Node Package Manager&#xff0c;是…...

1.Godot引擎|场景|节点|GDS|介绍

Godot介绍 Godot是一款游戏引擎 可以通过在steam商城免费下载 初学者和编程基础稍差的推荐学习使用GDScript&#xff0c;和python有些相似 Godot节点 Godot的开发思想——围绕节点 节点的特征与优势 最常用基本的开发组件大部分都具有具体的功能&#xff0c;如图片&#xf…...

springboot3 redis 实现分布式锁

分布式锁介绍 分布式锁是一种在分布式系统中用于控制不同节点上的进程或线程对共享资源进行互斥访问的技术机制。 在分布式环境中&#xff0c;多个服务可能同时访问和操作共享资源&#xff0c;如数据库、文件系统等。为了保持数据的一致性和完整性&#xff0c;需要确保在同一…...

2024年第十四届MathorCup数学应用挑战赛A题思路分享(妈妈杯)

A题 移动通信网络中PCI规划问题 物理小区识别码(PCI)规划是移动通信网络中下行链路层上,对各覆盖小区编号进行合理配置,以避免PCI冲突、PCI混淆以及PCI模3干扰等现象。PCI规划对于减少物理层的小区间互相干扰(ICI),增加物理下行控制信道(PDCCH)的吞吐量有着重要的作用,尤其…...

运动听歌哪款耳机靠谱?精选五款热门开放式耳机

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

Kubernetes学习笔记12

k8s核心概念&#xff1a;控制器&#xff1a; 我们删除Pod是可以直接删除的&#xff0c;如果生产环境中的误操作&#xff0c;Pod同样也会被轻易地被删除掉。 所以&#xff0c;在K8s中引入另外一个概念&#xff1a;Controller&#xff08;控制器&#xff09;的概念&#xff0c;…...

Qt Designer 控件箱中的控件介绍及布局比列分配

控件箱介绍 Qt Designer的控件箱&#xff08;Widget Box&#xff09;包含了各种常用的控件&#xff0c;用户可以通过拖放的方式将这些控件添加到窗体设计器中&#xff0c;用于构建用户界面。以下是一些常见控件箱中的控件及其功能的讲解&#xff1a; 1.基本控件&#…...

蓝桥集训之三国游戏

蓝桥集训之三国游戏 核心思想&#xff1a;贪心 将每个事件的贡献值求出 降序排序从大到小求和为正是即可 #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知识整理 基础第一讲&#xff1a;基础架构&#xff1a;一条SQL查询语句是如何执行的&#xff1f;架构尽量减少长连接的原因和方案为什么尽量不要依赖查询缓存 索引第四讲&#xff1a;深入浅出索引&#xff08;上&#xff09;第五讲&#xff1a;深入浅出索引&#xff08;下…...

代码随想录算法训练营第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函数 将可复⽤的功能代码进⾏封装&#xff0c;类似与vue2混⼊。 三、ref&#xff1a;获取元素或者组件 let …...

三小时使用鸿蒙OS模仿羊了个羊,附源码

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

如何使用 ArcGIS Pro 制作热力图

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

[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解

突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 ​安全措施依赖问题​ GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...

【网络安全产品大调研系列】2. 体验漏洞扫描

前言 2023 年漏洞扫描服务市场规模预计为 3.06&#xff08;十亿美元&#xff09;。漏洞扫描服务市场行业预计将从 2024 年的 3.48&#xff08;十亿美元&#xff09;增长到 2032 年的 9.54&#xff08;十亿美元&#xff09;。预测期内漏洞扫描服务市场 CAGR&#xff08;增长率&…...

Qt Http Server模块功能及架构

Qt Http Server 是 Qt 6.0 中引入的一个新模块&#xff0c;它提供了一个轻量级的 HTTP 服务器实现&#xff0c;主要用于构建基于 HTTP 的应用程序和服务。 功能介绍&#xff1a; 主要功能 HTTP服务器功能&#xff1a; 支持 HTTP/1.1 协议 简单的请求/响应处理模型 支持 GET…...

python如何将word的doc另存为docx

将 DOCX 文件另存为 DOCX 格式&#xff08;Python 实现&#xff09; 在 Python 中&#xff0c;你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是&#xff0c;.doc 是旧的 Word 格式&#xff0c;而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

AI编程--插件对比分析:CodeRider、GitHub Copilot及其他

AI编程插件对比分析&#xff1a;CodeRider、GitHub Copilot及其他 随着人工智能技术的快速发展&#xff0c;AI编程插件已成为提升开发者生产力的重要工具。CodeRider和GitHub Copilot作为市场上的领先者&#xff0c;分别以其独特的特性和生态系统吸引了大量开发者。本文将从功…...

VM虚拟机网络配置(ubuntu24桥接模式):配置静态IP

编辑-虚拟网络编辑器-更改设置 选择桥接模式&#xff0c;然后找到相应的网卡&#xff08;可以查看自己本机的网络连接&#xff09; windows连接的网络点击查看属性 编辑虚拟机设置更改网络配置&#xff0c;选择刚才配置的桥接模式 静态ip设置&#xff1a; 我用的ubuntu24桌…...

QT3D学习笔记——圆台、圆锥

类名作用Qt3DWindow3D渲染窗口容器QEntity场景中的实体&#xff08;对象或容器&#xff09;QCamera控制观察视角QPointLight点光源QConeMesh圆锥几何网格QTransform控制实体的位置/旋转/缩放QPhongMaterialPhong光照材质&#xff08;定义颜色、反光等&#xff09;QFirstPersonC…...

JS手写代码篇----使用Promise封装AJAX请求

15、使用Promise封装AJAX请求 promise就有reject和resolve了&#xff0c;就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...

08. C#入门系列【类的基本概念】:开启编程世界的奇妙冒险

C#入门系列【类的基本概念】&#xff1a;开启编程世界的奇妙冒险 嘿&#xff0c;各位编程小白探险家&#xff01;欢迎来到 C# 的奇幻大陆&#xff01;今天咱们要深入探索这片大陆上至关重要的 “建筑”—— 类&#xff01;别害怕&#xff0c;跟着我&#xff0c;保准让你轻松搞…...

pikachu靶场通关笔记19 SQL注入02-字符型注入(GET)

目录 一、SQL注入 二、字符型SQL注入 三、字符型注入与数字型注入 四、源码分析 五、渗透实战 1、渗透准备 2、SQL注入探测 &#xff08;1&#xff09;输入单引号 &#xff08;2&#xff09;万能注入语句 3、获取回显列orderby 4、获取数据库名database 5、获取表名…...