-
Notifications
You must be signed in to change notification settings - Fork 7
/
progressive_utils.inc.php
122 lines (120 loc) · 3.99 KB
/
progressive_utils.inc.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
<?php
function GetImageScans($src, &$info) {
$scans = null;
$hash = sha1($src);
$dir = './tmp/' . $hash[0];
if (!is_dir($dir))
mkdir($dir);
$dir = realpath($dir);
$original = "$dir/$hash.original.jpg";
$progressive = "$dir/$hash.jpg";
$baseline = "$dir/$hash-baseline.jpg";
if (!is_file($original)) {
file_put_contents($original, file_get_contents($src));
}
if (is_file($original)) {
$size = getimagesize($original);
$info['original'] = $original;
$info['originalSize'] = filesize($original);
$info['width'] = $size[0];
$info['height'] = $size[1];
if (!is_file($baseline)) {
$cmd = 'jpegtran -optimize -copy none -outfile ' . escapeshellarg($baseline) . ' ' . escapeshellarg($original);
exec($cmd, $result);
}
if (is_file($baseline)) {
$info['baseline'] = $baseline;
$info['baselineSize'] = filesize($baseline);
}
if (!is_file($progressive)) {
$cmd = 'jpegtran -progressive -optimize -copy none -outfile ' . escapeshellarg($progressive) . ' ' . escapeshellarg($original);
exec($cmd, $result);
}
if (is_file($progressive)) {
$info['progressive'] = $progressive;
$info['progressiveSize'] = filesize($progressive);
$file = fopen($progressive, 'rb');
if ($file) {
$bytes = fread($file, $info['progressiveSize']);
// process the jpeg markers
$i = 0;
$scan_start = 0;
$size = strlen($bytes);
while (FindNextScan($bytes, $size, $i)) {
$scan_length = $i - $scan_start;
$scans[] = substr($bytes, $scan_start, $scan_length);
$scan_start = $i;
}
fclose($file);
}
}
}
return $scans;
}
function FindNextScan(&$bytes, $size, &$i) {
$found = false;
if ($i < $size) {
while (!$found && FindNextMarker($bytes, $size, $i, $marker, $marker_length)) {
if (bin2hex($marker) == 'ffda') {
$found = true;
}
$i += $marker_length;
}
if (!$found) {
$found = true;
$i = $size;
}
}
return $found;
}
function FindNextMarker(&$bytes, $size, &$i, &$marker, &$marker_length) {
$marker_length = 0;
$marker = null;
$found = false;
$nolength = array('d0','d1','d2','d3','d4','d5','d6','d7','d8','d9','01');
$sos = 'da';
if ($i < $size) {
$val = dechex(ord($bytes[$i]));
if ($val == 'ff') {
$marker = $bytes[$i];
// ff can repeat, the actual marker comes from the first non-ff
while ($val == 'ff') {
$i++;
$val = dechex(ord($bytes[$i]));
}
$marker .= $bytes[$i];
$i++;
if (in_array($val, $nolength)) {
$found = true;
} elseif($val == $sos) {
// image data
$j = $i + 1;
$next_marker = $size;
while ($j < $size - 1 && !$found) {
$val = dechex(ord($bytes[$j]));
if ($val == 'ff') {
$k = $j + 1;
$val = dechex(ord($bytes[$k]));
if ($val != '00') { // escaping
while ($k < $size = 1 && $val == 'ff') {
$k++;
$val = dechex(ord($bytes[$k]));
}
$next_marker = $j;
$found = true;
}
}
$j++;
}
$marker_length = $next_marker - $i;
} elseif ($i + 1 < $size) {
$l1 = ord($bytes[$i]);
$l2 = ord($bytes[$i+1]);
$marker_length = $l1 * 256 + $l2;
$found = true;
}
}
}
return $found;
}
?>