/**
* 函数:调整图片尺寸或生成缩略图 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. python程序语法元素分析

    #TemConvert.py TempStr = input("请输入带有符号的温度值:") if TempStr[-1] in ['F', 'f']: C = (eval(Tem ...

  2. ELK、ELFK企业级日志分析系统

    ELK.ELFK企业级日志分析系统 目录 ELK.ELFK企业级日志分析系统 一.ELK日志分析系统 1. ELK简介 1.2 ElasticSearch 1.3 Logstash 1.4 Kiban ...

  3. Nginx中的Location和Rewrite

    Nginx中的Location和Rewrite 目录 Nginx中的Location和Rewrite 一.location 1. location的分类 2. location常用的匹配规则 3. l ...

  4. PCI Verilog IP

    1      PCI IP设计 虽然PCI已经逐渐淘汰,但是还是有不少应用需要这样的接口通讯. 设计目的是为了提供基于源码的PCI IP,这样硬件就不必受限于某一个FPGA型号,也方便ASIC迁移.由 ...

  5. Lesson14——NumPy 字符串函数之 Par3:字符串信息函数

    NumPy 教程目录 1 字符串信息函数 1.1 numpy.char.count char.count(a, sub, start=0, end=None) 返回一个数组,其中包含 [start, ...

  6. Bootstrap提供的CDN服务标签与下载文档

    目录 1.引入Bootstrap提供的CDN服务 1.选择下载Bootstrap CDN 二:下载Bootstrap官方文档 1.进入Bootstrap官网,选择3版本中文档. 1.引入Bootstr ...

  7. Solution -「CF 1391E」Pairs of Pairs

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的无向图,在其上找到一条包括不少于 \(\lceil\frac{n}2\rceil\ ...

  8. IDEA 2021 没有Allow parallel run

    IDEA 2021 没有Allow parallel run 尝试运行多个客户端. 新版IDEA找不到Allow parallel run

  9. macbook安装scala、hadoop、saprk环境

    一.scala安装 1. 安装jdk 有mac专用的jdk安装包,这里下载安装jdk1.8 2. 安装scala 2.1下载scala 2.2解压到指定目录 tar -zxvf /Users/lode ...

  10. Realtime Data Processing at Facebook

    概要 这篇论文发表于2016年,主要是介绍Facebook内部的流式计算平台的设计与思考,对于流式计算的关键特性的实现选型上进行深度对比分析. 流式计算系统5个衡量指标 文中提到有5个重要的考量部分 ...