Completed
Push — master ( ea8464...30c3c3 )
by WEBEWEB
01:20
created

ImageHelper::newInputStream()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6346
c 0
b 0
f 0
cc 7
nc 12
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-bundle 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\Bundle\CoreBundle\Model;
13
14
/**
15
 * Image helper.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Bundle\CoreBundle\Model
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
        if ($image->getWidth() < $maxWidth || $image->getHeight() < $maxHeight) {
33
            return [$image->getWidth(), $image->getHeight()];
34
        }
35
36
        if (null === $image->getOrientation()) {
37
            $max = max($maxWidth, $maxHeight);
38
            return [$max, $max];
39
        }
40
41
        $ratio = $image->getWidth() / $image->getHeight();
42
43
        $width  = $maxWidth;
44
        $height = $maxHeight;
45
46
        if (Image::ORIENTATION_HORIZONTAL === $image->getOrientation()) {
47
            $height = abs($width / $ratio);
48
        } else {
49
            $width = abs($height * $ratio);
50
        }
51
52
        return [$width, $height];
53
    }
54
55
    /**
56
     * Create an input stream.
57
     *
58
     * @param Image $image The image.
59
     * @return resource|null Returns the input stream in case of success, null otherwise.
60
     */
61
    public static function newInputStream(Image $image) {
62
63
        $stream = null;
64
65
        switch ($image->getExtension()) {
66
67
            case "jpe":
68
            case "jpeg":
69
            case "jpg":
70
                $stream = imagecreatefromjpeg($image->getPathname());
71
                break;
72
73
            case "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->getExtension()) {
97
98
            case "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->getExtension()) {
120
121
            case "jpe":
122
            case "jpeg":
123
            case "jpg":
124
                return imagejpeg($outputStream, $pathname);
125
126
            case "png":
127
                return imagepng($outputStream, $pathname);
128
        }
129
130
        return false;
131
    }
132
}
133