質問編集履歴
1
書式を変更しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,263 @@
|
|
1
|
+
### 発生している問題・エラーメッセージ
|
2
|
+
|
3
|
+
パラメータ全て埋まっているはずなのに表示される
|
4
|
+
|
5
|
+
```
|
6
|
+
|
7
|
+
Warning: imagecopyresampled() expects parameter 2 to be resource, string given in /Applications/MAMP/htdocs/Sunseer_PHP_BBS/uploader.php on line 35
|
8
|
+
|
9
|
+
```
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
### 該当のソースコード
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
```php
|
18
|
+
|
19
|
+
<?php
|
20
|
+
|
21
|
+
ini_set("display_errors", 1);
|
22
|
+
|
23
|
+
error_reporting(E_ALL);
|
24
|
+
|
25
|
+
$dsn = 'mysql:dbname=*; host=localhost; charset=utf8';
|
26
|
+
|
27
|
+
$user = '*';
|
28
|
+
|
29
|
+
$passwd = '*';
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
define("MAX_SIZE",1*1024*1024);
|
34
|
+
|
35
|
+
define("THUMBNAIL_WIDTH",400);
|
36
|
+
|
37
|
+
define("IMAGES_DIR",__DIR__."/Image");
|
38
|
+
|
39
|
+
define("THUMBNAIL_DIR",__DIR__."/Thumbs");
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
echo THUMBNAIL_WIDTH;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
$id = $_POST['id'];
|
50
|
+
|
51
|
+
$image = $_FILES['image']['name'];
|
52
|
+
|
53
|
+
$up_err = $_FILES['image']['error'];
|
54
|
+
|
55
|
+
$src = $_FILES['image']['tmp_name'];
|
56
|
+
|
57
|
+
$type = $_FILES['image']['type'];
|
58
|
+
|
59
|
+
$dir = "Image/".$image;
|
60
|
+
|
61
|
+
$image_size = getimagesize($_FILES['image']['tmp_name']);
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
if ($_FILES['image']['size']<MAX_SIZE){
|
70
|
+
|
71
|
+
$image_width = $image_size[0];
|
72
|
+
|
73
|
+
$image_height = $image_size[1];
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
// THUMBNAIL_WITDH →Use of undefined constant THUMBNAIL_WITDH - assumed 'THUMBNAIL_WITDH' (this will throw an Error in a future version of PHP)
|
78
|
+
|
79
|
+
if ($image_width > 400 ) {
|
80
|
+
|
81
|
+
$thumbwidth = 400;
|
82
|
+
|
83
|
+
$thumbheight = round($image_height * 400/$image_width);
|
84
|
+
|
85
|
+
$thumbImage = imagecreatetruecolor(400,$thumbheight);
|
86
|
+
|
87
|
+
imagecopyresampled($thumbImage,$src,0,0,0,0,$thumbwidth,$thumbheight,$image_width,$image_height);
|
88
|
+
|
89
|
+
switch($type){
|
90
|
+
|
91
|
+
case IMAGETYPE_GIF;
|
92
|
+
|
93
|
+
imagegif($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
94
|
+
|
95
|
+
// ファイルの出力
|
96
|
+
|
97
|
+
case IMAGETYPE_PNG;
|
98
|
+
|
99
|
+
imagepng($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
100
|
+
|
101
|
+
case IMAGETYPE_JPEG;
|
102
|
+
|
103
|
+
imagejpeg($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
104
|
+
|
105
|
+
//
|
106
|
+
|
107
|
+
break;
|
108
|
+
|
109
|
+
default:
|
110
|
+
|
111
|
+
echo "判別できません";
|
112
|
+
|
113
|
+
break;
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
}else {
|
120
|
+
|
121
|
+
echo "画像ファイルのサイズが大きすぎます";
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
var_dump($_FILES);
|
128
|
+
|
129
|
+
// print __DIR__;
|
130
|
+
|
131
|
+
// $savePath = IMAGES_DIR."/{$src}";
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
if ($up_err == UPLOAD_ERR_OK) {
|
142
|
+
|
143
|
+
if (move_uploaded_file($src,$dir)) {
|
144
|
+
|
145
|
+
try {
|
146
|
+
|
147
|
+
$db = new PDO($dsn,$user,$passwd);
|
148
|
+
|
149
|
+
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
|
150
|
+
|
151
|
+
$sql = "UPDATE db_BBS SET icon='$dir' WHERE id='$id'";
|
152
|
+
|
153
|
+
// $stat = $db->query()
|
154
|
+
|
155
|
+
if(!$db->query($sql)){
|
156
|
+
|
157
|
+
echo "sql処理に問題あり";
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
} catch (\PDOException $e) {
|
162
|
+
|
163
|
+
print $e->getMessage();
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
// :TODO
|
168
|
+
|
169
|
+
}else{
|
170
|
+
|
171
|
+
echo "アップロード処理に失敗しました";
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}else {
|
176
|
+
|
177
|
+
echo "エラー:{$up_err}<br>";
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
?>
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
<!DOCTYPE html>
|
186
|
+
|
187
|
+
<html lang="ja">
|
188
|
+
|
189
|
+
<head>
|
190
|
+
|
191
|
+
<meta charset="utf-8">
|
192
|
+
|
193
|
+
<title>upload image</title>
|
194
|
+
|
195
|
+
</head>
|
196
|
+
|
197
|
+
<body>
|
198
|
+
|
199
|
+
<h1>images</h1>
|
200
|
+
|
201
|
+
<img src="<?php echo "Image/".$image; ?>">
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
### 試したこと
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
ここに問題に対して試したことを記載してください。
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
問題箇所↓
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
if ($image_width > 400 ) {
|
222
|
+
|
223
|
+
$thumbwidth = 400;
|
224
|
+
|
225
|
+
$thumbheight = round($image_height * 400/$image_width);
|
226
|
+
|
227
|
+
$thumbImage = imagecreatetruecolor(400,$thumbheight);
|
228
|
+
|
229
|
+
imagecopyresampled($thumbImage,$src,0,0,0,0,$thumbwidth,$thumbheight,$image_width,$image_height);
|
230
|
+
|
231
|
+
switch($type){
|
232
|
+
|
233
|
+
case IMAGETYPE_GIF;
|
234
|
+
|
235
|
+
imagegif($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
236
|
+
|
237
|
+
// ファイルの出力
|
238
|
+
|
239
|
+
case IMAGETYPE_PNG;
|
240
|
+
|
241
|
+
imagepng($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
242
|
+
|
243
|
+
case IMAGETYPE_JPEG;
|
244
|
+
|
245
|
+
imagejpeg($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
246
|
+
|
247
|
+
//
|
248
|
+
|
249
|
+
break;
|
250
|
+
|
251
|
+
default:
|
252
|
+
|
253
|
+
echo "判別できません";
|
254
|
+
|
255
|
+
break;
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
```
|
260
|
+
|
1
261
|
### 前提・実現したいこと
|
2
262
|
|
3
263
|
アップロードされたファイルのテンポラリファイルをサムネイル化して、Thumbsディレクトリで管理したい
|
@@ -8,270 +268,6 @@
|
|
8
268
|
|
9
269
|
$_FILE['image']['tmp_name']をソースとして、サムネイル画像を作成しThumbsディレクトリに保存したい
|
10
270
|
|
11
|
-
■■な機能を実装中に以下のエラーメッセージが発生しました。
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
### 発生している問題・エラーメッセージ
|
16
|
-
|
17
|
-
パラメータ全て埋まっているはずなのに表示される
|
18
|
-
|
19
|
-
```
|
20
|
-
|
21
|
-
Warning: imagecopyresampled() expects parameter 2 to be resource, string given in /Applications/MAMP/htdocs/Sunseer_PHP_BBS/uploader.php on line 35
|
22
|
-
|
23
|
-
```
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
### 該当のソースコード
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
```php
|
32
|
-
|
33
|
-
<?php
|
34
|
-
|
35
|
-
ini_set("display_errors", 1);
|
36
|
-
|
37
|
-
error_reporting(E_ALL);
|
38
|
-
|
39
|
-
$dsn = 'mysql:dbname=*; host=localhost; charset=utf8';
|
40
|
-
|
41
|
-
$user = '*';
|
42
|
-
|
43
|
-
$passwd = '*';
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
define("MAX_SIZE",1*1024*1024);
|
48
|
-
|
49
|
-
define("THUMBNAIL_WIDTH",400);
|
50
|
-
|
51
|
-
define("IMAGES_DIR",__DIR__."/Image");
|
52
|
-
|
53
|
-
define("THUMBNAIL_DIR",__DIR__."/Thumbs");
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
echo THUMBNAIL_WIDTH;
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
$id = $_POST['id'];
|
64
|
-
|
65
|
-
$image = $_FILES['image']['name'];
|
66
|
-
|
67
|
-
$up_err = $_FILES['image']['error'];
|
68
|
-
|
69
|
-
$src = $_FILES['image']['tmp_name'];
|
70
|
-
|
71
|
-
$type = $_FILES['image']['type'];
|
72
|
-
|
73
|
-
$dir = "Image/".$image;
|
74
|
-
|
75
|
-
$image_size = getimagesize($_FILES['image']['tmp_name']);
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
if ($_FILES['image']['size']<MAX_SIZE){
|
84
|
-
|
85
|
-
$image_width = $image_size[0];
|
86
|
-
|
87
|
-
$image_height = $image_size[1];
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
// THUMBNAIL_WITDH →Use of undefined constant THUMBNAIL_WITDH - assumed 'THUMBNAIL_WITDH' (this will throw an Error in a future version of PHP)
|
92
|
-
|
93
|
-
if ($image_width > 400 ) {
|
94
|
-
|
95
|
-
$thumbwidth = 400;
|
96
|
-
|
97
|
-
$thumbheight = round($image_height * 400/$image_width);
|
98
|
-
|
99
|
-
$thumbImage = imagecreatetruecolor(400,$thumbheight);
|
100
|
-
|
101
|
-
imagecopyresampled($thumbImage,$src,0,0,0,0,$thumbwidth,$thumbheight,$image_width,$image_height);
|
102
|
-
|
103
|
-
switch($type){
|
104
|
-
|
105
|
-
case IMAGETYPE_GIF;
|
106
|
-
|
107
|
-
imagegif($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
108
|
-
|
109
|
-
// ファイルの出力
|
110
|
-
|
111
|
-
case IMAGETYPE_PNG;
|
112
|
-
|
113
|
-
imagepng($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
114
|
-
|
115
|
-
case IMAGETYPE_JPEG;
|
116
|
-
|
117
|
-
imagejpeg($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
118
|
-
|
119
|
-
//
|
120
|
-
|
121
|
-
break;
|
122
|
-
|
123
|
-
default:
|
124
|
-
|
125
|
-
echo "判別できません";
|
126
|
-
|
127
|
-
break;
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
}else {
|
134
|
-
|
135
|
-
echo "画像ファイルのサイズが大きすぎます";
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
var_dump($_FILES);
|
142
|
-
|
143
|
-
// print __DIR__;
|
144
|
-
|
145
|
-
// $savePath = IMAGES_DIR."/{$src}";
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
if ($up_err == UPLOAD_ERR_OK) {
|
156
|
-
|
157
|
-
if (move_uploaded_file($src,$dir)) {
|
158
|
-
|
159
|
-
try {
|
160
|
-
|
161
|
-
$db = new PDO($dsn,$user,$passwd);
|
162
|
-
|
163
|
-
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
|
164
|
-
|
165
|
-
$sql = "UPDATE db_BBS SET icon='$dir' WHERE id='$id'";
|
166
|
-
|
167
|
-
// $stat = $db->query()
|
168
|
-
|
169
|
-
if(!$db->query($sql)){
|
170
|
-
|
171
|
-
echo "sql処理に問題あり";
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
} catch (\PDOException $e) {
|
176
|
-
|
177
|
-
print $e->getMessage();
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
// :TODO
|
182
|
-
|
183
|
-
}else{
|
184
|
-
|
185
|
-
echo "アップロード処理に失敗しました";
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
}else {
|
190
|
-
|
191
|
-
echo "エラー:{$up_err}<br>";
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
?>
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
<!DOCTYPE html>
|
200
|
-
|
201
|
-
<html lang="ja">
|
202
|
-
|
203
|
-
<head>
|
204
|
-
|
205
|
-
<meta charset="utf-8">
|
206
|
-
|
207
|
-
<title>upload image</title>
|
208
|
-
|
209
|
-
</head>
|
210
|
-
|
211
|
-
<body>
|
212
|
-
|
213
|
-
<h1>images</h1>
|
214
|
-
|
215
|
-
<img src="<?php echo "Image/".$image; ?>">
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
```
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
### 試したこと
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
ここに問題に対して試したことを記載してください。
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
問題箇所↓
|
232
|
-
|
233
|
-
```
|
234
|
-
|
235
|
-
if ($image_width > 400 ) {
|
236
|
-
|
237
|
-
$thumbwidth = 400;
|
238
|
-
|
239
|
-
$thumbheight = round($image_height * 400/$image_width);
|
240
|
-
|
241
|
-
$thumbImage = imagecreatetruecolor(400,$thumbheight);
|
242
|
-
|
243
|
-
imagecopyresampled($thumbImage,$src,0,0,0,0,$thumbwidth,$thumbheight,$image_width,$image_height);
|
244
|
-
|
245
|
-
switch($type){
|
246
|
-
|
247
|
-
case IMAGETYPE_GIF;
|
248
|
-
|
249
|
-
imagegif($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
250
|
-
|
251
|
-
// ファイルの出力
|
252
|
-
|
253
|
-
case IMAGETYPE_PNG;
|
254
|
-
|
255
|
-
imagepng($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
256
|
-
|
257
|
-
case IMAGETYPE_JPEG;
|
258
|
-
|
259
|
-
imagejpeg($thumbImage,THUMBNAIL_DIR.'/'.$image);
|
260
|
-
|
261
|
-
//
|
262
|
-
|
263
|
-
break;
|
264
|
-
|
265
|
-
default:
|
266
|
-
|
267
|
-
echo "判別できません";
|
268
|
-
|
269
|
-
break;
|
270
|
-
|
271
|
-
}
|
272
|
-
|
273
|
-
```
|
274
|
-
|
275
271
|
|
276
272
|
|
277
273
|
なぜimagecopyresampledできないかわからないです
|