/**
* 函数:调整图片尺寸或生成缩略图 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. .exe文件自动重启

    echo  :杀死进程taskkill /f /im YYTWEB.exe  :等待10秒:ping 127.0.0.1 -n 10  start "" "D:\都江堰银 ...

  2. Android动态加载布局之LayoutInflater【转】

    万分感谢大佬:https://www.jianshu.com/p/6a235ba5ee17 深入了解View<一>之Android LayoutInfalter原理分析 下文为:Layou ...

  3. Android 实用开源库(不定期更新)

    ZXing 极其好用的二维码开源库. GayHub:https://github.com/zxing/zxing MPAndroidChart MPAndroidChart 是 Android 一个强 ...

  4. aidl介绍

    (1)远程服务 运行在其他应用里面的服务     (2)本地服务 运行在自己应用里面的服务    (3)进行进程间通信  IPC   (4)aidl Android interface Definat ...

  5. PHP中英文混合字符串处理

    转载请注明来源:https://www.cnblogs.com/hookjc/ function cut_str($string, $sublen, $start = 0, $code = 'utf- ...

  6. 源码推荐 VVebo剥离的TableView绘制

    源码推荐 VVebo剥离的TableView绘制 https://github.com/johnil/VVeboTableViewDemo 此项目由VVebo剥离,希望你能通过这个demo看到我是如何 ...

  7. python官网导航翻译

  8. v79.01 鸿蒙内核源码分析(用户态锁篇) | 如何使用快锁Futex(上) | 百篇博客分析OpenHarmony源码

    百篇博客分析|本篇为:(用户态锁篇) | 如何使用快锁Futex(上) 进程通讯相关篇为: v26.08 鸿蒙内核源码分析(自旋锁) | 当立贞节牌坊的好同志 v27.05 鸿蒙内核源码分析(互斥锁) ...

  9. 日行一算(Vowel-大小写转换)

    题目 题目描述 solo从小就对英文字母非常感兴趣,尤其是元音字母(a,e,i,o,u,A,E,I,O,U),他在写日记的时候都会把元音字母写成大写的,辅音字母则都写成小写,虽然别人看起来很别扭,但是 ...

  10. prometheus监控java项目(jvm等):k8s外、k8s内

    前言 虽然可以使用jvisualvm之类的工具监控java项目,但是集群环境下,还是捉襟见肘,下面介绍如何用主流的prometheus来监控java项目. java项目配置 在pom.xml中添加依赖 ...