利用php怎么實(shí)現(xiàn)一個(gè)截圖上傳功能?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

原理很簡單其實(shí)就是監(jiān)控粘貼事件,然后檢測是否粘貼的東西里面有圖片,有的話直接觸發(fā)ajax上傳
代碼可以直接運(yùn)行,有興趣你們可以試試
<?php
header("Access-Control-Allow-Origin:*");
$url = 'http://'.$_SERVER['HTTP_HOST'];
$file = (isset($_POST["file"])) ? $_POST["file"] : '';
if($file)
{
$data = base64_decode(str_replace('data:image/png;base64,', '', $file)); //截圖得到的只能是png格式圖片,所以只要處理png就行了
$name = md5(time()) . '.png'; // 這里把文件名做了md5處理
file_put_contents($name, $data);
echo"$url/$name";
die;
}
?>
<div id="box"contenteditable>
</div>
<input type="hidden"name="img"value=""id="img_puth"/>
<script>
//查找box元素,檢測當(dāng)粘貼時(shí)候,
document.querySelector('#box').addEventListener('paste', function(e) {
//判斷是否是粘貼圖片
if (e.clipboardData && e.clipboardData.items[0].type.indexOf('image') > -1)
{
var that = this,
reader = new FileReader();
file = e.clipboardData.items[0].getAsFile();
//ajax上傳圖片
reader.onload = function(e)
{
var xhr = new XMLHttpRequest(),
fd = new FormData();
xhr.open('POST', '', true);
xhr.onload = function ()
{
var img = new Image();
img.src = xhr.responseText;
// that.innerHTML = '<img src="'+img.src+'"alt=""/>';
document.getElementById("img_puth").value = img.src;
}
// this.result得到圖片的base64 (可以用作即時(shí)顯示)
fd.append('file', this.result);
that.innerHTML = '<img src="'+this.result+'"alt=""/>';
xhr.send(fd);
}
reader.readAsDataURL(file);
}
}, false);
</script>關(guān)于利用php怎么實(shí)現(xiàn)一個(gè)截圖上傳功能問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)頁標(biāo)題:利用php怎么實(shí)現(xiàn)一個(gè)截圖上傳功能-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://www.chinadenli.net/article20/djdjco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站排名、品牌網(wǎng)站制作、全網(wǎng)營銷推廣、面包屑導(dǎo)航、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容