1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\IComparable; |
7
|
|
|
|
8
|
|
|
class Drawing extends BaseDrawing implements IComparable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Path. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $path; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new Drawing. |
19
|
|
|
*/ |
20
|
1 |
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
// Initialise values |
23
|
1 |
|
$this->path = ''; |
24
|
|
|
|
25
|
|
|
// Initialize parent |
26
|
1 |
|
parent::__construct(); |
27
|
1 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get Filename. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getFilename() |
35
|
|
|
{ |
36
|
|
|
return basename($this->path); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get indexed filename (using image index). |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getIndexedFilename() |
45
|
|
|
{ |
46
|
|
|
$fileName = $this->getFilename(); |
47
|
|
|
$fileName = str_replace(' ', '_', $fileName); |
48
|
|
|
|
49
|
|
|
return str_replace('.' . $this->getExtension(), '', $fileName) . $this->getImageIndex() . '.' . $this->getExtension(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get Extension. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getExtension() |
58
|
|
|
{ |
59
|
|
|
$exploded = explode('.', basename($this->path)); |
60
|
|
|
|
61
|
|
|
return $exploded[count($exploded) - 1]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get Path. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public function getPath() |
70
|
|
|
{ |
71
|
1 |
|
return $this->path; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set Path. |
76
|
|
|
* |
77
|
|
|
* @param string $pValue File path |
78
|
|
|
* @param bool $pVerifyFile Verify file |
79
|
|
|
* |
80
|
|
|
* @throws PhpSpreadsheetException |
81
|
|
|
* |
82
|
|
|
* @return Drawing |
83
|
|
|
*/ |
84
|
1 |
View Code Duplication |
public function setPath($pValue, $pVerifyFile = true) |
|
|
|
|
85
|
|
|
{ |
86
|
1 |
|
if ($pVerifyFile) { |
87
|
1 |
|
if (file_exists($pValue)) { |
88
|
1 |
|
$this->path = $pValue; |
89
|
|
|
|
90
|
1 |
|
if ($this->width == 0 && $this->height == 0) { |
91
|
|
|
// Get width/height |
92
|
1 |
|
list($this->width, $this->height) = getimagesize($pValue); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
1 |
|
throw new PhpSpreadsheetException("File $pValue not found!"); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
$this->path = $pValue; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get hash code. |
106
|
|
|
* |
107
|
|
|
* @return string Hash code |
108
|
|
|
*/ |
109
|
|
|
public function getHashCode() |
110
|
|
|
{ |
111
|
|
|
return md5( |
112
|
|
|
$this->path . |
113
|
|
|
parent::getHashCode() . |
114
|
|
|
__CLASS__ |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
public function __clone() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$vars = get_object_vars($this); |
124
|
|
|
foreach ($vars as $key => $value) { |
125
|
|
|
if (is_object($value)) { |
126
|
|
|
$this->$key = clone $value; |
127
|
|
|
} else { |
128
|
|
|
$this->$key = $value; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.