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