<?php
namespace PhpOffice\PhpSpreadsheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
> use ZipArchive;
class Drawing extends BaseDrawing
{
> const IMAGE_TYPES_CONVERTION_MAP = [
/**
> IMAGETYPE_GIF => IMAGETYPE_PNG,
* Path.
> IMAGETYPE_JPEG => IMAGETYPE_JPEG,
*
> IMAGETYPE_PNG => IMAGETYPE_PNG,
* @var string
> IMAGETYPE_BMP => IMAGETYPE_PNG,
*/
> ];
private $path;
>
/**
> * Whether or not we are dealing with a URL.
* Create a new Drawing.
> *
*/
> * @var bool
public function __construct()
> */
{
> private $isUrl;
// Initialise values
>
$this->path = '';
> /**
> $this->isUrl = false;
// Initialize parent
parent::__construct();
}
/**
* Get Filename.
*
* @return string
*/
public function getFilename()
{
return basename($this->path);
}
/**
* Get indexed filename (using image index).
< *
< * @return string
*/
< public function getIndexedFilename()
> public function getIndexedFilename(): string
{
< $fileName = $this->getFilename();
< $fileName = str_replace(' ', '_', $fileName);
<
< return str_replace('.' . $this->getExtension(), '', $fileName) . $this->getImageIndex() . '.' . $this->getExtension();
> return md5($this->path) . '.' . $this->getExtension();
}
/**
* Get Extension.
*
* @return string
*/
public function getExtension()
{
$exploded = explode('.', basename($this->path));
return $exploded[count($exploded) - 1];
}
/**
> * Get full filepath to store drawing in zip archive.
* Get Path.
> *
*
> * @return string
* @return string
> */
*/
> public function getMediaFilename()
public function getPath()
> {
{
> if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
return $this->path;
> throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
}
> }
>
/**
> return sprintf('image%d%s', $this->getImageIndex(), $this->getImageFileExtensionForSave());
* Set Path.
> }
*
>
* @param string $pValue File path
> /**
< * @param string $pValue File path
< * @param bool $pVerifyFile Verify file
< *
< * @throws PhpSpreadsheetException
< *
< * @return Drawing
< */
< public function setPath($pValue, $pVerifyFile = true)
< {
< if ($pVerifyFile) {
< if (file_exists($pValue)) {
< $this->path = $pValue;
<
< if ($this->width == 0 && $this->height == 0) {
< // Get width/height
< [$this->width, $this->height] = getimagesize($pValue);
> * @param string $path File path
> * @param bool $verifyFile Verify file
> * @param ZipArchive $zip Zip archive instance
> *
> * @return $this
> */
> public function setPath($path, $verifyFile = true, $zip = null)
> {
> if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) {
> // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
> if (filter_var($path, FILTER_VALIDATE_URL)) {
> $this->path = $path;
> // Implicit that it is a URL, rather store info than running check above on value in other places.
> $this->isUrl = true;
> $imageContents = file_get_contents($path);
> $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
> if ($filePath) {
> file_put_contents($filePath, $imageContents);
> if (file_exists($filePath)) {
> $this->setSizesAndType($filePath);
> unlink($filePath);
> }
> }
> } elseif (file_exists($path)) {
> $this->path = $path;
> $this->setSizesAndType($path);
> } elseif ($zip instanceof ZipArchive) {
> $zipPath = explode('#', $path)[1];
> if ($zip->locateName($zipPath) !== false) {
> $this->path = $path;
> $this->setSizesAndType($path);
} else {
< throw new PhpSpreadsheetException("File $pValue not found!");
> throw new PhpSpreadsheetException("File $path not found!");
}
} else {
< $this->path = $pValue;
> $this->path = $path;
}
return $this;
}
/**
> * Get isURL.
* Get hash code.
> */
*
> public function getIsURL(): bool
* @return string Hash code
> {
*/
> return $this->isUrl;
public function getHashCode()
> }
{
>
return md5(
> /**
$this->path .
> * Set isURL.
parent::getHashCode() .
> *
__CLASS__
> * @return $this
);
> */
}
> public function setIsURL(bool $isUrl): self
}
> {
> $this->isUrl = $isUrl;
>
> return $this;
> }
>
> /**
> }
>
> /**
> * Get Image Type for Save.
> */
> public function getImageTypeForSave(): int
> {
> if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
> throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
> }
>
> return self::IMAGE_TYPES_CONVERTION_MAP[$this->type];
> }
>
> /**
> * Get Image file extention for Save.
> */
> public function getImageFileExtensionForSave(bool $includeDot = true): string
> {
> if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
> throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
> }
>
> $result = image_type_to_extension(self::IMAGE_TYPES_CONVERTION_MAP[$this->type], $includeDot);
>
> return "$result";
> }
>
> /**
> * Get Image mime type.
> */
> public function getImageMimeType(): string
> {
> if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
> throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
> }
>
> return image_type_to_mime_type(self::IMAGE_TYPES_CONVERTION_MAP[$this->type]);