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;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/811310.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024山东大健康展会,济南生物医药展,中国医疗健康展

——中国&#xff08;济南&#xff09;国际大健康产业博览会China&#xff08;Jinan&#xff09;International Big Health Industry Expo&#xff08;China-DJK山东健博会&#xff09;&#xff0c;聚焦企业招商和宣传的定位&#xff0c;立足于济南新旧动能转换起步区&#xff…

HashMap部分底层源码解析

哈希表的物理结构 HashMap底层都是哈希表&#xff08;也称散列表&#xff09;&#xff0c;线程不安全&#xff0c;其中维护了一个长度为2的幂次方的Entry类型的数组table&#xff0c;数组的每一个索引位置被称为一个桶(bucket)&#xff0c;你添加的映射关系(key,value)最终都被…

腾讯云向量数据库-RAG介绍

1.说明 RAG结合LLM(通用大预言模型)构件基于私有文档、专业领域知识、实时信息的charbot。 2.RAG的主要步骤 知识切片成chunk向量化chunk入库query检索知识chunk构件prompts调用llm生成回答 3.优势 快速构件demo快速理解rag社区支持 4.痛点 投入大效果差调优难 5.RAG应…

结合SOCKS5代理、代理IP与网络安全促进跨界电商和游戏产业发展

一、引言 在全球化的大背景下&#xff0c;数字技术的应用已成为企业出海战略的重要组成部分。尤其是在跨界电商和游戏行业&#xff0c;通过有效利用SOCKS5代理、代理IP和网络安全技术&#xff0c;不仅可以突破地域限制&#xff0c;还能保障数据的安全和用户的隐私。本文旨在探讨…

一、flask入门和视图

run启动参数 模板渲染 后端给前端页面传参 前端页面设置css from flask import Flask, render_template,jsonify# 创建flask对象 app = Flask(__name__)# 视图函数 + 路由route @app.route("/") def hello_world():# 响应,返回给前端的数据return "hello worl…

多 线 程

1&#xff0e;什么是多线程? 有了多线程&#xff0c;我们就可以让程序同时做多件事情 2.多线程的作用? 提高效率 3&#xff0e;多线程的应用场景? 只要你想让多个事情同时运行就需要用到多线程 比如:软件中的耗时操作、所有的聊天软件、所有的服务器 1.进程和线程【理解】 …

Day36|贪心算法part05:435. 无重叠区间、763.划分字母区间、56. 合并区间

435. 无重叠区间 有了上题射气球的因子&#xff0c;这题也就有思路了&#xff0c;反正无脑排序就行了&#xff1a; 首先将所有区间按照end的大小从小到大排序&#xff1b;选取最早end为起始x_end遍历所有区间&#xff0c;如果该区间的start比end大&#xff08;可重叠&#xf…

活动预告|如何构建云原生现代化数据栈?北京首场 Meetup 来啦!

数字化时代带来了海量的数据涌现&#xff0c;传统的数据架构已然无法满足现代企业的需求&#xff0c;现代化数据栈应运而生。基于云原生的现代化数据栈具备了多云兼容的特性&#xff0c;在不同的云环境下能够保持高性能运作&#xff0c;使企业得以无缝地处理和分析海量的数据集…

vue 不同环境打包指令的配置(vue-cli2、vue-cli3、vite)

一个项目可能会有开发版本、上线版本、测试版本等等多个版本&#xff0c;不同的环境会有不同请求api接口&#xff0c;就需更改一些基本配置&#xff0c;这时候为不同环境配置不同的打包指令就很必要。 一、vue-cli2 1、安装 cross-env&#xff0c;它可以处理 windows 和其他 …

C语言题目:阶乘数列求和(函数)

题目描述 输入一个正数x和一个正整数n&#xff0c;求下列算式的值。要求定义两个调用函数&#xff1a;fact(n)计算n的阶乘&#xff1b;mypow(x,n)计算x的n次幂&#xff08;即xn&#xff09;&#xff0c;两个函数的返回值类型是double。 x - x2/2! x3/3! ... (-1)n-1xn/n! …

利用SARscape对日本填海造陆和天然气开采进行地表形变监测

日本千叶市&#xff0c;是日本南部重要的工业港市。位于西部的浦安市是一个典型的"填海造田"城市&#xff0c;东南部的东金区有一片天然气开采区域&#xff0c;本文利用SARscape&#xff0c;用干涉叠加的方法&#xff0c;即PS和SBAS&#xff0c;对这两个区域进行地表…

倒计时4天!百度Create AI开发者大会“大模型与深度学习技术”论坛亮点抢鲜看!

作为人工智能的核心基础技术&#xff0c;深度学习具有很强的通用性&#xff0c;大模型技术在深度学习的基础上&#xff0c;通过构建更加庞大神经网络模型和应用transformer等更加领先的算法&#xff0c;使模型的处理能力产生质的飞跃。飞桨&#xff08;PaddlePaddle&#xff09…

MySQL分区表(14/16)

分区表 基本概述 分区表是数据库中一种用于优化大型表数据管理和查询性能的技术。它将一个表的数据根据特定的规则或条件分割成多个部分&#xff0c;每个部分称为一个分区。每个分区可以独立于其他分区进行存储、管理和查询&#xff0c;这样可以提高数据处理的效率&#xff0…

VS Code中“@“符号如何自动补全导入路径

一、下载 Path Intellisense 插件 二、打开设置&#xff0c;在扩展中选择该插件&#xff0c;点击setting.json 三、添加配置&#xff1a; "":"${workspaceRoot}/src" 如图&#xff1a; 四、在项目src目录中新建jsconfig.json文件 &#xff08;一定要是src目…

动态规划(背包问题)

一:动态规划概述: 动态规划实际上是一种将原本的 大 方面的问题转化为许许多多的 小方面 的一种应用, 在一定程度上避免数据的重复, 并且能够将数据以自己希望的方式进行存储, 用来解决多阶段的数学问题, 从而提高算法的效率 在算法当中, 动态规划主要包括有: 递推, 线性DP 记忆…

【Java核心技术】第3章 Java的基本程序设计结构

1 数据类型 Java一共有8种数据类型&#xff1a; 4种整型 类型存储需求int4字节short2字节long8字节byte1字节 2种浮点型 类型存储需求float4字节double8字节 1种字符型 1种布尔型 2 变量声明 2.1 局部类型推断 如果可以从变量的初始值推断变量类型&#xff0c;只需要使用…

全量知识系统 程序详细设计 之 三种“活物” 之1(QA百度搜索 )

Q1. 今天聊聊 全知系统中 三种“活物”。先从他们的一个简单描述开始&#xff1a; 自主&#xff1a;计算机“集群”的“沉”与“浮”&#xff1b; 自然&#xff1a;AI “众生”的“世”和“界” &#xff1b;自由&#xff1a;人类 “公民”的“宇”或“宙”。 全知系统中的三…

【数组】5螺旋矩阵

这里写自定义目录标题 一、题目二、解题精髓-循环不变量三、代码 一、题目 给定⼀个正整数 n&#xff0c;⽣成⼀个包含 1 到 n^2 所有元素&#xff0c;且元素按顺时针顺序螺旋排列的正⽅形矩阵。 示例: 输⼊: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 二、解题精髓…

JVM参数列表

-client :设置JVM使用client模式,特点启动较快(神机不明显(I5/8G/SSD)) -server :设置JVM使用server模式。64位JDK默认启动该模式 -agentlib:libname[options] :用于加载本地的lib -agentlib:hprof :用于获取JVM的运行情况 -agentpath:pathnamep[options] :加载制定路径的本…

Day:007(1) | Python爬虫:高效数据抓取的编程技术(scrapy框架使用)

Scrapy的介绍 Scrapy 是一个用于抓取网站和提取结构化数据的应用程序框架&#xff0c;可用于各种有用的应用程序&#xff0c;如数据挖掘、信息处理或历史存档。 尽管 Scrapy 最初是为网络抓取而设计的&#xff0c;但它也可用于使用API提取数据或用作通用网络爬虫。 Scrapy的优势…