等比例压缩图片到指定的KB大小
基本原理:
取原来的图片,长宽乘以比例,重新生成一张图片,获取这张图片的大小,如果还是超过预期大小,继续在此基础上乘以压缩比例,生成图片,直到达到预期
/**
* @获取远程图片的体积大小 单位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大小的更多相关文章
- C# 压缩图片到指定宽度,假如图片小于指定宽度 判断图片大小是否大于指定大小(KB) 如果大于则压缩图片质量 宽高不变
class Program { static void Main(string[] args) {//G:\zhyue\backup\projects\Test\ConsoleApplication1 ...
- 处理页面载入图片js(等比例压缩图片)
第一页面html <div class="admin">${answer.content}</div> <div class="admin ...
- 最新javascript自动按比例显示图片,按比例压缩图片显示
最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- java中判断图片格式并且等比例压缩图片
最近项目中需要判断上传的图片必须是png,jpg,gif三种格式的图片,并且当图片的宽度大于600px时,压缩图片至600px,并且等比例的压缩图片的高度. 具体的实现形式: 大致的思路是: 判断根据 ...
- php等比例压缩图片
<?php function resizeImage($im,$maxwidth,$maxheight,$name,$filetype) { $pic_width = imagesx($im); ...
- java上传并压缩图片(等比例压缩或者原尺寸压缩)
本文转载自http://www.voidcn.com/article/p-npjxrbxr-kd.html 先看效果: 原图:1.33M 处理后:27.4kb 关键代码; package codeGe ...
- Java实现的上传并压缩图片功能【可等比例压缩或原尺寸压缩】
本文实例讲述了Java实现的上传并压缩图片功能.分享给大家供大家参考,具体如下: 先看效果: 原图:1.33M 处理后:27.4kb 关键代码: package codeGenerate.util; ...
- ios压缩图片
/** * 压缩图片到指定文件大小 * * @param image 目标图片 * @param size 目标大小(最大值) * * @return 返回的图片文件 */ - (NSDat ...
- java 上传图片压缩图片
package com.bitspace.flame.util; import java.io.File; import java.awt.Image;import java.awt.image.Bu ...
随机推荐
- SQL数据库 开启时出现 数据库连接错误2,error:40的问题。如何解决
错误如下:(原因是sql server服务停止) 解决这个问题,就需要启动sql server服务:主要有三种方法: 一.(后台启动服务) 1.开始->控制面板: 2.管理工具 3.服务 4.把 ...
- android ListView子布局中按钮响应点击事件
只需要在子布局的根布局中添加以下属性即可: android:descendantFocusability="blocksDescendants"
- nullcon HackIM 2016 -- Crypto Question 2
Some one was here, some one had breached the security and had infiltrated here. All the evidences ar ...
- AJAX - onreadystatechange
[AJAX - onreadystatechange] 参考:http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_onreadystatechang ...
- Win32 OpenGL标准例子
在VS2008的MSDN中有一个标准的OpenGL例子,记录如下: /* * Example of a Win32 OpenGL program. * The OpenGL code is the s ...
- switch语句下的变量声明和定义
switch语句下的变量声明和定义的问题: switch...case...语句中存在声明和定义会出现一些问题.这个由switch语法特性决定的, switch中每个case都是平等的层次,区别于一般 ...
- c# NPOI 导出EXCEL
需要引入dll文件 也可以在NuGet里面管理(推荐) 比较方便 . using System; using System.Collections.Generic; using System.Linq ...
- Nginx的启动、停止与重启
启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c / ...
- 【Android端 APP GPU过度绘制】GPU过度绘制及优化
一.Android端的卡顿 Android端APP在具体使用的过程中容易出现卡顿的情况,比如查看页面时出现一顿一顿的感受,切换tab之后响应很慢,或者具体滑动操作的时候也很慢. 二.卡顿的原因 卡顿的 ...
- 使用miniui框架制作树形节点
<div id="leftTree" class="mini-outlooktree" url="<%=basePath%>mana ...