-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
154 lines (132 loc) · 3.92 KB
/
image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/****
* 1.建立資料庫及資料表
* 2.建立上傳圖案機制
* 3.取得圖檔資源
* 4.進行圖形處理
* ->圖形縮放
* ->圖形加邊框
* ->圖形驗證碼
* 5.輸出檔案
*/
include_once "base.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>文字檔案匯入</title>
<link rel="stylesheet" href="style.css">
<style>
#uploadForm{
width:400px;
margin:1rem auto;
font-size:1.25rem;
padding:1rem;
}
#list{
border-collapse: collapse;
box-shadow: 0 0 10px #ccc;
margin:1rem auto;
}
#list img{
width:150px;
}
#list td,#list th{
border:1px solid #ccc;
padding:0.5rem 1.1rem;
font-size:1.15rem;
}
#list tr:hover{
background:lightgreen;
transform: scale(1.1);
}
</style>
</head>
<body>
<h1 class="header">圖形處理練習</h1>
<!---建立檔案上傳機制--->
<form id="uploadForm" action="?" method="post" enctype="multipart/form-data">
<div>
選擇檔案:<input type="file" name="file">
</div>
<div>
<input type="number" step="any" name="percent" value="0.5">
</div>
<div>
<select name="color" >
<option value="blue">藍色</option>
<option value="red">紅色</option>
<option value="green">綠色</option>
</select>
</div>
<input type="submit" value="上傳">
</form>
<?php
if(isset($_FILES['file']['tmp_name'])){
move_uploaded_file($_FILES['file']['tmp_name'],'./upload/'.$_FILES['file']['name']);
echo "<img src='./upload/{$_FILES['file']['name']}'>";
}
?>
<!----縮放圖形----->
<?php
if(isset($_FILES['file']['tmp_name'])){
$filename="./upload/{$_FILES['file']['name']}";
$percent=$_POST['percent']??0.5;
$src_width=getimagesize($filename)[0];
$src_height=getimagesize($filename)[1];
$dst_width=$src_width*$percent;
$dst_height=$src_height*$percent;
$dst_img=imagecreatetruecolor($dst_width,$dst_height);
$src_img=imagecreatefromjpeg($filename);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_width,$dst_height,$src_width,$src_height);
imagejpeg($dst_img,"./upload/result.jpg",100);
imagedestroy($dst_img);
imagedestroy($src_img);
}
?>
<h2>縮放後的圖形</h2>
<img src="./upload/result.jpg" alt="">
<!----圖形加邊框----->
<?php
if(isset($_FILES['file']['tmp_name'])){
$filename="./upload/{$_FILES['file']['name']}";
$percent=0.5;
$border=10;
$color=$_POST['color'];
$src_width=getimagesize($filename)[0];
$src_height=getimagesize($filename)[1];
$dst_width=$src_width*$percent;
$dst_height=$src_height*$percent;
$dst_img=imagecreatetruecolor($dst_width,$dst_height);
$blue=imagecolorallocate($dst_img,0,0,255);
$red=imagecolorallocate($dst_img,255,0,0);
$green=imagecolorallocate($dst_img,0,255,0);
imagefill($dst_img,0,0,$$color);
/* switch($color){
case 'blue':
imagefill($dst_img,0,0,$blue);
break;
case 'red':
imagefill($dst_img,0,0,$red);
break;
case "green":
imagefill($dst_img,0,0,$green);
break;
} */
$src_img=imagecreatefromjpeg($filename);
$dst_width=$src_width*$percent - ($border*2);
$dst_height=$src_height*$percent - ($border*2);
imagecopyresampled($dst_img,$src_img,$border,$border,0,0,$dst_width,$dst_height,$src_width,$src_height);
imagejpeg($dst_img,"./upload/border.jpg",100);
imagedestroy($dst_img);
imagedestroy($src_img);
}
?>
<h2>加邊框後的圖形</h2>
<img src="./upload/border.jpg" alt="">
<!----產生圖形驗證碼----->
</body>
</html>