-
Notifications
You must be signed in to change notification settings - Fork 3
/
rotate.php
80 lines (55 loc) · 1.88 KB
/
rotate.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
<?php
require("smartyStarter.php");
require("Picture.php");
$pictures = new Picture($local_database, $local_username, $local_password);
$picture_id = isset($_REQUEST["picture_id"]) ? $_REQUEST["picture_id"] : "";
$rotatedPic = $pictures->loadPicture($picture_id);
$picName = $rotatedPic['filename'];
$extension = $rotatedPic['extension'];
$filepath = $rotatedPic['path'];
if(strlen($picName)<3) {
$rotatedBy = "";
$coreName = $picName;
}else {
$rotatedBy = substr ($picName, strlen($picName)-2, 2);
if($rotatedBy == '_a' || $rotatedBy == '_b' || $rotatedBy == '_c') {
$coreName = substr ($picName , 0, strlen($picName)-2);
}else{
$coreName = $picName;
}
}
function ifFileDoesntExistRotate($newFilepath, $filepath) {
if(!file_exists($newFilepath)) {
$degrees = -90;
header('Content-type: image/JPG');
$source = imagecreatefromjpeg($filepath);
$rotate = imagerotate($source,$degrees,0);
imagejpeg($rotate, $newFilepath); //save the new image
imagedestroy($source); //free up the memory
imagedestroy($rotate); //free up the memory
}
}
switch($rotatedBy) {
case '_a':
$newFilepath = 'images/'. $coreName.'_b.'.$extension.'';
$coreName = $coreName .'_b';
ifFileDoesntExistRotate($newFilepath, $filepath);
break;
case '_b':
$newFilepath = 'images/'. $coreName.'_c.'.$extension.'';
$coreName = $coreName .'_c';
ifFileDoesntExistRotate($newFilepath, $filepath);
break;
case '_c':
$newFilepath = 'images/'. $coreName.'.'.$extension.'';
ifFileDoesntExistRotate($newFilepath, $filepath);
break;
default:
$newFilepath = 'images/'. $coreName.'_a.'.$extension.'';
$coreName = $coreName . '_a';
ifFileDoesntExistRotate($newFilepath,$filepath);
}
$pictures->updateFilepath($picture_id, $newFilepath);
$pictures->updatePicName($picture_id, $coreName);
$pictures->closeConnection();
echo $newFilepath;