Ajax+PHP邊學邊練 之五 圖片處理

來源:互聯網
上載者:User

先上個:


Sample6_1.php 中建立Form:

複製代碼 代碼如下://顯示上傳狀態和圖片
<div id="showimg"></div>
//上傳檔案需要定義enctype,為了顯示圖片將target設為uploadframe
<form id="uploadform" action="process_upload.php" method="post"
enctype="multipart/form-data" target="uploadframe">
Upload a File:<br />
<input type="file" id="myfile" name="myfile" />
//上傳檔案
<input type="submit" value="Submit" onclick="uploadimg(document.getElementById('uploadform')); return false;" />
<iframe id="uploadframe" name="uploadframe" src="process_upload.php" class="noshow"></iframe>
</form>

上傳圖片函數 uploadimg: 複製代碼 代碼如下:function uploadimg(theform){
//提交Form
theform.submit();
//在showimg <div>中顯示上傳狀態
setStatus ("Loading...","showimg");
}
//上傳狀態函數
function setStatus (theStatus, theObj){
obj = document.getElementById(theObj);
if (obj){
obj.innerHTML = "<div class=\"bold\">" + theStatus + "</div>";
}
}

process_upload.php 提供檔案上傳功能: 複製代碼 代碼如下:<?php
//提供圖片類型校正
$allowedtypes = array("image/jpeg","image/pjpeg","image/png", "image/x-png","image/gif");
//檔案存放目錄
$savefolder = "images";

//如果有檔案上傳就開始幹活
if (isset ($_FILES['myfile'])){
//檢查上傳檔案是否符合$allowedtypes類型
if (in_array($_FILES['myfile']['type'],$allowedtypes)){
if ($_FILES['myfile']['error'] == 0){
$thefile = "$savefolder/".$_FILES['myfile']['name'];
//通過move_uploaded_file上傳檔案
if (!move_uploaded_file($_FILES['myfile']['tmp_name'], $thefile)){
echo "There was an error uploading the file.";
}
else{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
<!-- 顯示圖片 -->
<img src="<?php echo $thefile; ?>" onload="doneloading(parent,'<?php echo $thefile; ?>')" />
</body>
</html>
<?php
}
}
}
}
?>

上面代碼最後部分的doneloading 函數就是用來顯示圖片及修改圖片尺寸大小。其中會用到thumb.php,它會在images目錄中產生出源圖片的大、中、小三個尺寸,有興趣可以研究一下。歡迎大家拍磚~
文中源碼打包下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.