使用PHP&和上传之前调整大小使用Javascript

时间:2014-08-11 13:19:24

标签: javascript php jquery html

在上传之前需要一些帮助来调整图片大小。

我正在使用一个非常出色的解决方案,当它在网站上运行时效果很好,但是第二个我的手机试图从图库或相机上传它会严重挤压图像。

HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Upload Photos</title>
<link rel="stylesheet" href="./style.css" />
<head>
<body>

<h1>Upload Photos</h1>

<form enctype="multipart/form-data"> 
<input type="file" accept="image/*" />
<div class="photos">

</div>
</form>

<script src="./upload.js"></script>

</body>
</html>

的Javascript

// Once files have been selected
document.querySelector('form input[type=file]').addEventListener('change', function(event){

// Read files
var files = event.target.files;

// Iterate through files
for (var i = 0; i < files.length; i++) {

    // Ensure it's an image
    if (files[i].type.match(/image.*/)) {

        // Load image
        var reader = new FileReader();
        reader.onload = function (readerEvent) {
            var image = new Image();
            image.onload = function (imageEvent) {

                // Add elemnt to page
                var imageElement = document.createElement('div');
                imageElement.classList.add('uploading');
                imageElement.innerHTML = '<span class="progress"><span></span></span>';
                var progressElement = imageElement.querySelector('span.progress span');
                progressElement.style.width = 0;
                document.querySelector('form div.photos').appendChild(imageElement);

                // Resize image
                var canvas = document.createElement('canvas'),
                    max_size = 1200,
                    width = image.width,
                    height = image.height;
                if (width > height) {
                    if (width > max_size) {
                        height *= max_size / width;
                        width = max_size;
                    }
                } else {
                    if (height > max_size) {
                        width *= max_size / height;
                        height = max_size;
                    }
                }
                canvas.width = width;
                canvas.height = height;
                canvas.getContext('2d').drawImage(image, 0, 0, width, height);

                // Upload image
                var xhr = new XMLHttpRequest();
                if (xhr.upload) {

                    // Update progress
                    xhr.upload.addEventListener('progress', function(event) {
                        var percent = parseInt(event.loaded / event.total * 100);
                        progressElement.style.width = percent+'%';
                    }, false);

                    // File uploaded / failed
                    xhr.onreadystatechange = function(event) {
                        if (xhr.readyState == 4) {
                            if (xhr.status == 200) {

                                imageElement.classList.remove('uploading');
                                imageElement.classList.add('uploaded');
                                imageElement.style.backgroundImage = 'url('+xhr.responseText+')';

                                console.log('Image uploaded: '+xhr.responseText);

                            } else {
                                imageElement.parentNode.removeChild(imageElement);
                            }
                        }
                    }

                    // Start upload
                    xhr.open('post', 'process.php', true);
                    xhr.send(canvas.toDataURL('image/jpeg'));

                }

            }

            image.src = readerEvent.target.result;

        }
        reader.readAsDataURL(files[i]);
    }

}

// Clear files
event.target.value = '';

});

PHP

<?php

// Generate filename
$filename = md5(mt_rand()).'.jpg';

// Read RAW data
$data = file_get_contents('php://input');

// Read string as an image file
$image = file_get_contents('data://'.substr($data, 5));

// Save to disk
if ( ! file_put_contents('images/'.$filename, $image)) {
header('HTTP/1.1 503 Service Unavailable');
exit();
}

// Clean up memory
unset($data);
unset($image);

// Return file URL
echo './images/'.$filename;

?>

任何有助于了解移动电话图像无法正常工作的帮助将不胜感激。

0 个答案:

没有答案