-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransform.php
More file actions
91 lines (76 loc) · 2 KB
/
Copy pathTransform.php
File metadata and controls
91 lines (76 loc) · 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace x000000\StorageManager;
/**
* @method Transform resize(int? $width, int? $height) resize
* @method Transform crop(int? $width, int? $height, int|string $x, int|string $y, int|string $ratio = null) crop
*/
class Transform
{
private $_storage;
private $_source;
private $_transforms = [];
private $_rawUrl;
private $_url;
public static $transformMap = [
'resize' => Transforms\Resize::class,
'crop' => Transforms\Crop::class,
];
public function __construct(Storage $storage, $source)
{
$this->_storage = $storage;
$this->_source = $source;
}
public function __toString()
{
return $this->url();
}
public function url()
{
if ($this->_url === null) {
if (empty($this->_source)) {
return $this->_url = $this->_rawUrl = false;
}
if (empty($this->_transforms)) {
// no transform given so we can return url to the source file
return $this->_url = $this->_rawUrl = $this->_storage->getSource($this->_source);
}
if (!$path = $this->_storage->getThumb($this->_source, $this->_transforms)) {
return $this->_url = $this->_rawUrl = false;
}
$this->_rawUrl = $path;
// we should encode file name so it won't break anything
$path = explode('/', $path);
$path[] = urlencode( array_pop($path) );
return $this->_url = implode('/', $path);
}
return $this->_url;
}
public function rawUrl()
{
if ($this->_url === null) {
$this->url();
}
return $this->_rawUrl;
}
public function getTransforms()
{
return $this->_transforms;
}
public function add(Transforms\AbstractTransform $transform)
{
if ($this->_url !== null) {
throw new \BadMethodCallException('Transforms already applied');
}
$this->_transforms[] = $transform;
}
public function __call($name, $arguments)
{
if (isset(self::$transformMap[$name])) {
$class = self::$transformMap[$name];
$this->add(new $class(... $arguments));
return $this;
} else {
throw new \BadMethodCallException('Method ' . self::class . "::$name() is not exists");
}
}
}