当前位置: 首页 > 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;}}

http://www.lryc.cn/news/337861.html

相关文章:

  • Android 14.0 SystemUI修改状态栏电池图标样式为横屏显示
  • FPGA:图像数字细节增强算法(工程+仿真+实物,可用毕设)
  • Android netty的使用
  • 苹果电脑启动磁盘是什么意思 苹果电脑磁盘清理软件 mac找不到启动磁盘 启动磁盘没有足够的空间来进行分区
  • 【Java SE】多态
  • Yarn vs npm的大同小异Yarn是什么?
  • 1.Godot引擎|场景|节点|GDS|介绍
  • springboot3 redis 实现分布式锁
  • 2024年第十四届MathorCup数学应用挑战赛A题思路分享(妈妈杯)
  • 运动听歌哪款耳机靠谱?精选五款热门开放式耳机
  • Kubernetes学习笔记12
  • Qt Designer 控件箱中的控件介绍及布局比列分配
  • 蓝桥集训之三国游戏
  • MySQL知识整理
  • 代码随想录算法训练营第36天| 435. 无重叠区间、 763.划分字母区间*、56. 合并区间
  • SpringBoot整合Nacos
  • vue3 浅学
  • 三小时使用鸿蒙OS模仿羊了个羊,附源码
  • 如何使用 ArcGIS Pro 制作热力图
  • SpringBoot之集成Redis
  • mybatis-plus与mybatis同时使用别名问题
  • MySQL基础知识——MySQL日志
  • uniapp 地图分幅网格生成 小程序基于map组件
  • python项目练习——22、人脸识别软件
  • Linux中账号登陆报错access denied
  • python语言之round(num, n)小数四舍五入
  • 安全风险攻击面管理如何提升企业网络弹性?
  • 常用的几款性能测试软件
  • 谷歌google浏览器无法更新Chrome至最新版本怎么办?浏览器Chrome无法更新至最新版本
  • 认识异常(1)