forked from wes/phpimageresize
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathResizer.php
More file actions
74 lines (56 loc) · 2.2 KB
/
Copy pathResizer.php
File metadata and controls
74 lines (56 loc) · 2.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
<?php
require 'FileSystem.php';
class Resizer {
private $path;
private $configuration;
private $fileSystem;
public function __construct($path, $configuration=null) {
if ($configuration == null) $configuration = new Configuration();
$this->checkPath($path);
$this->checkConfiguration($configuration);
$this->path = $path;
$this->configuration = $configuration;
$this->fileSystem = new FileSystem();
}
public function injectFileSystem(FileSystem $fileSystem) {
$this->fileSystem = $fileSystem;
}
public function obtainFilePath() {
$imagePath = '';
if($this->path->isHttpProtocol()):
$filename = $this->path->obtainFileName();
$local_filepath = $this->configuration->obtainRemote() .$filename;
$inCache = $this->isInCache($local_filepath);
if(!$inCache):
$this->download($local_filepath);
endif;
$imagePath = $local_filepath;
endif;
if(!$this->fileSystem->file_exists($imagePath)):
$imagePath = $_SERVER['DOCUMENT_ROOT'].$imagePath;
if(!$this->fileSystem->file_exists($imagePath)):
throw new RuntimeException();
endif;
endif;
return $imagePath;
}
private function download($filePath) {
$img = $this->fileSystem->file_get_contents($this->path->sanitizedPath());
$this->fileSystem->file_put_contents($filePath,$img);
}
private function isInCache($filePath) {
$fileExists = $this->fileSystem->file_exists($filePath);
$fileValid = $this->fileNotExpired($filePath);
return $fileExists && $fileValid;
}
private function fileNotExpired($filePath) {
$cacheMinutes = $this->configuration->obtainCacheMinutes();
$this->fileSystem->filemtime($filePath) < strtotime('+'. $cacheMinutes. ' minutes');
}
private function checkPath($path) {
if (!($path instanceof ImagePath)) throw new InvalidArgumentException();
}
private function checkConfiguration($configuration) {
if (!($configuration instanceof Configuration)) throw new InvalidArgumentException();
}
}