/**
* 函数:调整图片尺寸或生成缩略图 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;
}
}

java上传图片时压缩图片的更多相关文章

  1. java 上传图片 并压缩图片大小

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  2. java 上传图片 并压缩图片大小(转)

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  3. java上传图片并压缩图片大小

    Thumbnailator 是一个优秀的图片处理的Google开源Java类库.处理效果远比Java API的好.从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生 ...

  4. 上传图片时压缩图片 - 前端(canvas)做法

    HTML前端代码: <?php $this->layout('head'); ?> <?php $this->layout('sidebar'); ?> <m ...

  5. java实现上传图片并压缩图片大小功能

    缩略图压缩文件jar包 <!-- 图片缩略图 --> <dependency> <groupId>net.coobird</groupId> <a ...

  6. Java使用 Thumbnails 压缩图片

    业务:用户上传一张图片到文件站,需要返回原图url和缩略图url 处理思路: 因为上传图片方法返回url是单个上传,第一步先上传原图并返回url 处理缩略图并上传:拿到MultipartFile压缩成 ...

  7. vue + vant 上传图片之压缩图片

    <van-uploader v-model="fileList" multiple :after-read="afterRead" :max-count= ...

  8. vue+element-ui上传图片时压缩大小

    第一种方法:需要安装一个模块 yarn add image-conversion --save <el-upload ref="upload" :data="dat ...

  9. java上传图片,把图片存到本地

    思路:js通过FileReader获取图片的Base64,Java解码用IO存到本地. HTML 代码 <input type="file" ng-model="f ...

随机推荐

  1. ubuntu 终端乱码

    转载请注明来源:https://www.cnblogs.com/hookjc/ 解决方法: 一. Ubuntu默认的中文字符编码 Ubuntu默认的中文字符编码为zh_CN.UTF-8, 这个可以在 ...

  2. Java 中使用正则表达式校检IP是否输入正确

    感谢大佬案例:https://www.jb51.net/article/114671.htm 正则表达式学习:(待办)近期总结

  3. getter/setter方法

    1.setter方法 作用:用来设置成员变量,可以在方法里面过滤掉一些不合理的值 命名规范: 必须是对象方法 返回值类型为void 方法名必须以set开头,而且后面跟上成员变量名去掉"_&q ...

  4. 使用java实现圆形运动

    1 package com.neuedu.test; 2 3 import java.awt.Graphics; 4 import java.awt.Image; 5 6 import com.neu ...

  5. Fastjson的JSONObject.toJSON()解析复杂对象发生内存泄漏问题

    这可能是fastjson的一个bug,我使用最新版依然存在该问题. 在用做报表功能的时候,发现一旦单元格过多,大概有80-100个单元格,就会发生程序假死,CPU持续占用超过90%,内存持续占用超90 ...

  6. Ansible之roles模块--lnmp分布式部署

    Ansible之roles模块--lnmp分布式部署 目录 Ansible之roles模块--lnmp分布式部署 1. role模块的作用 2. roles的目录结构 3. roles内个目录含义解释 ...

  7. LVS-DR群集

    LVS-DR群集 目录 LVS-DR群集 一.LVS-DR的工作原理 1. LVS-DR数据包流向分析 2. IP包头及数据帧头信息的变化 3. DR模式的特点 4.LVS-DR中的ARP问题 (1) ...

  8. 安装Linux8.3.2011

    镜像地址:http://mirrors.aliyun.com/centos/8.3.2011/isos/x86_64/ 非DVD镜像安装时的安装源地址:http://mirrors.aliyun.co ...

  9. B快速导航

    GETTING STARTED If you are new to Selenium, we have a few resources that can help you get up to spee ...

  10. 北京太速科技-第六代Intel i7四核八线程6U VPX主控板

    一.产品概述 该产品是一款基于第六代Intel i7四核八线程的高性能6U VPX刀片式计算机.产品提供了可支持全网状交换的高速数据通道,其中P1,P2各支持4个PCIe x4 Gen3总线接口,P3 ...