-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
99 lines (85 loc) · 2.41 KB
/
upload.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
<?php
/**
* POSTされた画像を回転して保存
* ファイル名:Y-m-d-H-i-s-u.{fileType}
*/
/**
* main
*/
function main ()
{
$time = new DateTime();
$uploadDirName = 'uploads';
if ($_FILES['file']['tmp_name'] ?? '') {
$inputName = $_FILES['file']['tmp_name'];
$fileType = exif_imagetype($inputName);
$exif = @exif_read_data($inputName); //EXIF無かったらエラーになる
$exif = $exif ? $exif : [];
$outputName = $uploadDirName . DIRECTORY_SEPARATOR . $time->format('Y-m-d-H-i-s-u') . image_type_to_extension($fileType);
$image = imagecreatefromstring(file_get_contents($inputName));
if (! $image) exit('image create error');
$image = rotate($image, $exif);
return save($image, $outputName, $fileType);
}
}
/**
* rotate
* @param resource $image
* @param array $exif
* @return resource
*/
function rotate($image, array $exif)
{
$orientation = $exif['Orientation'] ?? 1;
switch ($orientation) {
case 1 : //no rotate
break;
case 2 : //FLIP_HORIZONTAL
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3 : //ROTATE 180
$image = imagerotate($image,180, 0);
break;
case 4 : //FLIP_VERTICAL
imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5 : //ROTATE 270 FLIP_HORIZONTAL
$image = imagerotate($image,270, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 6 : //ROTATE 90
$image = imagerotate($image,270, 0);
break;
case 7 : //ROTATE 90 FLIP_HORIZONTAL
$image = imagerotate($image,90, 0);
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 8 : //ROTATE 270
$image = imagerotate($image,90, 0);
break;
}
return $image;
}
/**
* save
* @param resource $image
* @param string $outputName
* @param int $fileType
* @return bool
*/
function save($image, string $outputName, int $fileType)
{
switch ($fileType) {
case IMAGETYPE_GIF :
return imagegif($image, $outputName);
break;
case IMAGETYPE_JPEG :
return imagejpeg($image, $outputName);
break;
case IMAGETYPE_PNG :
return imagepng($image, $outputName);
break;
}
imagedestroy($image);
}
main();