This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop.php
More file actions
218 lines (194 loc) · 6.2 KB
/
crop.php
File metadata and controls
218 lines (194 loc) · 6.2 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
include('includes/constants.php');
include('includes/functions.php');
$message = '';
startHeader();
displayHeader('Image Editor - Crop');
echo '<!-- Jcrop gives a GUI for users to pick what you want to crop out -->
<!-- Jcrop START -->
<script src="js/jquery.Jcrop.min.js"></script>
<script type="text/javascript">
jQuery(function($){
var jcrop_api;
$("#target").Jcrop({
onChange: showCoords,
onSelect: showCoords,
onRelease: clearCoords
},function(){
jcrop_api = this;
});
$("#coords").on("change","input",function(e){
var x1 = $("#x1").val(),
x2 = $("#x2").val(),
y1 = $("#y1").val(),
y2 = $("#y2").val();
jcrop_api.setSelect([x1,y1,x2,y2]);
});
});
// Simple event handler, called from onChange and onSelect
// event handlers, as per the Jcrop invocation above
function showCoords(c)
{
$("#x1").val(c.x);
$("#y1").val(c.y);
$("#x2").val(c.x2);
$("#y2").val(c.y2);
$("#w").val(c.w);
$("#h").val(c.h);
};
function clearCoords()
{
$(".coordsinput").val("");
};
</script>
<link rel="stylesheet" href="css/jquery.Jcrop.min.css" type="text/css" />
<!-- Jcrop END -->';
endHeader();
displayContentStart();
if (isset($_REQUEST['do']))
{
$do = $_REQUEST['do'];
// If Uploading
if ($do=='upload')
{
// No Image Errors
if ($_FILES["image"]["error"]==0)
{
// Get File Info
$filename = $_FILES["image"]["name"];
$filetype = $_FILES["image"]["type"];
$file_tmpname = $_FILES["image"]["tmp_name"];
// If Current File Type is Allowed
if (in_array($filetype, $allowedIMGtype))
{
// Save Image To Server
move_uploaded_file($file_tmpname, $currentdir . 'uploadedimages/' . $_FILES["image"]["name"]);
$imagelocation = 'uploadedimages/' . $_FILES["image"]["name"];
$displayCropForm = TRUE;
$displayUploadForm = FALSE;
}
else
{
$message = '<p class="badwarning">Sorry, we do not accept this file type.</p>';
$displayCropForm = FALSE;
$displayUploadForm = TRUE;
}
}
else
{
$message = '<p class="badwarning">There is an error with the image.</p>';
$displayCropForm = FALSE;
$displayUploadForm = TRUE;
}
}
// If Cropping
else if ($do=='crop')
{
$x1 = $_REQUEST['x1'];
$x2 = $_REQUEST['x2'];
$y1 = $_REQUEST['y1'];
$y2 = $_REQUEST['y2'];
$croppedheight = $_REQUEST['h'];
$croppedwidth = $_REQUEST['w'];
$imgname = $_REQUEST['imgname'];
if (file_exists("uploadedimages/$imgname"))
{
$extension = pathinfo($currentdir . 'uploadedimages/' . $imgname, PATHINFO_EXTENSION);
// Get New Image Type ID
$file_extID = array_search($extension, $allowedEXTs);
// Get Current Image Type
if ($file_extID==0)
{
$src = imagecreatefromgif($currentdir . 'uploadedimages/' . $imgname);
}
else if (($file_extID==1)||($file_extID==2))
{
$src = imagecreatefromjpeg($currentdir . 'uploadedimages/' . $imgname);
}
else if ($file_extID==3)
{
$src = imagecreatefrompng($currentdir . 'uploadedimages/' . $imgname);
}
$dest = imagecreatetruecolor($croppedwidth, $croppedheight);
imagecopy($dest, $src, 0, 0, $x1, $y1, $croppedwidth, $croppedheight);
saveImage($dest, $file_extID, $currentdir . 'uploadedimages/' . $imgname);
imagedestroy($src);
$downloadlink = "<a href=\"uploadedimages/$imgname\" download=\"$imgname\">$imgname</a>";
$displayCropForm = FALSE;
$displayUploadForm = FALSE;
}
else
{
$message = '<p class="badwarning">Unable to find your image. Please <a href="crop.php">reupload</a> it.</p>';
$displayCropForm = FALSE;
$displayUploadForm = TRUE;
}
}
else
{
$displayCropForm = FALSE;
$displayUploadForm = TRUE;
}
}
else
{
$displayCropForm = FALSE;
$displayUploadForm = TRUE;
}
if ($displayCropForm==TRUE)
{
echo '<div id="formheader">
Crop Image
</div>
<img src="' . $imagelocation . '" alt="User Image" id="target" class="centered" />
<form action="?do=crop" method="POST" id="coords" class="coords">
<input type="hidden" name="imgname" value="' . $_FILES["image"]["name"] . '" />
<div style="float:right;">
<input type="submit" name="submit" value="Crop Image" />
</div>
<div id="crop-inline-labels">
<label>X1 <input type="text" size="4" id="x1" name="x1" class="coordsinput" value="" /></label>
<label>Y1 <input type="text" size="4" id="y1" name="y1" class="coordsinput" value="" /></label>
<label>X2 <input type="text" size="4" id="x2" name="x2" class="coordsinput" value="" /></label>
<label>Y2 <input type="text" size="4" id="y2" name="y2" class="coordsinput" value="" /></label>
<label>W <input type="text" size="4" id="w" name="w" class="coordsinput" value="" /></label>
<label>H <input type="text" size="4" id="h" name="h" class="coordsinput" value="" /></label>
</div>
</form>';
}
else if (isset($downloadlink))
{
echo "<p>
Your image has successfully been cropped! You can download it to your computer below.
</p>
<p>
<strong>Download Link:</strong> $downloadlink
</p>
<p>
<img src=\"uploadedimages/$imgname\" alt=\"$imgname\" />
</p>";
}
else
{
echo '<form action="?do=upload" method="POST" enctype="multipart/form-data" style="float:right;">
<div id="formheader">
Crop Image
</div>
<div id="formimginput">
Image: <input type="file" name="image" id="image" value="" accept="image/*" /><br />
<input type="submit" name="submit" value="Upload" />
</div>
</form>
<p>
Cropping an image allows you to decrease its size by cutting out parts of the image. Please read the following in its entirety before continuing.
</p>
<p>
This script takes place in two steps.<br />
The first step requires you to upload the image from your computer to the site so it has the information needed for you to crop it.<br />
<br />
Then, with help from <a href="http://deepliquid.com/content/Jcrop.html" target="_blank">Jcrop</a>, you can visually select the area of the image that you want to crop out. For your convenience, you can see what the new height and width of the image will be. When you are ready to crop, just click the crop button and the server does the rest.
</p>
' . $message;
}
displayContentEnd();
?>