动态上传和编辑图像 - 图形化
upload and edit image dynamically - graphically
我需要在上传之前打开图像并以图形和动态方式对其进行编辑。首先,用户应该选择图像文件和界面返回文件的路径,然后用户应该看到预览,如果他/她想选择全部或一段图像进行上传。
javascript或jquery中是否有任何函数可以为我执行此操作,或者我是否必须使用插件,如果是,哪些插件?
There are many good jQuery plugins are available to crop image on the fly,
网页图片上传表格
<!-- image preview area-->
<img id="uploadPreview" style="display:none;"/>
<!-- image uploading form -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input id="uploadImage" type="file" accept="image/jpeg" name="image" />
<input type="submit" value="Upload">
<!-- hidden inputs -->
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
</form>
jQuery 和 JavaScript
// set info for cropping image using hidden fields
function setInfo(i, e) {
$('#x').val(e.x1);
$('#y').val(e.y1);
$('#w').val(e.width);
$('#h').val(e.height);
}
$(document).ready(function() {
var p = $("#uploadPreview");
// prepare instant preview
$("#uploadImage").change(function(){
// fadeOut or hide preview
p.fadeOut();
// prepare HTML5 FileReader
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
oFReader.onload = function (oFREvent) {
p.attr('src', oFREvent.target.result).fadeIn();
};
});
// implement imgAreaSelect plug in (http://odyniec.net/projects/imgareaselect/)
$('img#uploadPreview').imgAreaSelect({
// set crop ratio (optional)
aspectRatio: '1:1',
onSelectEnd: setInfo
});
});
.PHP:
<?php
$valid_exts = array('jpeg', 'jpg', 'png', 'gif');
$max_file_size = 200 * 1024; #200kb
$nw = $nh = 200; # image with & height
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ( isset($_FILES['image']) ) {
if (! $_FILES['image']['error'] && $_FILES['image']['size'] < $max_file_size) {
# get file extension
$ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
# file type validity
if (in_array($ext, $valid_exts)) {
$path = 'uploads/' . uniqid() . '.' . $ext;
$size = getimagesize($_FILES['image']['tmp_name']);
# grab data form post request
$x = (int) $_POST['x'];
$y = (int) $_POST['y'];
$w = (int) $_POST['w'] ? $_POST['w'] : $size[0];
$h = (int) $_POST['h'] ? $_POST['h'] : $size[1];
# read image binary data
$data = file_get_contents($_FILES['image']['tmp_name']);
# create v image form binary data
$vImg = imagecreatefromstring($data);
$dstImg = imagecreatetruecolor($nw, $nh);
# copy image
imagecopyresampled($dstImg, $vImg, 0, 0, $x, $y, $nw, $nh, $w, $h);
# save image
imagejpeg($dstImg, $path);
# clean memory
imagedestroy($dstImg);
echo "<img src='$path' />";
} else {
echo 'unknown problem!';
}
} else {
echo 'file is too small or large';
}
} else {
echo 'file not set';
}
} else {
echo 'bad request!';
}
?>
引用网址:http://www.w3bees.com/2013/08/image-upload-and-crop-with-jquery-and.html
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- 将JavaScript图形和图表转换为图像
- 如何在 D3 中创建工具提示,以便在图形中节点上的鼠标悬停上获取图像
- 动态上传和编辑图像 - 图形化
- 使用图形 API 检索照片的图像位置
- 在使用webgl、html5画布的情况下,使用图像实现三维图形
- 如何将保存的画布图形图像恢复到新的画布实例中
- Facebook自定义故事与大图像-打开图形与Javascript
- 在图像上放置图形以供选择
- 在人力车,jquery或d3.js中显示图像悬停图形
- D3.js下载图形为SVG图像
- Javascript:通过图形API从Canvas上传图像到FB
- Cytoscape按按钮将图形保存为图像
- 如何从Facebook图形响应图像的URL
- 关于获得Highstocks图形图像发送到服务器的建议
- 通过Javascript/Phonegap将base64图像上传到Facebook图形API
- 画布为画布中的不同图形应用不同的图像模式
- 将jqPlot图形保存到图像文件中
- 创建图形并导出为图像的库
- 使用Javascript和Facebook图形API的可点击图像