Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
27 / 30 |
|
83.33% |
10 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Gd | |
90.00% |
27 / 30 |
|
83.33% |
10 / 12 |
14.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getImageResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setImageResource | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getRenderingFunction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setRenderingFunction | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMimeType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getContents | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| getExtension | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getIndexedFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPath | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setPath | |
0.00% |
0 / 2 |
|
0.00% |
0 / 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 | class Gd extends AbstractDrawingAdapter |
| 23 | { |
| 24 | // Rendering functions |
| 25 | public const RENDERING_DEFAULT = 'imagepng'; |
| 26 | public const RENDERING_PNG = 'imagepng'; |
| 27 | public const RENDERING_GIF = 'imagegif'; |
| 28 | public const RENDERING_JPEG = 'imagejpeg'; |
| 29 | |
| 30 | // MIME types |
| 31 | public const MIMETYPE_DEFAULT = 'image/png'; |
| 32 | public const MIMETYPE_PNG = 'image/png'; |
| 33 | public const MIMETYPE_GIF = 'image/gif'; |
| 34 | public const MIMETYPE_JPEG = 'image/jpeg'; |
| 35 | |
| 36 | /** |
| 37 | * Image resource. |
| 38 | * |
| 39 | * @var resource |
| 40 | */ |
| 41 | protected $imageResource; |
| 42 | |
| 43 | /** |
| 44 | * Rendering function. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $renderingFunction = self::RENDERING_DEFAULT; |
| 49 | |
| 50 | /** |
| 51 | * Mime type. |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | protected $mimeType = self::MIMETYPE_DEFAULT; |
| 56 | |
| 57 | /** |
| 58 | * Unique name. |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | protected $uniqueName; |
| 63 | |
| 64 | /** |
| 65 | * Gd constructor. |
| 66 | */ |
| 67 | public function __construct() |
| 68 | { |
| 69 | parent::__construct(); |
| 70 | $this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get image resource. |
| 75 | * |
| 76 | * @return resource |
| 77 | */ |
| 78 | public function getImageResource() |
| 79 | { |
| 80 | return $this->imageResource; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set image resource. |
| 85 | * |
| 86 | * @param resource $value |
| 87 | * |
| 88 | * @return $this |
| 89 | */ |
| 90 | public function setImageResource($value = null) |
| 91 | { |
| 92 | $this->imageResource = $value; |
| 93 | |
| 94 | if (null !== $this->imageResource) { |
| 95 | // Get width/height |
| 96 | $this->width = imagesx($this->imageResource); |
| 97 | $this->height = imagesy($this->imageResource); |
| 98 | } |
| 99 | |
| 100 | return $this; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get rendering function. |
| 105 | * |
| 106 | * @return string |
| 107 | */ |
| 108 | public function getRenderingFunction() |
| 109 | { |
| 110 | return $this->renderingFunction; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Set rendering function. |
| 115 | * |
| 116 | * @param string $value |
| 117 | * |
| 118 | * @return $this |
| 119 | */ |
| 120 | public function setRenderingFunction($value = self::RENDERING_DEFAULT) |
| 121 | { |
| 122 | $this->renderingFunction = $value; |
| 123 | |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get mime type. |
| 129 | */ |
| 130 | public function getMimeType(): string |
| 131 | { |
| 132 | return $this->mimeType; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Set mime type. |
| 137 | * |
| 138 | * @param string $value |
| 139 | * |
| 140 | * @return $this |
| 141 | */ |
| 142 | public function setMimeType($value = self::MIMETYPE_DEFAULT) |
| 143 | { |
| 144 | $this->mimeType = $value; |
| 145 | |
| 146 | return $this; |
| 147 | } |
| 148 | |
| 149 | public function getContents(): string |
| 150 | { |
| 151 | ob_start(); |
| 152 | if (self::MIMETYPE_DEFAULT === $this->getMimeType()) { |
| 153 | imagealphablending($this->getImageResource(), false); |
| 154 | imagesavealpha($this->getImageResource(), true); |
| 155 | } |
| 156 | call_user_func($this->getRenderingFunction(), $this->getImageResource()); |
| 157 | $imageContents = ob_get_contents(); |
| 158 | ob_end_clean(); |
| 159 | |
| 160 | return $imageContents; |
| 161 | } |
| 162 | |
| 163 | public function getExtension(): string |
| 164 | { |
| 165 | $extension = strtolower($this->getMimeType()); |
| 166 | $extension = explode('/', $extension); |
| 167 | $extension = $extension[1]; |
| 168 | |
| 169 | return $extension; |
| 170 | } |
| 171 | |
| 172 | public function getIndexedFilename(): string |
| 173 | { |
| 174 | return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @var string |
| 179 | */ |
| 180 | protected $path; |
| 181 | |
| 182 | /** |
| 183 | * Get Path. |
| 184 | */ |
| 185 | public function getPath(): string |
| 186 | { |
| 187 | return $this->path; |
| 188 | } |
| 189 | |
| 190 | public function setPath(string $path): self |
| 191 | { |
| 192 | $this->path = $path; |
| 193 | |
| 194 | return $this; |
| 195 | } |
| 196 | } |