標籤:大小 代碼 esx img 定義 名稱 檔案名稱 部分 doctype
<!DOCTYPE html>
<!-- HTML5表單 -->
<form method="post" action="" enctype="multipart/form-data">
<h2>檔案上傳</h2>
<input type="file" name="picture"><br/>
<input type="submit" name="submit" value="提交">
</form>
<!--PHP代碼部分-->
<?php
if(isset($_POST[‘submit‘]))
{
$filename=$_FILES[‘picture‘][‘name‘];//擷取上傳檔案的名稱
$type=$_FILES[‘picture‘][‘type‘];//擷取上傳檔案的類型
$upfile="../good/image/EX5_9.jpg";//上傳後檔案所在的路徑和檔案名稱
$small_upfile="../good/image/EX5_9_small.jpg";//上傳後縮圖所在的路徑和檔案名稱
if(!$filename)//判斷檔案是否存在
{
echo "<script>alert(‘檔案不存在!‘) </script>";
}
//else if($type!=‘image/pjpeg‘)//判斷檔案是否為JPEG格式
//{
//echo "<script>alert(‘檔案格式不正確!‘) </script>";
//}
//else
{
//複製上傳檔案並將檔案儲存為$upfile
move_uploaded_file($_FILES[‘picture‘][‘tmp_name‘],$upfile);
$dst_w=150;//設定縮圖的寬
$dst_h=150;//設定縮圖的高
$src_image=imagecreatefromjpeg($upfile);//讀取上傳後的檔案並建立映像
$src_w=imagesx($src_image);//獲得映像的寬
$src_h=imagesy($src_image);//獲得映像的高
$dst_image=imagecreatetruecolor($dst_w,$dst_h);//建立新映像
//將映像$src_image重新定義大小並寫入新的映像$dst_image中
imagecopyresized($dst_image,$src_image,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_image,$small_upfile);//將新映像儲存為$small_upfile
echo "檔案上傳成功,縮圖如下:<br/>";
echo "<img src=$small_upfile>";//顯示映像
imagedestroy($src_image);
imagedestroy($dst_image);
}
}
?>
php 縮圖