|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the core-library package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2019 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Utility; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
use WBW\Library\Core\Model\Attribute\IntegerHeightTrait; |
|
17
|
|
|
use WBW\Library\Core\Model\Attribute\IntegerSizeTrait; |
|
18
|
|
|
use WBW\Library\Core\Model\Attribute\IntegerWidthTrait; |
|
19
|
|
|
use WBW\Library\Core\Model\Attribute\StringDirectoryTrait; |
|
20
|
|
|
use WBW\Library\Core\Model\Attribute\StringExtensionTrait; |
|
21
|
|
|
use WBW\Library\Core\Model\Attribute\StringFilenameTrait; |
|
22
|
|
|
use WBW\Library\Core\Model\Attribute\StringMimeTypeTrait; |
|
23
|
|
|
use WBW\Library\Core\Model\Attribute\StringPathnameTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Image. |
|
27
|
|
|
* |
|
28
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
29
|
|
|
* @package WBW\Library\Core\Utility |
|
30
|
|
|
*/ |
|
31
|
|
|
class Image implements ImageInterface { |
|
32
|
|
|
|
|
33
|
|
|
use IntegerHeightTrait; |
|
34
|
|
|
use IntegerSizeTrait; |
|
35
|
|
|
use IntegerWidthTrait; |
|
36
|
|
|
use StringDirectoryTrait; |
|
37
|
|
|
use StringExtensionTrait; |
|
38
|
|
|
use StringFilenameTrait; |
|
39
|
|
|
use StringMimeTypeTrait; |
|
40
|
|
|
use StringPathnameTrait; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $pathname The pathname. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($pathname) { |
|
48
|
|
|
$this->setPathname($pathname); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the dimensions. |
|
53
|
|
|
* |
|
54
|
|
|
* @return int[] Returns the dimensions. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getDimensions() { |
|
57
|
|
|
return [$this->getWidth(), $this->getHeight()]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the orientation. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string|null Returns the orientation, null if width and height are equals. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getOrientation() { |
|
66
|
|
|
|
|
67
|
|
|
if ($this->getHeight() < $this->getWidth()) { |
|
68
|
|
|
return self::ORIENTATION_HORIZONTAL; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($this->getWidth() < $this->getHeight()) { |
|
72
|
|
|
return self::ORIENTATION_VERTICAL; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Initialize. |
|
80
|
|
|
* |
|
81
|
|
|
* @return Image Returns this image. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function init() { |
|
84
|
|
|
|
|
85
|
|
|
if (null !== $this->getDirectory()) { |
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->setDirectory(dirname($this->getPathname())); |
|
90
|
|
|
$this->setFilename(basename($this->getPathname())); |
|
91
|
|
|
|
|
92
|
|
|
$parts = explode(".", $this->getFilename()); |
|
93
|
|
|
$this->setExtension(end($parts)); |
|
94
|
|
|
|
|
95
|
|
|
$this->setMimeType(mime_content_type($this->getPathname())); |
|
96
|
|
|
|
|
97
|
|
|
list($width, $height) = getimagesize($this->getPathname()); |
|
98
|
|
|
$this->setWidth($width); |
|
99
|
|
|
$this->setHeight($height); |
|
100
|
|
|
|
|
101
|
|
|
$this->setSize(filesize($this->getPathname())); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Resize. |
|
108
|
|
|
* |
|
109
|
|
|
* @param int $newWidth The new width. |
|
110
|
|
|
* @param int $newHeight The new height. |
|
111
|
|
|
* @param string $pathname The pathname. |
|
112
|
|
|
* @return bool Returns true in case of success, false otherwise. |
|
113
|
|
|
* @throws RuntimeException Throws a runtime exception if the re-sampled copy failed. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function resize($newWidth, $newHeight, $pathname) { |
|
116
|
|
|
|
|
117
|
|
|
list($w, $h) = ImageHelper::newDimensions($this, $newWidth, $newHeight); |
|
118
|
|
|
|
|
119
|
|
|
$input = ImageHelper::newInputStream($this); |
|
120
|
|
|
$output = ImageHelper::newOutputStream($this, $w, $h); |
|
121
|
|
|
if (null === $output) { |
|
122
|
|
|
throw new RuntimeException("Failed to create true color image"); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$result = imagecopyresampled($output, $input, 0, 0, 0, 0, $w, $h, $this->getWidth(), $this->getHeight()); |
|
126
|
|
|
if (false === $result) { |
|
127
|
|
|
throw new RuntimeException("Failed to copy re-sampled image"); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return ImageHelper::saveOutputStream($this, $output, $pathname); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set the pathname. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $pathname The pathname. |
|
137
|
|
|
* @return Image Returns this image. |
|
138
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if the pathname was not found. |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function setPathname($pathname) { |
|
141
|
|
|
if (false === realpath($pathname)) { |
|
142
|
|
|
throw new InvalidArgumentException(sprintf("The image \"%s\" was not found", $pathname)); |
|
143
|
|
|
} |
|
144
|
|
|
$this->pathname = realpath($pathname); |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|