当我尝试从用户输入的图像中获取数据时,我遇到了同样的问题。
问题是我将base64编码的字符串存储到输入字段中。可能存在某种限制,可以容纳多少数据输入字段。
我使用textarea而不是输入字段,我的代码工作得很好!
试试这个:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get data from textarea
$data = $_POST['image'];
$data = explode("base64,", $data);
// Decode the string
$data = base64_decode($data[1]);
$im = imagecreatefromstring($data);
if ($im !== false) {
// Saves file
imagejpeg($im, 'image.jpg');
imagedestroy($im);
}
else {
// Show errors
echo 'An error occurred.';
}
}
?>
<!doctype html>
<html>
<head>
<title>Image</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<img src="" id="image-preview" style="width: 400px;" alt="">
<form action="" method="post">
<textarea name="image" id="image"></textarea>
<input type="submit" value="Make Image">
</form>
<input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" />
<script type="text/javascript">
// Get's the data from file field and shows it in img tag
function PreviewImage() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
oFReader.onload = function(oFREvent) {
document.getElementById("image-preview").src = oFREvent.target.result;
$("textarea").text(oFREvent.target.result);
};
};
</script>
</body>
</html>