Completed
Push — master ( 635b22...ba6ffc )
by WEBEWEB
02:00
created

ImageHelper   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 1
dl 0
loc 111
rs 10
c 0
b 0
f 0

4 Methods

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