-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
executable file
·174 lines (140 loc) · 4.6 KB
/
index.php
File metadata and controls
executable file
·174 lines (140 loc) · 4.6 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
*
*** This is intended as demo code ***
* This is for testing PHP resize functions on uploaded images.
* This file includes the upload form, and the code for processing the image.
* It has code for resizing an image, and for automatically making a positioned/cropped thumbnail.
* To position thumbnail, there is a select element.
* For portrait image, "left" and "right" selections are ignored, thumb will be centered
* For landscape image, "top" and "bottom" selections are ignored, thumb will be centered
*/
/* display errors */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$upload = false; // true if file upload succeeded
$cropVar = array(); // collection of parameters for cropping image
$thumbpos = (!empty(isset($_POST['thumbpos']))) ? trim($_POST['thumbpos']) : 'center' ;
/* create uploads folder */
if (!file_exists('uploads')) {
mkdir('uploads');
}
/* handle file upload */
if (isset($_FILES['userfile']['name'])) {
/* variables from uploaded file */
$name = $_FILES['userfile']['name'];
$tmp = $_FILES['userfile']['tmp_name'];
$target = 'uploads/'.$name;
$basename = pathinfo($name, PATHINFO_BASENAME);
$filename = pathinfo($name, PATHINFO_FILENAME);
echo $basename.'<br>';
echo $filename.'<br>';
/* save uploaded file to destination folder */
if (move_uploaded_file($tmp, $target)) {
echo $target." uploaded<br>";
$upload = true;
} else {
echo "file didn't upload<br>";
}
} else {
echo "select a file<br>";
}
/* if upload was successful, process it */
if ($upload) {
/* check file type to create image */
switch($_FILES['userfile']['type']) {
case "image/png":
$imageType = "PNG";
$sourceImage = imagecreatefrompng($target);
break;
case "image/jpeg":
$imageType = "JPEG";
$sourceImage = imagecreatefromjpeg($target);
break;
case "image/gif":
$imageType = "GIF";
$sourceImage = imagecreatefromgif($target);
break;
case "image/bmp":
$imageType = "BMP";
$sourceImage = imagecreatefrombmp($target);
break;
case "image/jpg":
$imageType = "JPG";
$sourceImage = imagecreatefromjpeg($target);
break;
default:
$imageType = "None";
$sourceImage = NULL;
}
echo "File type = ".$imageType;
echo "<br>";
if ($sourceImage!=NULL) {
/* get image dimensions */
$imgWd = imagesx($sourceImage); //width
$imgHt = imagesy($sourceImage); //height
$imgAsp = ($imgWd/$imgHt); // aspect ration
echo "width: ".$imgWd."<br>";
echo "height: ".$imgHt."<br>";
echo "aspect: ".$imgAsp."<br>";
echo "thumb position: ".$thumbpos;
/* information for positioning and cropping */
/* if thumb position not applicable to aspect ratio, thumb will be centered */
if ($imgAsp<1) {
$cropVar['w'] = $imgWd;
$cropVar['h'] = $imgWd;
if ($thumbpos=="top") {
$cropVar['x'] = 0;
$cropVar['y'] = 0;
} else if ($thumbpos=="bottom") {
$cropVar['x'] = 0;
$cropVar['y'] = $imgHt - $imgWd;
} else {
$cropVar['x'] = 0;
$cropVar['y'] = ($imgHt - $imgWd)/2;
}
} else {
$cropVar['w'] = $imgHt;
$cropVar['h'] = $imgHt;
if ($thumbpos=="left") {
$cropVar['x'] = 0;
$cropVar['y'] = 0;
} else if ($thumbpos=="right") {
$cropVar['x'] = $imgWd - $imgHt;
$cropVar['y'] = 0;
} else {
$cropVar['x'] = ($imgWd - $imgHt)/2;
$cropVar['y'] = 0;
}
}
/* crop image then resize */
$imgCropCntr = imagecrop($sourceImage, ['x'=>$cropVar['x'], 'y'=>$cropVar['y'], 'width'=>$cropVar['w'], 'height'=>$cropVar['h']]);
$imgCropScl = imagescale($imgCropCntr, 300);
/* resize and crop image */
$imgScaled800 = imagescale($sourceImage, 800);
$imgScaled300 = imagescale($sourceImage, 300);
$imgCrop300 = imagecrop($imgScaled300, ['x'=>0, 'y'=>0, 'width'=>300, 'height'=>300]);
/* save scaled images as jpg */
imagejpeg($imgScaled800, 'uploads/'.$filename.'-800.jpg', 90);
imagejpeg($imgScaled300, 'uploads/'.$filename.'-300.jpg', 90);
imagejpeg($imgCrop300, 'uploads/'.$filename.'-thumb-300.jpg', 90);
imagejpeg($imgCropScl, 'uploads/'.$filename.'-thumb-cntr.jpg', 90);
/* display the centered/cropped/rescaled thumbnail */
echo '<h4>Thumbnail test:</h4><img src="'.'uploads/'.$filename.'-thumb-cntr.jpg'.'">';
}
}
?>
<h2>PHP image resize tests</h2>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="userfile" id="userfile">
<select name="thumbpos" id="thumbpos">
<option value="center">center</option>
<option value="left">left</option>
<option value="right">right</option>
<option value="top">top</option>
<option value="bottom">bottom</option>
</select>
<input type="submit">
</form>
<a href=".">reset</a>