分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

上传图片时压缩图片 - 后端做法

发布时间:2023-09-06 02:12责任编辑:傅花花关键词:后端上传图片
 ???/** ????* 函数:调整图片尺寸或生成缩略图 v 1.1 ????* @param $Image 需要调整的图片(含路径) ????* @param $Dw 调整时最大宽度;缩略图时的绝对宽度 ????* @param $Dh 调整时最大高度;缩略图时的绝对高度 ????* @param $Type 1,调整尺寸; 2,生成缩略图 ????* @return bool ????*/ ???public function compressImg($image, $Dw, $Dh, $type) ???{ ???????if (!file_exists($image)) { ???????????return false; ???????} ???????// 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作) ???????// 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作) ???????if ($type != 1) { ???????????copy($image, str_replace(".", "_x.", $image)); ???????????$image = str_replace(".", "_x.", $image); ???????} ???????// 取得文件的类型,根据不同的类型建立不同的对象 ???????$ImgInfo = getimagesize($image); ???????switch ($ImgInfo[2]) { ???????????case 1: ???????????????$Img = @imagecreatefromgif($image); ???????????????break; ???????????case 2: ???????????????$Img = @imagecreatefromjpeg($image); ???????????????Break; ???????????case 3: ???????????????$Img = @imagecreatefrompng($image); ???????????????break; ???????} ???????// 如果对象没有创建成功,则说明非图片文件 ???????if (empty($Img)) { ???????????// 如果是生成缩略图的时候出错,则需要删掉已经复制的文件 ???????????if ($type != 1) { ???????????????unlink($image); ???????????} ???????????return false; ???????} ???????// 如果是执行调整尺寸操作则 ???????if ($type == 1) { ???????????$w = imagesx($Img); ???????????$h = imagesy($Img); ???????????$width = $w; ???????????$height = $h; ???????????if ($width > $Dw) { ???????????????$Par = $Dw / $width; ???????????????$width = $Dw; ???????????????$height = $height * $Par; ???????????????if ($height > $Dh) { ???????????????????$Par = $Dh / $height; ???????????????????$height = $Dh; ???????????????????$width = $width * $Par; ???????????????} ???????????} elseif ($height > $Dh) { ???????????????$Par = $Dh / $height; ???????????????$height = $Dh; ???????????????$width = $width * $Par; ???????????????if ($width > $Dw) { ???????????????????$Par = $Dw / $width; ???????????????????$width = $Dw; ???????????????????$height = $height * $Par; ???????????????} ???????????} else { ???????????????$width = $Dw; ???????????????$height = $Dh; ???????????} ???????????$nImg = imagecreatetruecolor($Dw, $Dh); // 新建一个真彩色画布 ???????????$white = imagecolorallocate($nImg, 255, 255, 255); ???????????// 填充白底色 ???????????imagefill($nImg, 0, 0, $white); ???????????if ($h / $w > $Dh / $Dw) { // 高比较大 ???????????????$width = $w * ($Dh / $h); ???????????????$IntNW = $Dw - $width; ???????????????$Dx = $IntNW / 2; ???????????????$Dy = 0; ???????????} else { // 宽比较大 ???????????????$height = $h * ($Dw / $w); ???????????????$IntNH = $Dh - $height; ???????????????$Dx = 0; ???????????????$Dy = $IntNH / 2; ???????????} ???????????imagecopyresampled($nImg, $Img, $Dx, $Dy, 0, 0, $width, $height, $w, $h); // 重采样拷贝部分图像并调整大小 ???????????imagejpeg($nImg, $image); // 以JPEG格式将图像输出到浏览器或文件 ???????????return true; ???????} else { // 如果是执行生成缩略图操作则 ???????????$w = imagesx($Img); ???????????$h = imagesy($Img); ???????????$nImg = imagecreatetruecolor($Dw, $Dh); ???????????$white = imagecolorallocate($nImg, 255, 255, 255); ???????????// 填充白底色 ???????????imagefill($nImg, 0, 0, $white); ???????????if ($h / $w > $Dh / $Dw) { // 高比较大 ???????????????$width = $w * ($Dh / $h); ???????????????$IntNW = $Dw - $width; ???????????????imagecopyresampled($nImg, $Img, $IntNW / 2, 0, 0, 0, $width, $Dh, $w, $h); ???????????} else { // 宽比较大 ???????????????$height = $h * ($Dw / $w); ???????????????$IntNH = $Dh - $height; ???????????????imagecopyresampled($nImg, $Img, 0, $IntNH / 2, 0, 0, $Dw, $height, $w, $h); ???????????} ???????????imagejpeg($nImg, $image); ???????????return true; ???????} ???}

需要注意的是: 如果是要生成宽高相等的图片,输出到页面的时候也要设置宽高等比例的

上传图片时压缩图片 - 后端做法

原文地址:https://www.cnblogs.com/pyspang/p/9567382.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved