我正在尝试在html,jquery ajax和php中创建一个图库。到目前为止它的工作率为90%我会说,图像被上传到一个特定的文件夹并保存到数据库中,但是当我同时从6张图片开始上传时,我遇到了一些问题,有些图片调整得非常小而不是采取我在代码中给出的值。
这是我的HTML:
<form id="upload-images-form" enctype="multipart/form-data">
<input type="file" name="imagesinput[]" id="imagesinput" multiple="multiple" accept="image/jpeg">
<input id="upload-images-button" class="btn btn-success" name="submit" type="submit" value="Subir"/>
</form>
我的jquery:
$('#upload-images-form').on("submit", function(e){
e.preventDefault();
console.log("uploading images");
var formData = new FormData(this);
var request = $.ajax({
method: "POST",
url: "upload_gallery/",
cache: false,
processData: false,
contentType: false,
data: formData,
xhr: function() {
var xhr = $.ajaxSettings.xhr();
progressBar.parent().css('display','block');
if (xhr.upload) {
xhr.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
//update progressbar
progressBar.css("width", + percent +"%");
progressBar.text(percent +"%");
}, true);
}
return xhr;
},
mimeType:"multipart/form-data"
});
request.done(function( msg ) {
console.log(msg);
});
request.fail(function( jqXHR, textStatus ) {
console.log( "Request failed: " + textStatus );
});
});
最后我的PHP,我处理和验证图像;
$images = $_FILES['imagesinput'];
$responses = array();
$resized_width = 1000; //new max-width for images
$resized_height = 700; // new max-height for images
if(isset($_FILES['imagesinput'])){
$img_desc = reArrayFiles($images); // function to help handling the array coming from ajax
$i = 0;
$len = count($img_desc);
foreach($img_desc as $val){ // array loop
if($val['size'] <= 5 * 1024 * 1024){ // size validation
if($val['type'] == "image/jpeg" || $val['type'] == "image/jpg"){ // format validation
if(!is_dir(ROOT_URI.'/gallery/user_id_'.$id)){ // check if folder exist
mkdir(ROOT_URI.'/gallery/user_id_'.$id, 0777);
}
$total_files = new FilesystemIterator('../../gallery/user_id_'.$id); // limit images
if(iterator_count($total_files) === 20){
array_push($responses, 'alert_total_files');
echo json_encode($responses);
die;
}
list($original_width, $original_height) = getimagesize($val['tmp_name']); // get image size
$original_ratio = $original_width/$original_height;
if ($resized_width / $resized_height > $original_ratio) {
$resized_width = $resized_height * $original_ratio;
} else {
$resized_height = $resized_width / $original_ratio;
}
// resize image(proportional)
$image_p = imagecreatetruecolor($resized_width, $resized_height);
$resized_image = imagecreatefromjpeg($val['tmp_name']);
imagecopyresampled($image_p, $resized_image, 0, 0, 0, 0, $resized_width, $resized_height, $original_width, $original_height);
// verificar de que la imagen no existe, si existe, no se sube
if(!file_exists('../../gallery/user_id_'.$id.'/'.$val['name'])){
$conn = mysqli_connect($servername, $username, $password, $db);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
imagejpeg($image_p, ROOT_URI.'/local_gallery/local_id_'.$id.'/'.$val['name']);
$path_url = BASE_URI.'app/gallery/user_id_'.$id.'/'.$val['name'];
$insert = mysqli_prepare($conn, "INSERT INTO user_gallery (id_user, path_url) VALUES (?, ?)");
mysqli_stmt_bind_param($insert, "ss", $id, $path_url);
if(mysqli_stmt_execute($insert)){
array_push($responses, 'alertsuccess');
}
imagedestroy($image_p);
}
else{
array_push($responses, 'file_exists');
}
}
else{
array_push($responses, 'alertformat');
}
}
else{
array_push($responses, 'alertsize');
}
if($i == $len - 1){
echo json_encode($responses);
}
$i++;
}
}
function reArrayFiles($file){
$file_ary = array();
$file_count = count($file['name']);
$file_key = array_keys($file);
for($i=0;$i<$file_count;$i++){
foreach($file_key as $val){
$file_ary[$i][$val] = $file[$val][$i];
}
}
return $file_ary;
}
我正在试图理解可能是什么问题,我只是觉得可能是因为循环太快而无法提供正确的值,所以它仍然需要来自普通图像的旧值?
一些线索?我真的很感激,谢谢。