Drawing.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. class Drawing extends BaseDrawing
  5. {
  6. /**
  7. * Path.
  8. *
  9. * @var string
  10. */
  11. private $path;
  12. /**
  13. * Create a new Drawing.
  14. */
  15. public function __construct()
  16. {
  17. // Initialise values
  18. $this->path = '';
  19. // Initialize parent
  20. parent::__construct();
  21. }
  22. /**
  23. * Get Filename.
  24. *
  25. * @return string
  26. */
  27. public function getFilename()
  28. {
  29. return basename($this->path);
  30. }
  31. /**
  32. * Get indexed filename (using image index).
  33. *
  34. * @return string
  35. */
  36. public function getIndexedFilename()
  37. {
  38. $fileName = $this->getFilename();
  39. $fileName = str_replace(' ', '_', $fileName);
  40. return str_replace('.' . $this->getExtension(), '', $fileName) . $this->getImageIndex() . '.' . $this->getExtension();
  41. }
  42. /**
  43. * Get Extension.
  44. *
  45. * @return string
  46. */
  47. public function getExtension()
  48. {
  49. $exploded = explode('.', basename($this->path));
  50. return $exploded[count($exploded) - 1];
  51. }
  52. /**
  53. * Get Path.
  54. *
  55. * @return string
  56. */
  57. public function getPath()
  58. {
  59. return $this->path;
  60. }
  61. /**
  62. * Set Path.
  63. *
  64. * @param string $pValue File path
  65. * @param bool $pVerifyFile Verify file
  66. *
  67. * @throws PhpSpreadsheetException
  68. *
  69. * @return Drawing
  70. */
  71. public function setPath($pValue, $pVerifyFile = true)
  72. {
  73. if ($pVerifyFile) {
  74. if (file_exists($pValue)) {
  75. $this->path = $pValue;
  76. if ($this->width == 0 && $this->height == 0) {
  77. // Get width/height
  78. list($this->width, $this->height) = getimagesize($pValue);
  79. }
  80. } else {
  81. throw new PhpSpreadsheetException("File $pValue not found!");
  82. }
  83. } else {
  84. $this->path = $pValue;
  85. }
  86. return $this;
  87. }
  88. /**
  89. * Get hash code.
  90. *
  91. * @return string Hash code
  92. */
  93. public function getHashCode()
  94. {
  95. return md5(
  96. $this->path .
  97. parent::getHashCode() .
  98. __CLASS__
  99. );
  100. }
  101. }