1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU Lesser General Public |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16
|
|
|
* Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Lesser General Public |
19
|
|
|
* License along with this library; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21
|
|
|
* |
22
|
|
|
* @category PhpSpreadsheet |
23
|
|
|
* |
24
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
25
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
26
|
|
|
*/ |
27
|
|
|
class Drawing extends BaseDrawing implements \PhpOffice\PhpSpreadsheet\IComparable |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Path. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $path; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new Drawing. |
38
|
|
|
*/ |
39
|
13 |
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
// Initialise values |
42
|
13 |
|
$this->path = ''; |
43
|
|
|
|
44
|
|
|
// Initialize parent |
45
|
13 |
|
parent::__construct(); |
46
|
13 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get Filename. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
6 |
|
public function getFilename() |
54
|
|
|
{ |
55
|
6 |
|
return basename($this->path); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get indexed filename (using image index). |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
7 |
|
public function getIndexedFilename() |
64
|
|
|
{ |
65
|
7 |
|
$fileName = $this->getFilename(); |
66
|
7 |
|
$fileName = str_replace(' ', '_', $fileName); |
67
|
|
|
|
68
|
7 |
|
return str_replace('.' . $this->getExtension(), '', $fileName) . $this->getImageIndex() . '.' . $this->getExtension(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get Extension. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
6 |
|
public function getExtension() |
77
|
|
|
{ |
78
|
6 |
|
$exploded = explode('.', basename($this->path)); |
79
|
|
|
|
80
|
6 |
|
return $exploded[count($exploded) - 1]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Path. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
12 |
|
public function getPath() |
89
|
|
|
{ |
90
|
12 |
|
return $this->path; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set Path. |
95
|
|
|
* |
96
|
|
|
* @param string $pValue File path |
97
|
|
|
* @param bool $pVerifyFile Verify file |
98
|
|
|
* |
99
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
100
|
|
|
* |
101
|
|
|
* @return Drawing |
102
|
|
|
*/ |
103
|
13 |
View Code Duplication |
public function setPath($pValue = '', $pVerifyFile = true) |
|
|
|
|
104
|
|
|
{ |
105
|
13 |
|
if ($pVerifyFile) { |
106
|
12 |
|
if (file_exists($pValue)) { |
107
|
12 |
|
$this->path = $pValue; |
108
|
|
|
|
109
|
12 |
|
if ($this->width == 0 && $this->height == 0) { |
110
|
|
|
// Get width/height |
111
|
12 |
|
list($this->width, $this->height) = getimagesize($pValue); |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
throw new \PhpOffice\PhpSpreadsheet\Exception("File $pValue not found!"); |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
3 |
|
$this->path = $pValue; |
118
|
|
|
} |
119
|
|
|
|
120
|
13 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get hash code. |
125
|
|
|
* |
126
|
|
|
* @return string Hash code |
127
|
|
|
*/ |
128
|
6 |
|
public function getHashCode() |
129
|
|
|
{ |
130
|
6 |
|
return md5( |
131
|
6 |
|
$this->path . |
132
|
6 |
|
parent::getHashCode() . |
133
|
6 |
|
__CLASS__ |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function __clone() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$vars = get_object_vars($this); |
143
|
|
|
foreach ($vars as $key => $value) { |
144
|
|
|
if (is_object($value)) { |
145
|
|
|
$this->$key = clone $value; |
146
|
|
|
} else { |
147
|
|
|
$this->$key = $value; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.