html
第一步,我们先画一个表头
现在再描绘主体部分
step1: please select image file
step2: please select a crop region
file size
type
image dimension
w
h
css
.bheader {
background-color: #dddddd;
border-radius: 10px 10px 0 0;
padding: 10px 0;
text-align: center;
}
.bbody {
color: #000;
overflow: hidden;
padding-bottom: 20px;
text-align: center;
background: -moz-linear-gradient(#ffffff, #f2f2f2);
background: -ms-linear-gradient(#ffffff, #f2f2f2);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffffff), color-stop(100%, #f2f2f2));
background: -webkit-linear-gradient(#ffffff, #f2f2f2);
background: -o-linear-gradient(#ffffff, #f2f2f2);
filter: progid:dximagetransform.microsoft.gradient(startcolorstr='#ffffff', endcolorstr='#f2f2f2');
-ms-filter: progid:dximagetransform.microsoft.gradient(startcolorstr='#ffffff', endcolorstr='#f2f2f2');
background: linear-gradient(#ffffff, #f2f2f2);
}
.bbody h2, .info, .error {
margin: 10px 0;
}
.step2, .error {
display: none;
}
.error {
font-size: 18px;
font-weight: bold;
color: red;
}
.info {
font-size: 14px;
}
label {
margin: 0 5px;
}
input {
border: 1px solid #cccccc;
border-radius: 10px;
padding: 4px 8px;
text-align: center;
width: 70px;
}
.jcrop-holder {
display: inline-block;
}
input[type=submit] {
background: #e3e3e3;
border: 1px solid #bbb;
border-radius: 3px;
-webkit-box-shadow: inset 0 0 1px 1px #f6f6f6;
box-shadow: inset 0 0 1px 1px #f6f6f6;
color: #333;
font: bold 12px/1 helvetica neue, helvetica, arial, sans-serif;
padding: 8px 0 9px;
text-align: center;
text-shadow: 0 1px 0 #fff;
width: 150px;
}
input[type=submit]:hover {
background: #d9d9d9;
-webkit-box-shadow: inset 0 0 1px 1px #eaeaea;
box-shadow: inset 0 0 1px 1px #eaeaea;
color: #222;
cursor: pointer;
}
input[type=submit]:active {
background: #d0d0d0;
-webkit-box-shadow: inset 0 0 1px 1px #e3e3e3;
box-shadow: inset 0 0 1px 1px #e3e3e3;
color: #000;
}
js
// convert bytes into friendly format
function bytestosize(bytes) {
var sizes = ['bytes', 'kb', 'mb'];
if (bytes == 0) return 'n/a';
var i = parseint(math.floor(math.log(bytes) / math.log(1024)));
return (bytes / math.pow(1024, i)).tofixed(1) + ' ' + sizes[i];
};
// check for selected crop region
function checkform() {
if (parseint($('#w').val())) return true;
$('.error').html('please select a crop region and then press upload').show();
return false;
};
// update info by cropping (onchange and onselect events handler)
function updateinfo(e) {
$('#x1').val(e.x);
$('#y1').val(e.y);
$('#x2').val(e.x2);
$('#y2').val(e.y2);
$('#w').val(e.w);
$('#h').val(e.h);
};
// clear info by cropping (onrelease event handler)
function clearinfo() {
$('.info #w').val('');
$('.info #h').val('');
};
function fileselecthandler() {
// get selected file
var ofile = $('#image_file')[0].files[0];
// hide all errors
$('.error').hide();
// check for image type (jpg and png are allowed)
var rfilter = /^(image\/jpeg|image\/png)$/i;
if (! rfilter.test(ofile.type)) {
$('.error').html('please select a valid image file (jpg and png are allowed)').show();
return;
}
// check for file size
if (ofile.size > 250 * 1024) {
$('.error').html('you have selected too big file, please select a one smaller image file').show();
return;
}
// preview element
var oimage = document.getelementbyid('preview');
// prepare html5 filereader
var oreader = new filereader();
oreader.onload = function(e) {
// e.target.result contains the dataurl which we can use as a source of the image
oimage.src = e.target.result;
oimage.onload = function () { // onload event handler
// display step 2
$('.step2').fadein(500);
// display some basic image info
var sresultfilesize = bytestosize(ofile.size);
$('#filesize').val(sresultfilesize);
$('#filetype').val(ofile.type);
$('#filedim').val(oimage.naturalwidth + ' x ' + oimage.naturalheight);
// create variables (in this scope) to hold the jcrop api and image size
var jcrop_api, boundx, boundy;
// destroy jcrop if it is existed
if (typeof jcrop_api != 'undefined')
jcrop_api.destroy();
// initialize jcrop
$('#preview').jcrop({
minsize: [32, 32], // min crop size
aspectratio : 1, // keep aspect ratio 1:1
bgfade: true, // use fade effect
bgopacity: .3, // fade opacity
onchange: updateinfo,
onselect: updateinfo,
onrelease: clearinfo
}, function(){
// use the jcrop api to get the real image size
var bounds = this.getbounds();
boundx = bounds[0];
boundy = bounds[1];
// store the jcrop api in the jcrop_api variable
jcrop_api = this;
});
};
};
// read selected file as dataurl
oreader.readasdataurl(ofile);
}
php
function uploadimagefile() { // note: gd library is required for this function
if ($_server['request_method'] == 'post') {
$iwidth = $iheight = 200; // desired image result dimensions
$ijpgquality = 90;
if ($_files) {
// if no errors and size less than 250kb
if (! $_files['image_file']['error'] && $_files['image_file']['size'] if (is_uploaded_file($_files['image_file']['tmp_name'])) {
// new unique filename
$stempfilename = 'cache/' . md5(time().rand());
// move uploaded file into cache folder
move_uploaded_file($_files['image_file']['tmp_name'], $stempfilename);
// change file permission to 644
@chmod($stempfilename, 0644);
if (file_exists($stempfilename) && filesize($stempfilename) > 0) {
$asize = getimagesize($stempfilename); // try to obtain image info
if (!$asize) {
@unlink($stempfilename);
return;
}
// check for image type
switch($asize[2]) {
case imagetype_jpeg:
$sext = '.jpg';
// create a new image from file
$vimg = @imagecreatefromjpeg($stempfilename);
break;
case imagetype_png:
$sext = '.png';
// create a new image from file
$vimg = @imagecreatefrompng($stempfilename);
break;
default:
@unlink($stempfilename);
return;
}
// create a new true color image
$vdstimg = @imagecreatetruecolor( $iwidth, $iheight );
// copy and resize part of an image with resampling
imagecopyresampled($vdstimg, $vimg, 0, 0, (int)$_post['x1'], (int)$_post['y1'], $iwidth, $iheight, (int)$_post['w'], (int)$_post['h']);
// define a result image filename
$sresultfilename = $stempfilename . $sext;
// output image to file
imagejpeg($vdstimg, $sresultfilename, $ijpgquality);
@unlink($stempfilename);
return $sresultfilename;
}
}
}
}
}
}
$simage = uploadimagefile();
echo '';
这样,带剪切功能的上传图片就已经完成了,若有疑问,请留言。大家共勉。