ajax上傳圖片到PHP并壓縮圖片顯示的方法
本文實(shí)例為大家分享了ajax上傳圖片到PHP并壓縮圖片顯示的具體代碼,供大家參考,具體內(nèi)容如下
需求就是,上傳圖片并壓縮圖片頁(yè)面效果如下圖:

HTML代碼
<div id="main"> <div class="demo"> <div class="btn btn-success"> <span>上傳圖片</span> <input id="fileupload" type="file" name="mypic"> </div> <!--加載進(jìn)度--> <div class="progress progress-striped"> <span class="progress-bar progress-bar-success bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style=""></span> <span class="percent">0%</span > </div> <!--顯示圖片--> <div id="showimg"></div> <!--刪除圖片--> <div class="files"></div> </div> </div>
CSS代碼和引入的bootstrap
<style type="text/css">
.demo{width:580px; margin:30px auto}
.btn{position: relative;overflow: hidden;margin-right: 4px;}
.btn input {position: absolute;top: 0; right: 0;margin: 0;border: solid transparent;opacity: 0;filter:alpha(opacity=0);}
.progress { position:relative; margin-left:100px; margin-top:-24px; width:200px; border-radius:3px; display:none}
.percent { position:absolute; top:1px; left:2%; color:#fff }
.files{margin:10px 0}
.delimg{margin-left:20px; color:#090; cursor:pointer;margin-top: -6px;}
</style>
<!--bootstrap.css3.3.7-->
<link rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
JS代碼
<!--jquery1.8.1-->
<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<!--圖片jquery.form.js-->
<script type="text/javascript" src="https://www.helloweba.net/demo/upload/jquery.form.js"></script>
<script type="text/javascript">
$(function () {
//進(jìn)度條百分比加載顏色
var bar = $('.bar');
//進(jìn)度條百分比
var percent = $('.percent');
//圖片顯示
var showimg = $('#showimg');
//進(jìn)度條
var progress = $(".progress");
//新增
var files = $(".files");
var btn = $(".btn span");
$(".demo").wrap("<form id='myupload' action='action.php' method='post' enctype='multipart/form-data'></form>");
//點(diǎn)擊上傳圖片
$("#fileupload").change(function(){
//提交表單
$("#myupload").ajaxSubmit({
dataType: 'json',
beforeSend: function() {
//顯示進(jìn)度條
progress.show();
//進(jìn)度條為0
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
btn.html("上傳中...");
},
//上傳進(jìn)度
uploadProgress: function(event, position, total, percentComplete) {
//進(jìn)度條加載長(zhǎng)度數(shù)據(jù)是number型
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
success: function(data) {
//上傳成功返回參數(shù)
files.html("<b>"+data.name+"("+data.size+"k)</b> <span class='delimg btn btn-danger' rel='"+data.pic+"'>刪除</span>");
showimg.html("<img src='"+data.pic+"'>");
btn.html("上傳圖片");
},
error:function(xhr){
//上傳失敗
btn.html("上傳失敗");
bar.width('0')
files.html(xhr.responseText);
},
clearForm: true
});
});
//刪除圖片js
$(".delimg").live('click',function(){
//獲取圖片地址
var pic = $(this).attr("rel");
$.post("action.php?act=delimg",{imagename:pic},function(msg){
if(msg=='delete'){
files.html("刪除成功.");
//刪除圖片效果
showimg.empty();
//隱藏進(jìn)度條
progress.hide();
}else{
alert(msg);
}
});
});
});
</script>
PHP代碼
<?php
date_default_timezone_set("PRC");
//引入圖片壓縮類(lèi)
require 'imgcompress.class.php';
//如果有數(shù)據(jù)就是當(dāng)前數(shù)據(jù),沒(méi)有為空
$action=isset($_GET['act']) ? $action = $_GET['act']:'';
$filename=isset($_POST['imagename']) ? $_POST['imagename']:'';
if($action=='delimg'){
if(!empty($filename)){
//刪除圖片
unlink($filename);
//向頁(yè)面回調(diào)參數(shù)
echo 'delete';
}else{
echo '刪除失敗.';
}
}else{
//獲取圖片名字和原數(shù)據(jù)
$picname = $_FILES['mypic']['name'];
//獲取圖片大小
$picsize = $_FILES['mypic']['size'];
if ($picname != "") {
/**
*
* 注釋代碼為是否限制用戶(hù)上傳圖片大小和用戶(hù)上傳文件格式
*/
// if ($picsize > 512000) { //限制上傳大小
// echo '圖片大小不能超過(guò)500k';
// exit;
// }
// $type = strstr($picname, '.'); //限制上傳格式
// if ($type != ".gif" && $type != ".jpg") {
// echo '圖片格式不對(duì)!';
// exit;
// }
// $rand = rand(100, 999);
// $pics = date("YmdHis") . $rand . $type; //命名圖片名稱(chēng)
//防止上傳圖片名中文亂碼
$name=iconv("UTF-8","gb2312", $picname);
//上傳路徑
$pic_path = "files/". $name;
//移動(dòng)圖片位置
move_uploaded_file($_FILES['mypic']['tmp_name'], $pic_path);
}
//圖片地址 拿到圖片地址可以傳遞到數(shù)據(jù)庫(kù)
$source = "files/". $picname;
$size = round($picsize/1024,2); //轉(zhuǎn)換成kb
$arr = array(
'name'=>$picname,
'pic'=>$source,
'size'=>$size
);
echo json_encode($arr); //輸出json數(shù)據(jù)
$dst_img = $picname;
$percent = 1; //原圖壓縮,不縮放
/**
* 方法一
* 壓縮圖片傳遞三個(gè)參數(shù)
* 1.資源文件
* 2.壓縮圖片質(zhì)量 1是最高,從0.1開(kāi)始
* 3.圖片壓縮名字
*/
(new Compress($source,$percent))->compressImg($dst_img);
/**
* 方法二
* 1.資源文件
* 2.壓縮圖片質(zhì)量
* 3.圖片名字
*/
// require 'image.class.php';
// $src = "001.jpg";
// $image = new Image($src);··············
// $image->percent = 0.2;
// $image->saveImage(md5("aa123"));
}
圖片壓縮類(lèi)請(qǐng)下載源碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
[ASP.NET AJAX]Function對(duì)象及Type類(lèi)的方法介紹
[ASP.NET AJAX]Function對(duì)象及Type類(lèi)的方法介紹...2007-01-01
Ajax疊加(Ajax返回?cái)?shù)據(jù)用Ajax發(fā)出)示例代碼
把參數(shù)用Ajax發(fā)送到數(shù)據(jù)庫(kù)進(jìn)行查詢(xún)?nèi)缓笥肁jax將數(shù)據(jù)發(fā)送到數(shù)據(jù)庫(kù)簽到表,于是有了下面的代碼,感興趣的朋友可以了解下,希望對(duì)大家學(xué)習(xí)ajax有所幫助2013-08-08
Ajax+smarty技術(shù)實(shí)現(xiàn)無(wú)刷新分頁(yè)
這篇文章主要介紹了Ajax+smarty技術(shù)實(shí)現(xiàn)無(wú)刷新分頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-03-03
Ajax 框架之SSM整合框架實(shí)現(xiàn)ajax校驗(yàn)
這篇文章主要介紹了Ajax 框架之SSM整合框架實(shí)現(xiàn)ajax校驗(yàn),需要的朋友可以參考下2017-04-04
Jquery Ajax請(qǐng)求方法小結(jié)(值得收藏)
本文給大家介紹jquery ajax請(qǐng)求方法小結(jié),jquery作為一個(gè)輕量級(jí)的js框架,能快速的開(kāi)發(fā)js應(yīng)用,并且在一定程度上改變了我們寫(xiě)js代碼的習(xí)慣,對(duì)jquery ajax請(qǐng)求感興趣的朋友參考下2015-11-11
使用ajax技術(shù)實(shí)現(xiàn)txt彈出在頁(yè)面上的方法
下面小編就為大家?guī)?lái)一篇使用ajax技術(shù)實(shí)現(xiàn)txt彈出在頁(yè)面上的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
通過(guò)Ajax請(qǐng)求動(dòng)態(tài)填充頁(yè)面數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇通過(guò)Ajax請(qǐng)求動(dòng)態(tài)填充頁(yè)面數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Ajax簡(jiǎn)單的異步交互及Ajax原生編寫(xiě)
一提到異步交互大家就會(huì)說(shuō)ajax,仿佛ajax這個(gè)技術(shù)已經(jīng)成為了異步交互的代名詞.那下面將研究ajax的核心對(duì)象2016-01-01

