我正在使用XAMMP localhost进行项目:在Windows代码中可以完美执行,但是在Mac中,当我尝试裁剪图像时,类似的代码无法按预期工作。
PHP:business_logo.php
$output_dir = 'logo/';
if(isset($_FILES["my_business_logo"]))
{
$error =$_FILES["my_business_logo"]["error"];
{
if(!is_array($_FILES["my_business_logo"]['name'])) //single file
{
$RandomNum = time();
$ImageName = str_replace(' ','-',strtolower($_FILES['my_business_logo']['name']));
$ImageType = $_FILES['my_business_logo']['type']; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
$url = "$output_dir$NewImageName";
move_uploaded_file($_FILES["my_business_logo"]["tmp_name"],$output_dir.$NewImageName);
$check_query = "SELECT * FROM tbl_business_logo WHERE user_id='$pic_id'";
$query_check = mysqli_query($con, $check_query) or die(mysqli_error($con));
$no = mysqli_num_rows($query_check);
if($no == 0){
$query = "INSERT INTO tbl_business_logo(user_id,account_id,picture,url) VALUES('$pic_id','$account_id','$NewImageName','$url')";
mysqli_query($con, $query) or die(mysqli_error($con));
}else{
$query = "UPDATE tbl_business_logo SET picture='$NewImageName' , url='$url' WHERE user_id='$pic_id'";
mysqli_query($con, $query) or die(mysqli_error($con));
}
// $ret= $output_dir.$NewImageName;
$ret= '<center><img src="pages/settings/'.$output_dir.$NewImageName.'" style="width: 250px; height: 50px;" class="img-responsive"></center>';
}
}
echo $ret;
}
JavaScript
window.addEventListener('DOMContentLoaded', function () {
var avatar = document.getElementById('avatar');
var image = document.getElementById('my_business_logo');
var input = document.getElementById('logo_input');
var $progress = $('.progress');
var $progressBar = $('.progress-bar');
var $alert = $('.alert');
var $modal = $('#Logomodal');
var cropper;
$('[data-toggle="tooltip"]').tooltip();
input.addEventListener('change', function (e) {
var files = e.target.files;
var done = function (url) {
input.value = '';
image.src = url;
$alert.hide();
$modal.modal('show');
};
var reader;
var file;
var url;
if (files && files.length > 0) {
file = files[0];
if (URL) {
done(URL.createObjectURL(file));
} else if (FileReader) {
reader = new FileReader();
reader.onload = function (e) {
done(reader.result);
};
reader.readAsDataURL(file);
}
}
});
$modal.on('shown.bs.modal', function () {
cropper = new Cropper(image, {
dragMode: 'move',
aspectRatio: 5/1,
autoCropArea: 0.65,
restore: false,
guides: false,
center: false,
highlight: false,
cropBoxMovable: false,
cropBoxResizable: false,
toggleDragModeOnDblclick: false
});
}).on('hidden.bs.modal', function () {
cropper.destroy();
cropper = null;
});
document.getElementById('crop').addEventListener('click', function () {
var initialAvatarURL;
var canvas;
$modal.modal('hide');
if (cropper) {
canvas = cropper.getCroppedCanvas({
width: 250,
height: 50
});
initialAvatarURL = avatar.src;
avatar.src = canvas.toDataURL();
$progress.show();
$alert.removeClass('alert-success alert-warning');
canvas.toBlob(function (blob) {
var formData = new FormData();
var pic_id = document.getElementById("pic_id").value;
var account_id = document.getElementById("account_id").value;
// var imgfile= input.files[0];
formData.append('my_business_logo',blob, avatar);
formData.append('account_id',account_id);
formData.append('pic_id', pic_id);
$.ajax({
url: 'pages/settings/business_logo.php',
method: 'POST',
data: formData,
processData: false,
contentType: false,
xhr: function () {
var xhr = new XMLHttpRequest();
xhr.upload.onprogress = function (e) {
var percent = '0';
var percentage = '0%';
if (e.lengthComputable) {
percent = Math.round((e.loaded / e.total) * 100);
percentage = percent + '%';
$progressBar.width(percentage).attr('aria-valuenow', percent).text(percentage);
}
};
return xhr;
},
success: function (xhr) {
$alert.show().addClass('alert-success').text('Upload success');
$alert.append(xhr);
},
error: function () {
avatar.src = initialAvatarURL;
$alert.show().addClass('alert-warning').text('Upload error');
},
complete: function () {
$progress.hide();
}
});
});
}
});
});
预期输出:logo-1551522036.png,但在Mac上的实际输出为[object-htmlimageelement] -1555747488。[object-htmlimageelement]