forked from wes/phpimageresize
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfunction.resize.php
More file actions
executable file
·171 lines (127 loc) · 4.8 KB
/
Copy pathfunction.resize.php
File metadata and controls
executable file
·171 lines (127 loc) · 4.8 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
<?php
require 'ImagePath.php';
require 'Configuration.php';
require 'Resizer.php';
function sanitize($path) {
return urldecode($path);
}
function isInCache($path, $imagePath) {
$isInCache = false;
if(file_exists($path) == true):
$isInCache = true;
$origFileTime = date("YmdHis",filemtime($imagePath));
$newFileTime = date("YmdHis",filemtime($path));
if($newFileTime < $origFileTime): # Not using $opts['expire-time'] ??
$isInCache = false;
endif;
endif;
return $isInCache;
}
function composeNewPath($imagePath, $configuration) {
$w = $configuration->obtainWidth();
$h = $configuration->obtainHeight();
$filename = md5_file($imagePath);
$finfo = pathinfo($imagePath);
$ext = $finfo['extension'];
$cropSignal = isset($opts['crop']) && $opts['crop'] == true ? "_cp" : "";
$scaleSignal = isset($opts['scale']) && $opts['scale'] == true ? "_sc" : "";
$widthSignal = !empty($w) ? '_w'.$w : '';
$heightSignal = !empty($h) ? '_h'.$h : '';
$extension = '.'.$ext;
$newPath = $configuration->obtainCache() .$filename.$widthSignal.$heightSignal.$cropSignal.$scaleSignal.$extension;
if($opts['output-filename']) {
$newPath = $opts['output-filename'];
}
return $newPath;
}
function defaultShellCommand($configuration, $imagePath, $newPath) {
$opts = $configuration->asHash();
$w = $configuration->obtainWidth();
$h = $configuration->obtainHeight();
$command = $configuration->obtainConvertPath() ." " . escapeshellarg($imagePath) .
" -thumbnail ". (!empty($h) ? 'x':'') . $w ."".
(isset($opts['maxOnly']) && $opts['maxOnly'] == true ? "\>" : "") .
" -quality ". escapeshellarg($opts['quality']) ." ". escapeshellarg($newPath);
return $command;
}
function isPanoramic($imagePath) {
list($width,$height) = getimagesize($imagePath);
return $width > $height;
}
function composeResizeOptions($imagePath, $configuration) {
$opts = $configuration->asHash();
$w = $configuration->obtainWidth();
$h = $configuration->obtainHeight();
$resize = "x".$h;
$hasCrop = (true === $opts['crop']);
if(!$hasCrop && isPanoramic($imagePath)):
$resize = $w;
endif;
if($hasCrop && !isPanoramic($imagePath)):
$resize = $w;
endif;
return $resize;
}
function commandWithScale($imagePath, $newPath, $configuration) {
$opts = $configuration->asHash();
$resize = composeResizeOptions($imagePath, $configuration);
$cmd = $configuration->obtainConvertPath() ." ". escapeshellarg($imagePath) ." -resize ". escapeshellarg($resize) .
" -quality ". escapeshellarg($opts['quality']) . " " . escapeshellarg($newPath);
return $cmd;
}
function commandWithCrop($imagePath, $newPath, $configuration) {
$opts = $configuration->asHash();
$w = $configuration->obtainWidth();
$h = $configuration->obtainHeight();
$resize = composeResizeOptions($imagePath, $configuration);
$cmd = $configuration->obtainConvertPath() ." ". escapeshellarg($imagePath) ." -resize ". escapeshellarg($resize) .
" -size ". escapeshellarg($w ."x". $h) .
" xc:". escapeshellarg($opts['canvas-color']) .
" +swap -gravity center -composite -quality ". escapeshellarg($opts['quality'])." ".escapeshellarg($newPath);
return $cmd;
}
function doResize($imagePath, $newPath, $configuration) {
$opts = $configuration->asHash();
$w = $configuration->obtainWidth();
$h = $configuration->obtainHeight();
if(!empty($w) and !empty($h)):
$cmd = commandWithCrop($imagePath, $newPath, $configuration);
if(true === $opts['scale']):
$cmd = commandWithScale($imagePath, $newPath, $configuration);
endif;
else:
$cmd = defaultShellCommand($configuration, $imagePath, $newPath);
endif;
$c = exec($cmd, $output, $return_code);
if($return_code != 0) {
error_log("Tried to execute : $cmd, return code: $return_code, output: " . print_r($output, true));
throw new RuntimeException();
}
}
function resize($imagePath,$opts=null){
$path = new ImagePath($imagePath);
$configuration = new Configuration($opts);
$resizer = new Resizer($path, $configuration);
// This has to go to Configuration as Exception in initialization
if(empty($configuration->asHash()['output-filename']) && empty($w) && empty($h)) {
return 'cannot resize the image';
}
// This has to be done in resizer resize
try {
$imagePath = $resizer->obtainFilePath();
} catch (Exception $e) {
return 'image not found';
}
$newPath = composeNewPath($imagePath, $configuration);
$create = !isInCache($newPath, $imagePath);
if($create == true):
try {
doResize($imagePath, $newPath, $configuration);
} catch (Exception $e) {
return 'cannot resize the image';
}
endif;
// The new path must be the return value of resizer resize
$cacheFilePath = str_replace($_SERVER['DOCUMENT_ROOT'],'',$newPath);
return $cacheFilePath;
}