Resize::SetPreserveAspectRatio()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Resize
4
 *
5
 * @Author: Jens Kooij
6
 * @Version: 1.0
7
 * @package: JNS MVC
8
 * @Licence: http://creativecommons.org/licenses/by-nc-nd/3.0/ Attribution-NonCommercial-NoDerivs 3.0 Unported
9
 */
10
11
namespace CloudControl\Cms\images\methods {
12
13
    use CloudControl\Cms\images\IMethod;
14
15
    class Resize extends IMethod
16
    {
17
        protected $_width;
18
        protected $_height;
19
        protected $_preserveAspectRatio = true;
20
21
        /**
22
         * Set the width
23
         *
24
         * @param  int $width
25
         * @return self
26
         */
27
        public function SetWidth($width)
28
        {
29
            $this->_width = (int)$width;
30
            return $this;
31
        }
32
33
        /**
34
         * Set the height
35
         *
36
         * @param  int $height
37
         * @return self
38
         */
39
        public function SetHeight($height)
40
        {
41
            $this->_height = (int)$height;
42
            return $this;
43
        }
44
45
        /**
46
         * Sets wheter or not the aspect ratio of the original
47
         * image needs to preserved
48
         *
49
         * @param  bool $bool
50
         * @return self
51
         */
52
        public function SetPreserveAspectRatio($bool)
53
        {
54
            $this->_preserveAspectRatio = (bool)$bool;
55
            return $this;
56
        }
57
58
        public function Execute($imageResource)
59
        {
60
            // Define the origial width and height
61
            $originalWidth = imagesx($imageResource);
62
            $originalHeight = imagesy($imageResource);
63
64
            // Define the ratio and adjust the width and height
65
            if ($this->_preserveAspectRatio) {
66
                $ratio = min($this->_width / $originalWidth, $this->_height / $originalHeight);
67
                $this->_width = $originalWidth * $ratio;
68
                $this->_height = $originalHeight * $ratio;
69
            }
70
71
            // Create the new image
72
            $new = imagecreatetruecolor($this->_width, $this->_height);
73
74
            // Preserve transparency
75
            imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
0 ignored issues
show
Bug introduced by
It seems like $new can also be of type false; however, parameter $image of imagecolortransparent() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            imagecolortransparent(/** @scrutinizer ignore-type */ $new, imagecolorallocatealpha($new, 0, 0, 0, 127));
Loading history...
Bug introduced by
It seems like $new can also be of type false; however, parameter $image of imagecolorallocatealpha() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            imagecolortransparent($new, imagecolorallocatealpha(/** @scrutinizer ignore-type */ $new, 0, 0, 0, 127));
Loading history...
76
            imagealphablending($new, false);
0 ignored issues
show
Bug introduced by
It seems like $new can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            imagealphablending(/** @scrutinizer ignore-type */ $new, false);
Loading history...
77
            imagesavealpha($new, true);
0 ignored issues
show
Bug introduced by
It seems like $new can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            imagesavealpha(/** @scrutinizer ignore-type */ $new, true);
Loading history...
78
79
            // Do the actual resizing
80
            imagecopyresampled($new, $imageResource, 0, 0, 0, 0, $this->_width, $this->_height, $originalWidth,
0 ignored issues
show
Bug introduced by
It seems like $new can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            imagecopyresampled(/** @scrutinizer ignore-type */ $new, $imageResource, 0, 0, 0, 0, $this->_width, $this->_height, $originalWidth,
Loading history...
81
                $originalHeight);
82
83
            return $new;
84
        }
85
    }
86
}