资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

html5图片压缩,html图片缩放效果

怎么用JavaScript在线压缩图片

主要用了两个html5的 API,一个file,一个canvas,压缩主要使用cnavas做的,file是读取文件,之后把压缩好的照片放入内存,最后内存转入表单下img.src,随着表单提交。

为忻城等地区用户提供了全套网页设计制作服务,及忻城网站建设行业解决方案。主营业务为网站建设、做网站、忻城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

照片是自己用单反拍的,5M多,压缩下面3张分别是600多kb,400多kb,300kb的最后那张失真度很大了,压缩效率蛮高的。

!DOCTYPE html

htmlhead meta charset="utf-8"/ titleFile API Test/title script type="text/javascript" src="js/jquery-1.11.0.min.js"/script script type="text/javascript" src="js/JIC.js"/script style #test{ display: none; } /style/headbodyinput type="file" id="fileImg" form img src="" id="test" alt=""/formscript function handleFileSelect (evt) { // var filebtn = document.getElementById(id); // console.log(filebtn); // var files = filebtn.target.files; // console.log(filebtn.target); // console.log(files); var files = evt.target.files; for (var i = 0, f; f = files[i]; i++) { // Only process image files. if (!f.type.match('image.*')) { continue; } var reader = new FileReader(); // Closure to capture the file information. reader.onload = (function(theFile) { return function(e) { // Render thumbnail. // console.log(evt.target.files[0]); // console.log(e.target); console.log(e.target.result); var i = document.getElementById("test"); i.src = event.target.result; console.log($(i).width()); console.log($(i).height()); $(i).css('width',$(i).width()/10+'px'); //$(i).css('height',$(i).height()/10+'px'); console.log($(i).width()); console.log($(i).height()); var quality = 50; i.src = jic.compress(i,quality).src; console.log(i.src); i.style.display = "block"; }; })(f); // Read in the image file as a data URL. reader.readAsDataURL(f); } } document.getElementById('fileImg').addEventListener('change', handleFileSelect, false);/script/body/html

var jic = { /** * Receives an Image Object (can be JPG OR PNG) and returns a new Image Object compressed * @param {Image} source_img_obj The source Image Object * @param {Integer} quality The output quality of Image Object * @return {Image} result_image_obj The compressed Image Object */ compress: function(source_img_obj, quality, output_format){ var mime_type = "image/jpeg"; if(output_format!=undefined output_format=="png"){ mime_type = "image/png"; } var cvs = document.createElement('canvas'); //naturalWidth真实图片的宽度 cvs.width = source_img_obj.naturalWidth; cvs.height = source_img_obj.naturalHeight; var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0); var newImageData = cvs.toDataURL(mime_type, quality/100); var result_image_obj = new Image(); result_image_obj.src = newImageData; return result_image_obj; }, function ****(***)

html5交作业的时候用的是zip格式的压缩包 老师收到的时候我的css文件跟图片音乐全没了?为啥

估计不是没了,是你的html中对图片文件的引用使用了类似绝对路径的写法,对方接收后,放置的路径与你的不一样,造成读取失败。

先检查一下是不是真的把图片发过去了,再检查图片url是不是写对了。

html5怎么压缩图片

HTML是用来做网站的一种语言哈,就是在html里面改变图片的大小就要改变文件代码,打开图片源代码,图片文件的大小是height,和宽,我们可以更改,在语言中我们需要设置的都是英文的。

现在压缩工具将图片缩小之后都会对画质有影响,压缩图片文件选择压缩工具页面中的普通压缩就可以了压缩程度不要过大,找到图片压缩工具,图片要放置在工具页面上进行数据分析,根据图片的大小工具会制定压缩方案。

图片分享论坛却只允许发几百KB的文件;微信、分享给朋友的时候自动压缩的图像都比较模糊

PHP、HTML5上传图片自动压缩问题

给你个图片处理的类吧,图片剪裁处理后,也就等于将图片压缩了。

/**

* 图像处理类

* ============================================================================

* Copyright 2014 大秦科技,并保留所有权利。

* 网站地址: ;

* ============================================================================

*/

class Image{

//生成缩略图的方式

public $thumbType;

//缩略图的宽度

public $thumbWidth;

//缩略图的高度

public $thumbHeight;

//生成缩略图文件名后缀

public $thumbEndFix;

//缩略图文件前缀

public $thumbPreFix;

/**

* 构造函数

*/

public function __construct(){

$this-thumbType = 1;

$this-thumbWidth = 120;

$this-thumbHeight = 60;

$this-thumbPreFix ='';

$this-thumbEndFix =  '_thumb';

}

/**

* 检测是否为图像文件

* @param $img 图像

* @return bool

*/

private function check($img){

$type = array(".jpg", ".jpeg", ".png", ".gif");

$imgType = strtolower(strrchr($img, '.'));

return extension_loaded('gd')  file_exists($img)  in_array($imgType, $type);

}

/**

* 获得缩略图的尺寸信息

* @param $imgWidth 原图宽度

* @param $imgHeight 原图高度

* @param $thumbWidth 缩略图宽度

* @param $thumbHeight 缩略图的高度

* @param $thumbType 处理方式

* 1 固定宽度  高度自增 2固定高度  宽度自增 3固定宽度  高度裁切

* 4 固定高度 宽度裁切 5缩放最大边 原图不裁切

* @return mixed

*/

private function thumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType){

//初始化缩略图尺寸

$w = $thumbWidth;

$h = $thumbHeight;

//初始化原图尺寸

$cuthumbWidth = $imgWidth;

$cuthumbHeight = $imgHeight;

switch ($thumbType) {

case 1 :

//固定宽度  高度自增

$h = $thumbWidth / $imgWidth * $imgHeight;

break;

case 2 :

//固定高度  宽度自增

$w = $thumbHeight / $imgHeight * $imgWidth;

break;

case 3 :

//固定宽度  高度裁切

$cuthumbHeight = $imgWidth / $thumbWidth * $thumbHeight;

break;

case 4 :

//固定高度  宽度裁切

$cuthumbWidth = $imgHeight / $thumbHeight * $thumbWidth;

break;

case 5 :

//缩放最大边 原图不裁切

if (($imgWidth / $thumbWidth)  ($imgHeight / $thumbHeight)) {

$h = $thumbWidth / $imgWidth * $imgHeight;

} elseif (($imgWidth / $thumbWidth)  ($imgHeight / $thumbHeight)) {

$w = $thumbHeight / $imgHeight * $imgWidth;

} else {

$w = $thumbWidth;

$h = $thumbHeight;

}

break;

default:

//缩略图尺寸不变,自动裁切图片

if (($imgHeight / $thumbHeight)  ($imgWidth / $thumbWidth)) {

$cuthumbWidth = $imgHeight / $thumbHeight * $thumbWidth;

} elseif (($imgHeight / $thumbHeight)  ($imgWidth / $thumbWidth)) {

$cuthumbHeight = $imgWidth / $thumbWidth * $thumbHeight;

}

//            }

}

$arr [0] = $w;

$arr [1] = $h;

$arr [2] = $cuthumbWidth;

$arr [3] = $cuthumbHeight;

return $arr;

}

/**

* 图片裁切处理

* @param $img 原图

* @param string $outFile 另存文件名

* @param string $thumbWidth 缩略图宽度

* @param string $thumbHeight 缩略图高度

* @param string $thumbType 裁切图片的方式

* 1 固定宽度  高度自增 2固定高度  宽度自增 3固定宽度  高度裁切

* 4 固定高度 宽度裁切 5缩放最大边 原图不裁切 6缩略图尺寸不变,自动裁切最大边

* @return bool|string

*/

public function thumb($img, $outFile = '', $thumbWidth = '', $thumbHeight = '', $thumbType = ''){

if (!$this-check($img)) {

return false;

}

//基础配置

$thumbType = $thumbType ? $thumbType : $this-thumbType;

$thumbWidth = $thumbWidth ? $thumbWidth : $this-thumbWidth;

$thumbHeight = $thumbHeight ? $thumbHeight : $this-thumbHeight;

//获得图像信息

$imgInfo = getimagesize($img);

$imgWidth = $imgInfo [0];

$imgHeight = $imgInfo [1];

$imgType = image_type_to_extension($imgInfo [2]);

//获得相关尺寸

$thumb_size = $this-thumbSize($imgWidth, $imgHeight, $thumbWidth, $thumbHeight, $thumbType);

//原始图像资源

$func = "imagecreatefrom" . substr($imgType, 1);

$resImg = $func($img);

//缩略图的资源

if ($imgType == '.gif') {

$res_thumb = imagecreate($thumb_size [0], $thumb_size [1]);

$color = imagecolorallocate($res_thumb, 255, 0, 0);

} else {

$res_thumb = imagecreatetruecolor($thumb_size [0], $thumb_size [1]);

imagealphablending($res_thumb, false); //关闭混色

imagesavealpha($res_thumb, true); //储存透明通道

}

//绘制缩略图X

if (function_exists("imagecopyresampled")) {

imagecopyresampled($res_thumb, $resImg, 0, 0, 0, 0, $thumb_size [0], $thumb_size [1], $thumb_size [2], $thumb_size [3]);

} else {

imagecopyresized($res_thumb, $resImg, 0, 0, 0, 0, $thumb_size [0], $thumb_size [1], $thumb_size [2], $thumb_size [3]);

}

//处理透明色

if ($imgType == '.gif') {

imagecolortransparent($res_thumb, $color);

}

//配置输出文件名

$imgInfo = pathinfo($img);

$outFile = $outFile ? $outFile :dirname($img).'/'. $this-thumbPreFix . $imgInfo['filename'] . $this-thumbEndFix . "." . $imgInfo['extension'];

Files::create(dirname($outFile));

$func = "image" . substr($imgType, 1);

$func($res_thumb, $outFile);

if (isset($resImg))

imagedestroy($resImg);

if (isset($res_thumb))

imagedestroy($res_thumb);

return $outFile;

}

}

html5怎样调用手机摄像头或者相册

h2 class="title-detail"  

图片描述  

/h2  

input type="hidden" id="picIndex" value="0"  

div id='image-list' class="row image-list"  

!-- input id="upload_image" type="file" name="image" accept="image/*" / --  

div class="image-item space" onclick="showActionSheet()"  

div class="image-up"/div  

/div  

/div

function showPics(url,name){          

//根据路径读取到文件   

plus.io.resolveLocalFileSystemURL(url,function(entry){  

entry.file( function(file){  

var fileReader = new plus.io.FileReader();  

fileReader.readAsDataURL(file);  

span style="white-space:pre"   /span   fileReader.onloadend = function(e) {  

var picUrl = e.target.result.toString();  

var picIndex = $("#picIndex").val();  

var nowIndex = parseInt(picIndex)+1;  

$("#picIndex").val(nowIndex);  

var html = '';  

html += 'div class="image-item " id="item'+nowIndex+'"';  

html += 'div class="image-close" onclick="delPic('+nowIndex+')"X/div';  

html += 'divimg src="'+picUrl+'" class="upload_img" style="width:100%;height:100%;"//div';  

html += '/div';  

html += $("#image-list").html();  

$("#image-list").html(html);   

span style="white-space:pre"   /span}  

});  

});   

}  

//压缩图片    

function compressImage(url,filename){    

var name="_doc/upload/"+filename;  

plus.zip.compressImage({    

src:url,//src: (String 类型 )压缩转换原始图片的路径    

dst:name,//压缩转换目标图片的路径    

quality:40,//quality: (Number 类型 )压缩图片的质量.取值范围为1-100    

overwrite:true//overwrite: (Boolean 类型 )覆盖生成新文件    

},    

function(zip) {  

//页面显示图片  

showPics(zip.target,name);  

},function(error) {    

plus.nativeUI.toast("压缩图片失败,请稍候再试");    

});    

}   

//调用手机摄像头并拍照  

function getImage() {    

var cmr = plus.camera.getCamera();    

cmr.captureImage(function(p) {    

plus.io.resolveLocalFileSystemURL(p, function(entry) {    

compressImage(entry.toLocalURL(),entry.name);    

}, function(e) {    

plus.nativeUI.toast("读取拍照文件错误:" + e.message);    

});    

}, function(e) {    

}, {    

filter: 'image'   

});    

}  

//从相册选择照片  

function galleryImgs() {    

plus.gallery.pick(function(e) {    

var name = e.substr(e.lastIndexOf('/') + 1);  

compressImage(e,name);  

}, function(e) {    

}, {    

filter: "image"    

});    

}  

//点击事件,弹出选择摄像头和相册的选项  

function showActionSheet() {    

var bts = [{    

title: "拍照"    

}, {    

title: "从相册选择"    

}];    

plus.nativeUI.actionSheet({    

cancel: "取消",    

buttons: bts    

},    

function(e) {    

if (e.index == 1) {    

getImage();    

} else if (e.index == 2) {    

galleryImgs();    

}    

}    

);    

}

首先是html代码,很简单,就是给一个添加按钮,点击触发事件

其次方法共分为5个方法 看第二段JS代码

前端把图片压缩转换成base64编码把值赐予input='hidden' 之后提交给后台. HTML5,PHP,Javascript,canvas

缺点你无法直接验证传来的base64数据的完整性,比如大小,文件头之类的,还需要自己来实现。


网站栏目:html5图片压缩,html图片缩放效果
标题URL:http://cdkjz.cn/article/dsohgej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220