html5+php调用android手机图片,html5+exif.js+canvas+php实现手机上传图片,图片损坏无法打开...
上傳圖片,圖片損壞無法打開,圖片路徑也是正確的,
function selectFileImage(fileObj) {
var file = fileObj.files['0'];
//圖片方向角 added by lzk
var Orientation = null;
if (file) {
console.log("正在上傳,請稍后...");
var rFilter = /^(image\/jpeg|image\/png)$/i; // 檢查圖片格式
if (!rFilter.test(file.type)) {
//showMyTips("請選擇jpeg、png格式的圖片", false);
return;
}
// var URL = URL || webkitURL;
//獲取照片方向角屬性,用戶旋轉控制
EXIF.getData(file, function() {
// alert(EXIF.pretty(this));
EXIF.getAllTags(this);
//alert(EXIF.getTag(this, 'Orientation'));
Orientation = EXIF.getTag(this, 'Orientation');
//return;
});
var oReader = new FileReader();
oReader.onload = function(e) {
//var blob = URL.createObjectURL(file);
//_compress(blob, file, basePath);
var image = new Image();
image.src = e.target.result;
image.onload = function() {
var expectWidth = this.naturalWidth;
var expectHeight = this.naturalHeight;
if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
expectWidth = 800;
expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
} else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
expectHeight = 1200;
expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
}
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = expectWidth;
canvas.height = expectHeight;
ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
var base64 = null;
//修復ios
if (navigator.userAgent.match(/iphone/i)) {
console.log('iphone');
//alert(expectWidth + ',' + expectHeight);
//如果方向角不為1,都需要進行旋轉 added by lzk
if(Orientation != "" && Orientation != 1){
alert('旋轉處理|'+Orientation);
switch(Orientation){
case 6://需要順時針(向左)90度旋轉
alert('需要順時針(向左)90度旋轉');
rotateImg(this,'left',canvas);
break;
case 8://需要逆時針(向右)90度旋轉
alert('需要順時針(向右)90度旋轉');
rotateImg(this,'right',canvas);
break;
case 3://需要180度旋轉
alert('需要180度旋轉');
rotateImg(this,'right',canvas);//轉兩次
rotateImg(this,'right',canvas);
break;
}
}
/*var mpImg = new MegaPixImage(image);
mpImg.render(canvas, {
maxWidth: 800,
maxHeight: 1200,
quality: 0.8,
orientation: 8
});*/
base64 = canvas.toDataURL("image/jpeg", 0.8);
}else{
if(Orientation != "" && Orientation != 1){
//alert('旋轉處理');
switch(Orientation){
case 6://需要順時針(向左)90度旋轉
alert('需要順時針(向左)90度旋轉');
rotateImg(this,'left',canvas);
break;
case 8://需要逆時針(向右)90度旋轉
alert('需要順時針(向右)90度旋轉');
rotateImg(this,'right',canvas);
break;
case 3://需要180度旋轉
alert('需要180度旋轉');
rotateImg(this,'right',canvas);//轉兩次
rotateImg(this,'right',canvas);
break;
}
}
base64 = canvas.toDataURL("image/jpeg", 0.8);
}
// var str="
";// $(".img_add").append(str);
// uploadImage(base64);
$('#myupload').ajaxSubmit({
data:{'img':base64},
dataType: 'json',
success: function (data) {
if(data.error!='0'){
alert(data.error);
}
if(data.pic){
var pic=data.pic;
if (pic.substring(0,1)=='.'){
s=pic.substring(1);
}
var str="
";$(".img_add").append(str);
}
},
error: function () {
alert('上傳失敗');
},
})
$("#img_add").attr("src", base64);
};
};
oReader.readAsDataURL(file);
}
}
php上傳服務器部分
public function uploadimg(){
$opensession=$this->opensession;
$path = './Public/Uploads/Friends/'.$opensession['openid']."_".$_POST['openid']."/";//上傳路徑
if (isset($_POST)) {
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$pic_name = time() . rand(10000, 99999) . ".png" ;//圖片名稱
$pic_url = $path . $pic_name;//上傳后圖片路徑+名稱
if (file_put_contents($pic_url, $data)) { //臨時文件轉移到目標文件夾
echo json_encode(array("error"=>"0","pic"=>$pic_url,"name"=>$pic_name));
} else {
echo json_encode(array("error"=>"上傳失敗!"));
}
}
}
大神 ,請問這是什么原因
總結
以上是生活随笔為你收集整理的html5+php调用android手机图片,html5+exif.js+canvas+php实现手机上传图片,图片损坏无法打开...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: code blocks c语言,Code
- 下一篇: ubuntu rpm安装_为什么说Ubu