Completed
Pull Request — develop (#207)
by Franck
12:42 queued 05:23
created

AbstractGraphic::setWidthAndHeight()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 11
nc 3
nop 2
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Shape;
19
20
use PhpOffice\PhpPresentation\AbstractShape;
21
use PhpOffice\PhpPresentation\ComparableInterface;
22
23
/**
24
 * Abstract drawing
25
 */
26
abstract class AbstractGraphic extends AbstractShape implements ComparableInterface
27
{
28
    /**
29
     * Image counter
30
     *
31
     * @var int
32
     */
33
    private static $imageCounter = 0;
34
35
    /**
36
     * Image index
37
     *
38
     * @var int
39
     */
40
    private $imageIndex = 0;
41
42
    /**
43
     * Name
44
     *
45
     * @var string
46
     */
47
    protected $name;
48
49
    /**
50
     * Description
51
     *
52
     * @var string
53
     */
54
    protected $description;
55
56
    /**
57
     * Proportional resize
58
     *
59
     * @var boolean
60
     */
61
    protected $resizeProportional;
62
63
    /**
64
     * Slide relation ID (should not be used by user code!)
65
     *
66
     * @var string
67
     */
68
    public $relationId = null;
69
70
    /**
71
     * Create a new \PhpOffice\PhpPresentation\Slide\AbstractDrawing
72
     */
73
    public function __construct()
74
    {
75
        // Initialise values
76
        $this->name               = '';
77
        $this->description        = '';
78
        $this->resizeProportional = true;
79
80
        // Set image index
81
        self::$imageCounter++;
82
        $this->imageIndex = self::$imageCounter;
83
84
        // Initialize parent
85
        parent::__construct();
86
    }
87
    
88
    public function __clone()
89
    {
90
        parent::__clone();
91
        
92
        self::$imageCounter++;
93
        $this->imageIndex = self::$imageCounter;
94
    }
95
96
    /**
97
     * Get image index
98
     *
99
     * @return int
100
     */
101
    public function getImageIndex()
102
    {
103
        return $this->imageIndex;
104
    }
105
106
    /**
107
     * Get Name
108
     *
109
     * @return string
110
     */
111
    public function getName()
112
    {
113
        return $this->name;
114
    }
115
116
    /**
117
     * Set Name
118
     *
119
     * @param  string                          $pValue
120
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
121
     */
122
    public function setName($pValue = '')
123
    {
124
        $this->name = $pValue;
125
        return $this;
126
    }
127
128
    /**
129
     * Get Description
130
     *
131
     * @return string
132
     */
133
    public function getDescription()
134
    {
135
        return $this->description;
136
    }
137
138
    /**
139
     * Set Description
140
     *
141
     * @param  string                          $pValue
142
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
143
     */
144
    public function setDescription($pValue = '')
145
    {
146
        $this->description = $pValue;
147
148
        return $this;
149
    }
150
151
    /**
152
     * Set Width
153
     *
154
     * @param  int                             $pValue
155
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
156
     */
157
    public function setWidth($pValue = 0)
158
    {
159
        // Resize proportional?
160
        if ($this->resizeProportional && $pValue != 0) {
161
            $ratio         = $this->height / $this->width;
162
            $this->height = (int) round($ratio * $pValue);
163
        }
164
165
        // Set width
166
        $this->width = $pValue;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Set Height
173
     *
174
     * @param  int                             $pValue
175
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
176
     */
177
    public function setHeight($pValue = 0)
178
    {
179
        // Resize proportional?
180
        if ($this->resizeProportional && $pValue != 0) {
181
            $ratio        = $this->width / $this->height;
182
            $this->width = (int) round($ratio * $pValue);
183
        }
184
185
        // Set height
186
        $this->height = $pValue;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Set width and height with proportional resize
193
     * @author Vincent@luo MSN:[email protected]
194
     * @param  int                             $width
195
     * @param  int                             $height
196
     * @example $objDrawing->setResizeProportional(true);
197
     * @example $objDrawing->setWidthAndHeight(160,120);
198
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
199
     */
200
    public function setWidthAndHeight($width = 0, $height = 0)
201
    {
202
        $xratio = $width / $this->width;
203
        $yratio = $height / $this->height;
204
        if ($this->resizeProportional && !($width == 0 || $height == 0)) {
205
            if (($xratio * $this->height) < $height) {
206
                $this->height = (int) ceil($xratio * $this->height);
207
                $this->width  = $width;
208
            } else {
209
                $this->width  = (int) ceil($yratio * $this->width);
210
                $this->height = $height;
211
            }
212
        }
213
214
        return $this;
215
    }
216
217
    /**
218
     * Get ResizeProportional
219
     *
220
     * @return boolean
221
     */
222
    public function isResizeProportional()
223
    {
224
        return $this->resizeProportional;
225
    }
226
227
    /**
228
     * Set ResizeProportional
229
     *
230
     * @param  boolean                         $pValue
231
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
232
     */
233
    public function setResizeProportional($pValue = true)
234
    {
235
        $this->resizeProportional = $pValue;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Get hash code
242
     *
243
     * @return string Hash code
244
     */
245
    public function getHashCode()
246
    {
247
        return md5($this->name . $this->description . parent::getHashCode() . __CLASS__);
248
    }
249
}
250