基本原理:

取原来的图片,长宽乘以比例,重新生成一张图片,获取这张图片的大小,如果还是超过预期大小,继续在此基础上乘以压缩比例,生成图片,直到达到预期

     /**
* @获取远程图片的体积大小 单位byte
* @date 2016/9/23
* @author Jimmy
* @param $uri
* @param string $user
* @param string $pw
* @return string
*/
public static function remoteFilesize($uri, $user='', $pw='')
{
ob_start();
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
if (!empty($user) && !empty($pw)) {
$headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
preg_match($regex, $head, $matches);
return isset($matches[1]) ? $matches[1] : 'unknown';
}
     /**
* @desc 等比例压缩图片到指定KB大小
* @author Jimmy
* @date 2016/9/26
* @param $url 远程图片地址
* @param $maxSize 需要压缩的最终适合的大小KB
* @param $dirPath 文件存放的目录
* @param float $ratio 文件压缩系数(大于0小于1),该值越大,得到的压缩图片约接近指定的KB大小
* @return bool|string 压缩后文件存放路径
*/
public static function compressImageToSize($url,$maxSize,$dirPath,$ratio=0.9){
$fileLength=UtilityHelper::remoteFilesize($url);
$originSize=getimagesize($url);
$originWidth=$originSize[0];
$originHeight=$originSize[1];
$varWidth = $originWidth;
$varHeight = $originHeight;
if(!file_exists($dirPath)){
mkdir($dirPath);
}
$desPath = $dirPath. Uuid::createUuid().'.jpg';
if($fileLength/1024.0>$maxSize){//需要等比例压缩处理
while(true){
$currentWidth = intval($varWidth*$ratio);
$currentHeight = intval($varHeight*$ratio);
$varWidth = $currentWidth;
$varHeight = $currentHeight;
//生成缩略图片
$im=imagecreatefromjpeg($url);
$tn=imagecreatetruecolor($currentWidth, $currentHeight);
imagecopyresampled($tn, $im, 0, 0, 0, 0, $currentWidth, $currentHeight, $originWidth, $originHeight);
file_exists($desPath)&&unlink($desPath);
imagejpeg($tn, $desPath,100);
imagedestroy($tn);
$length=filesize($desPath)/1024.0;
if($length<$maxSize){
break;
}
}
}else{
file_put_contents($desPath,file_get_contents($url),true);
}
if(empty($desPath)){
return false;
}else{
return $desPath;
}
}

等比例压缩图片到指定的KB大小的更多相关文章

  1. C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变

    class Program { static void Main(string[] args) {//G:\zhyue\backup\projects\Test\ConsoleApplication1 ...

  2. 处理页面载入图片js(等比例压缩图片)

    第一页面html  <div class="admin">${answer.content}</div> <div class="admin ...

  3. 最新javascript自动按比例显示图片,按比例压缩图片显示

    最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  4. java中判断图片格式并且等比例压缩图片

    最近项目中需要判断上传的图片必须是png,jpg,gif三种格式的图片,并且当图片的宽度大于600px时,压缩图片至600px,并且等比例的压缩图片的高度. 具体的实现形式: 大致的思路是: 判断根据 ...

  5. php等比例压缩图片

    <?php function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) { $pic_width = imagesx($im); ...

  6. java上传并压缩图片(等比例压缩或者原尺寸压缩)

    本文转载自http://www.voidcn.com/article/p-npjxrbxr-kd.html 先看效果: 原图:1.33M 处理后:27.4kb 关键代码; package codeGe ...

  7. Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】

    本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...

  8. ios压缩图片

    /** *  压缩图片到指定文件大小 * *  @param image 目标图片 *  @param size  目标大小(最大值) * *  @return 返回的图片文件 */ - (NSDat ...

  9. java 上传图片压缩图片

    package com.bitspace.flame.util; import java.io.File; import java.awt.Image;import java.awt.image.Bu ...

随机推荐

  1. VC++ 简单的打印功能(对话框模式下)

    不多说,直接上代码 void CParamDlg::OnBnClickedButton6() { // TODO: 在此添加控件通知处理程序代码 CDC dc; CPrintDialog dlg(TR ...

  2. 大毕设-MATLAB-FFT实现

    引用来自:http://blog.csdn.net/sinwel/article/details/8115673 %仿真参数中的含义 % Ts 表示间隔Ts时间采样,这个越小越接近连续信号,而实际上不 ...

  3. Linuxmint&win7

    一.选择好Linux发行版,并下载ISO镜像.Linux发行版众多,然而主流的无非Linuxmint.Ubuntu.Fedora.Debian等,当然国内的Deepin也不错.小编旧文已专门就&quo ...

  4. linux 账号管理与ACL权限设定

    此文涉及命令:useradd.usermod.userdel.passwd.chage.setfacl.getfacl.su.sudo.fingr.chfn.chsh.id.groupadd.grou ...

  5. ASP.NET5 Beta8可用性

    ASP.NET5 beta8现已上都的NuGet作为一个工具升级到Visual Studio2015!此版本极大地扩展.NET核心对OS X和Linux所支持的范围.您现在可以使用网络,加密和全球化特 ...

  6. Zookeeper-Zookeeper可以干什么

    在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...

  7. Redis GetTypedClient

    C#操作Redis   未包含GetTypedClient定义 原创,转载请注明出处. VS版本:2015 用NuGet安装了Redis的包 按照百度的各种教程来操作,但是发现网上下载的DEMO可以正 ...

  8. 使用SVN同步资源后图标样式的详细解读

    项目视图   The Package Explorer view - 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽 ...

  9. java多线程详解(8)-volatile,Atomic比较

    在变成过程中我们需要保证变量的线程安全,在java中除了使用锁机制或者Threadlocal等保证线程安全,还提供了 java.util.concurrent.atomic.Atomic*(如Atom ...

  10. 终端 git log 修改样式

    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d ...