-
Notifications
You must be signed in to change notification settings - Fork 0
/
resizeimg.php
155 lines (135 loc) · 4.85 KB
/
resizeimg.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
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
<?php
/*
* @author Anakeen
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
* @package FDL
*/
/**
* Resize image (icons) by imagemagick converter
*
* @author Anakeen 2007
* @version $Id: resizeimg.php,v 1.10 2007/11/30 17:14:09 eric Exp $
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
* @package FDL
* @subpackage CORE
*/
/**
*/
include_once ("WHAT/Lib.Prefix.php");
include_once ("WHAT/Lib.Http.php");
include_once ("WHAT/Lib.Common.php");
function rezizelocalimage($img, $size, $basedest)
{
$source = $img;
$dest = DEFAULT_PUBDIR . $basedest;
$cmd = sprintf("convert -thumbnail %d %s %s", $size, escapeshellarg($source) , escapeshellarg($dest));
system($cmd);
if (file_exists($dest)) return $basedest;
return false;
}
function copylocalimage($img, $size, $basedest)
{
$source = $img;
$dest = DEFAULT_PUBDIR . $basedest;
$cmd = sprintf("/bin/cp %s %s", escapeshellarg($source) , escapeshellarg($dest));
system($cmd);
if (file_exists($dest)) return $basedest;
return false;
}
function getVaultPauth($vid)
{
$dbaccess = getDbAccess();
$rcore = pg_connect($dbaccess);
if ($rcore) {
$result = pg_query($rcore, "SELECT val from paramv where name='FREEDOM_DB' and type='G'");
if ($result) {
$row = pg_fetch_row($result);
$dbfree = current($row);
if ($dbfree) {
$rfree = pg_connect($dbfree);
if ($rfree) {
$result = pg_query("select id_dir,name,public_access from vaultdiskstorage where id_file=$vid");
if ($result) {
$row = pg_fetch_row($result);
if ($row) {
$iddir = $row[0];
$name = $row[1];
$free = $row[2];
if (!$free) return false;
if (preg_match('/\.([^\.]*)$/', $name, $reg)) {
$ext = $reg[1];
}
$result = pg_query("SELECT l_path,id_fs from vaultdiskdirstorage where id_dir = $iddir");
$row = pg_fetch_row($result);
$lpath = $row[0];
$idfs = $row[1];
$result = pg_query("SELECT r_path from vaultdiskfsstorage where id_fs = $idfs");
$row = pg_fetch_row($result);
$rpath = $row[0];
$localimg = "$rpath/$lpath/$vid.$ext";
if (file_exists($localimg)) return $localimg;
}
}
}
}
}
}
return false;
}
function getVaultCacheImage($vid, $size)
{
$basedest = "/img-cache/$size-vid$vid.png";
return $basedest;
}
$size = $_GET["size"];
$img = $_GET["img"];
if (!$img) {
$vid = $_GET["vid"];
if ($vid > 0) $img = "vaultid=$vid";
}
$dir = dirname($_SERVER["SCRIPT_NAME"]);
$ldir = DEFAULT_PUBDIR;
if (preg_match("/vaultid=([0-9]+)/", $img, $vids)) {
// vault file
$vid = $vids[1];
$basedest = getVaultCacheImage($vid, $size);
$dest = DEFAULT_PUBDIR . $basedest;
if (file_exists($dest)) {
$location = $ldir . "/" . $basedest;
} else {
$localimage = getVaultPauth(intval($vid));
if ($localimage) {
$tsize = getimagesize($localimage);
$width = intval($tsize[0]);
if ($width > $size) {
$newimg = rezizelocalimage($localimage, $size, $basedest);
} else {
$newimg = copylocalimage($localimage, $size, $basedest);
}
if ($newimg) $location = "$ldir/$newimg";
}
}
} else {
// local file
$turl = (parse_url($img));
$path = $turl["path"];
if (strstr($path, $dir) == $path) {
$localimage = substr($path, strlen($dir));
} else {
$localimage = $img;
}
$basedest = "/img-cache/$size-" . basename(str_replace("/", "_", $localimage)) . ".png";
$dest = DEFAULT_PUBDIR . $basedest;
if (file_exists($dest) && filemtime($dest) >= filemtime(DEFAULT_PUBDIR . "/$localimage")) {
$location = "$ldir/$basedest";
} else {
$newimg = rezizelocalimage(DEFAULT_PUBDIR . "/$localimage", $size, $basedest);
if ($newimg) $location = "$ldir/$newimg";
}
}
//print("<hr>Location: $location");
if ($location) $location = "/" . ltrim($location, "/");
else $location = $img;
Http_DownloadFile($location, basename($location) , "image/png", true, true);
//Header("Location: $location");
?>