国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

HTML5技術(shù)實現(xiàn)剪切+上傳圖片的功能

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 15:16:33
文檔

HTML5技術(shù)實現(xiàn)剪切+上傳圖片的功能

HTML5技術(shù)實現(xiàn)剪切+上傳圖片的功能:我們曾經(jīng)被幾個讀者問到同一個問題——如何將照片傳到網(wǎng)上。我認為這是一個很有趣的問題,我決定在本文中解決掉這個問題。但是,我認為只是單純的實現(xiàn)上傳功能可能會有些單調(diào),所以我決定添加一個很重要的元素—剪切功能。相信這樣應(yīng)該更有魅力的說。 html
推薦度:
導(dǎo)讀HTML5技術(shù)實現(xiàn)剪切+上傳圖片的功能:我們曾經(jīng)被幾個讀者問到同一個問題——如何將照片傳到網(wǎng)上。我認為這是一個很有趣的問題,我決定在本文中解決掉這個問題。但是,我認為只是單純的實現(xiàn)上傳功能可能會有些單調(diào),所以我決定添加一個很重要的元素—剪切功能。相信這樣應(yīng)該更有魅力的說。 html

現(xiàn)在再描繪主體部分



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'] < 250 * 1024) {
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 '';

這樣,帶剪切功能的上傳圖片就已經(jīng)完成了,若有疑問,請留言。大家共勉。

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

HTML5技術(shù)實現(xiàn)剪切+上傳圖片的功能

HTML5技術(shù)實現(xiàn)剪切+上傳圖片的功能:我們曾經(jīng)被幾個讀者問到同一個問題——如何將照片傳到網(wǎng)上。我認為這是一個很有趣的問題,我決定在本文中解決掉這個問題。但是,我認為只是單純的實現(xiàn)上傳功能可能會有些單調(diào),所以我決定添加一個很重要的元素—剪切功能。相信這樣應(yīng)該更有魅力的說。 html
推薦度:
標(biāo)簽: 照片 圖片 上傳
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 黄色成人在线观看 | 91精品国产综合久 | 亚洲一区二区免费 | 成人免费一级毛片在线播放视频 | 成人久久精品 | 国产精品国产三级国产普通话一 | 久久精品成人国产午夜 | 亚洲一级毛片 | 国产精品制服诱惑 | 亚洲欧美视频 | 国产福利一区二区 | 成人国产一区二区三区精品 | 国产精品久久久久999 | 足交在线观看 | 91一区二区三区四区五区 | 欧美三页| 欧美精品v日韩精品v国产精品 | 亚洲一区 中文字幕 久久 | 久久国产精品一区二区 | 制服丝袜中文字幕在线 | 最新国产在线 | 国产精品1区2区3区在线播放 | 国产精品久久久久久久成人午夜 | 亚洲va乱码一区二区三区 | 国产欧美日韩精品综合 | 六月丁香在线观看 | 精品国产电影在线看免费观看 | 99久久国产综合精品成人影院 | 欧美日韩在线高清 | 久久久久国产成人精品亚洲午夜 | 久久精品无遮挡一级毛片 | 么公的又大又深又硬想要 | 在线视频一二三区2021不卡 | 国产成人精品一区二区三区 | 香蕉视频网页版 | 亚洲欧美久久精品一区 | 国产成人乱码一区二区三区在线 | 97成人资源| 综合毛片| 国产高清不卡码一区二区三区 | 亚洲精品在线免费观看视频 |