|
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
|
|
|
/** |
|
15
|
|
|
* Image helper. |
|
16
|
|
|
* |
|
17
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
18
|
|
|
* @package WBW\Library\Core\Utility |
|
19
|
|
|
*/ |
|
20
|
|
|
class ImageHelper { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Create new dimensions. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Image $image The image. |
|
26
|
|
|
* @param int $maxWidth The maximum width. |
|
27
|
|
|
* @param int $maxHeight The maximum height. |
|
28
|
|
|
* @return int[] Returns the dimensions. |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function newDimensions(Image $image, $maxWidth, $maxHeight) { |
|
31
|
|
|
|
|
32
|
|
|
$image->init(); |
|
33
|
|
|
|
|
34
|
|
|
if ($image->getWidth() < $maxWidth || $image->getHeight() < $maxHeight) { |
|
35
|
|
|
return [$image->getWidth(), $image->getHeight()]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (null === $image->getOrientation()) { |
|
39
|
|
|
$max = max($maxWidth, $maxHeight); |
|
40
|
|
|
return [$max, $max]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$ratio = $image->getWidth() / $image->getHeight(); |
|
44
|
|
|
|
|
45
|
|
|
$width = $maxWidth; |
|
46
|
|
|
$height = $maxHeight; |
|
47
|
|
|
|
|
48
|
|
|
if (ImageInterface::ORIENTATION_HORIZONTAL === $image->getOrientation()) { |
|
49
|
|
|
$height = intval($width / $ratio); |
|
50
|
|
|
} else { |
|
51
|
|
|
$width = intval($height * $ratio); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return [$width, $height]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create an input stream. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Image $image The image. |
|
61
|
|
|
* @return resource|null Returns the input stream in case of success, null otherwise. |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function newInputStream(Image $image) { |
|
64
|
|
|
|
|
65
|
|
|
$stream = null; |
|
66
|
|
|
|
|
67
|
|
|
switch ($image->init()->getMimeType()) { |
|
68
|
|
|
|
|
69
|
|
|
case ImageInterface::MIME_TYPE_JPEG: |
|
70
|
|
|
$stream = imagecreatefromjpeg($image->getPathname()); |
|
71
|
|
|
break; |
|
72
|
|
|
|
|
73
|
|
|
case ImageInterface::MIME_TYPE_PNG: |
|
74
|
|
|
$stream = imagecreatefrompng($image->getPathname()); |
|
75
|
|
|
if (false !== $stream) { |
|
76
|
|
|
imagealphablending($stream, true); |
|
77
|
|
|
} |
|
78
|
|
|
break; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return false !== $stream ? $stream : null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create an output stream. |
|
86
|
|
|
* |
|
87
|
|
|
* @param Image $image the image. |
|
88
|
|
|
* @param int $width The width. |
|
89
|
|
|
* @param int $height The height. |
|
90
|
|
|
* @return resource|null Returns the output stream in case of success, null otherwise. |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function newOutputStream(Image $image, $width, $height) { |
|
93
|
|
|
|
|
94
|
|
|
$stream = imagecreatetruecolor($width, $height); |
|
95
|
|
|
|
|
96
|
|
|
switch ($image->init()->getMimeType()) { |
|
97
|
|
|
|
|
98
|
|
|
case ImageInterface::MIME_TYPE_PNG: |
|
99
|
|
|
if (false !== $stream) { |
|
100
|
|
|
imagealphablending($stream, false); |
|
101
|
|
|
imagesavealpha($stream, true); |
|
102
|
|
|
} |
|
103
|
|
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false !== $stream ? $stream : null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Save an output stream. |
|
111
|
|
|
* |
|
112
|
|
|
* @param Image $image the image. |
|
113
|
|
|
* @param resource $outputStream The output stream. |
|
114
|
|
|
* @param string $pathname The pathname. |
|
115
|
|
|
* @return bool Returns true in case of success, false otherwise. |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function saveOutputStream(Image $image, $outputStream, $pathname) { |
|
118
|
|
|
|
|
119
|
|
|
switch ($image->init()->getMimeType()) { |
|
120
|
|
|
|
|
121
|
|
|
case ImageInterface::MIME_TYPE_JPEG: |
|
122
|
|
|
return imagejpeg($outputStream, $pathname); |
|
123
|
|
|
|
|
124
|
|
|
case ImageInterface::MIME_TYPE_PNG: |
|
125
|
|
|
return imagepng($outputStream, $pathname); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|