Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.94% |
31 / 33 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ZipFile | |
93.94% |
31 / 33 |
|
87.50% |
7 / 8 |
13.04 | |
0.00% |
0 / 1 |
| getPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setPath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getContents | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| getExtension | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMimeType | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| getIndexedFilename | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getZipFileOut | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getZipFileIn | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
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 | * @see https://github.com/PHPOffice/PHPPresentation |
| 14 | * |
| 15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace PhpOffice\PhpPresentation\Shape\Drawing; |
| 21 | |
| 22 | use PhpOffice\Common\File as CommonFile; |
| 23 | use PhpOffice\PhpPresentation\Exception\FileNotFoundException; |
| 24 | use ZipArchive; |
| 25 | |
| 26 | class ZipFile extends AbstractDrawingAdapter |
| 27 | { |
| 28 | /** |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $path; |
| 32 | |
| 33 | /** |
| 34 | * Get Path. |
| 35 | */ |
| 36 | public function getPath(): string |
| 37 | { |
| 38 | return $this->path; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Set Path. |
| 43 | * |
| 44 | * @param string $pValue File path |
| 45 | */ |
| 46 | public function setPath(string $pValue = ''): self |
| 47 | { |
| 48 | $this->path = $pValue; |
| 49 | |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | public function getContents(): string |
| 54 | { |
| 55 | if (!CommonFile::fileExists($this->getZipFileOut())) { |
| 56 | throw new FileNotFoundException($this->getZipFileOut()); |
| 57 | } |
| 58 | |
| 59 | $imageZip = new ZipArchive(); |
| 60 | $imageZip->open($this->getZipFileOut()); |
| 61 | $imageContents = $imageZip->getFromName($this->getZipFileIn()); |
| 62 | $imageZip->close(); |
| 63 | unset($imageZip); |
| 64 | |
| 65 | return $imageContents; |
| 66 | } |
| 67 | |
| 68 | public function getExtension(): string |
| 69 | { |
| 70 | return pathinfo($this->getZipFileIn(), PATHINFO_EXTENSION); |
| 71 | } |
| 72 | |
| 73 | public function getMimeType(): string |
| 74 | { |
| 75 | if (!CommonFile::fileExists($this->getZipFileOut())) { |
| 76 | throw new FileNotFoundException($this->getZipFileOut()); |
| 77 | } |
| 78 | $oArchive = new ZipArchive(); |
| 79 | $oArchive->open($this->getZipFileOut()); |
| 80 | if (!function_exists('getimagesizefromstring')) { |
| 81 | $uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($this->getZipFileIn())); |
| 82 | $image = getimagesize($uri); |
| 83 | } else { |
| 84 | $image = getimagesizefromstring($oArchive->getFromName($this->getZipFileIn())); |
| 85 | } |
| 86 | |
| 87 | return image_type_to_mime_type($image[2]); |
| 88 | } |
| 89 | |
| 90 | public function getIndexedFilename(): string |
| 91 | { |
| 92 | $output = pathinfo($this->getZipFileIn(), PATHINFO_FILENAME); |
| 93 | $output = str_replace('.' . $this->getExtension(), '', $output); |
| 94 | $output .= $this->getImageIndex(); |
| 95 | $output .= '.' . $this->getExtension(); |
| 96 | $output = str_replace(' ', '_', $output); |
| 97 | |
| 98 | return $output; |
| 99 | } |
| 100 | |
| 101 | protected function getZipFileOut(): string |
| 102 | { |
| 103 | $path = str_replace('zip://', '', $this->getPath()); |
| 104 | $path = explode('#', $path); |
| 105 | |
| 106 | return empty($path[0]) ? '' : $path[0]; |
| 107 | } |
| 108 | |
| 109 | protected function getZipFileIn(): string |
| 110 | { |
| 111 | $path = str_replace('zip://', '', $this->getPath()); |
| 112 | $path = explode('#', $path); |
| 113 | |
| 114 | return empty($path[1]) ? '' : $path[1]; |
| 115 | } |
| 116 | } |