| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Depicter\Document\Models\Elements; |
| 3 | 3 | |
| 4 | 4 | use Averta\Core\Utility\Data; |
| 5 | +use Averta\Core\Utility\Media; | |
| 5 | 6 | use Averta\WordPress\Utility\JSON; |
| 6 | 7 | use Depicter; |
| 7 | 8 | use Depicter\Document\CSS\Breakpoints; |
| 8 | 9 | use Depicter\Document\Models; |
| @@ -12,10 +13,21 @@ | ||
| 12 | 13 | use Depicter\Services\MediaBridge; |
| 13 | 14 | |
| 14 | 15 | class Image extends Models\Element |
| 15 | 16 | { |
| 17 | + use Models\Traits\MediaSourceTrait; | |
| 16 | 18 | |
| 17 | 19 | /** |
| 20 | + * SVG Mime type | |
| 21 | + */ | |
| 22 | + const SVG_MIME = 'image/svg+xml'; | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * SVG Mime type | |
| 26 | + */ | |
| 27 | + const GIF_MIME = 'image/gif'; | |
| 28 | + | |
| 29 | + /** | |
| 18 | 30 | * @var array |
| 19 | 31 | */ |
| 20 | 32 | protected $renderArgs = []; |
| 21 | 33 | |
| @@ -130,9 +142,9 @@ | ||
| 130 | 142 | continue; |
| 131 | 143 | } |
| 132 | 144 | $originalMediaWidth = $attachment[1] ?: $mediaWidth; |
| 133 | 145 | $originalMediaHeight = $attachment[2] ?: $mediaHeight; |
| 134 | - list( $mediaWidth, $mediaHeight ) = $this->fitToSourceMedia( 'contain', $mediaWidth, $mediaHeight, $originalMediaWidth, $originalMediaHeight ); | |
| 146 | + [ $mediaWidth, $mediaHeight ] = Media::fitInBox( 'contain', $mediaWidth, $mediaHeight, $originalMediaWidth, $originalMediaHeight ); | |
| 135 | 147 | } |
| 136 | 148 | |
| 137 | 149 | $args[ $dataAttrName ] = [ |
| 138 | 150 | 'mediaSize'=> [ |
| @@ -166,9 +178,9 @@ | ||
| 166 | 178 | * @return mixed |
| 167 | 179 | * @throws \Exception |
| 168 | 180 | */ |
| 169 | 181 | protected function renderImageTag(){ |
| 170 | - list( $resizeWidth, $resizeHeight, $cropWidth, $cropHeight, $args ) = $this->getImageEditOptions( 'default' ); | |
| 182 | + [ $resizeWidth, $resizeHeight, $cropWidth, $cropHeight, $args ] = $this->getImageEditOptions( 'default' ); | |
| 171 | 183 | |
| 172 | 184 | $hasRelativeUnitSize = $this->hasRelativeUnitSize( 'default' ); |
| 173 | 185 | |
| 174 | 186 | if ( $this->isPreviewMode ) { |
| @@ -197,8 +209,12 @@ | ||
| 197 | 209 | 'data-depicter-srcset' => $imageSource, |
| 198 | 210 | ]) . $img; |
| 199 | 211 | } |
| 200 | 212 | |
| 213 | + if( $this->renderArgs['isSVG'] ){ | |
| 214 | + $this->addMediaUlrToDictionary( $imageSource, '', '', self::SVG_MIME ); | |
| 215 | + } | |
| 216 | + | |
| 201 | 217 | $this->markup->nest( $img . "\n" ); |
| 202 | 218 | |
| 203 | 219 | if ( false !== $a = $this->getLinkTag() ) { |
| 204 | 220 | $this->markup = $a->nest( "\n". $this->markup . "\n" ); |
| @@ -220,9 +236,9 @@ | ||
| 220 | 236 | |
| 221 | 237 | if ( empty( $this->renderArgs['assetId'] ) ) { |
| 222 | 238 | return ''; |
| 223 | 239 | } |
| 224 | - | |
| 240 | + | |
| 225 | 241 | $this->renderPictureWrapper(); |
| 226 | 242 | |
| 227 | 243 | $this->renderSourceTags(); |
| 228 | 244 | $this->renderImageTag(); |
| @@ -250,9 +266,9 @@ | ||
| 250 | 266 | * |
| 251 | 267 | * @return bool |
| 252 | 268 | */ |
| 253 | 269 | public function isSvg( $assetId ) { |
| 254 | - return $this->getMimeType( $assetId ) == 'image/svg+xml'; | |
| 270 | + return $this->getMimeType( $assetId ) == self::SVG_MIME; | |
| 255 | 271 | } |
| 256 | 272 | |
| 257 | 273 | /** |
| 258 | 274 | * Whether it's gif file or not |
| @@ -261,9 +277,9 @@ | ||
| 261 | 277 | * |
| 262 | 278 | * @return bool |
| 263 | 279 | */ |
| 264 | 280 | public function isGif( $assetId ) { |
| 265 | - return $this->getMimeType( $assetId ) == 'image/gif'; | |
| 281 | + return $this->getMimeType( $assetId ) == self::GIF_MIME; | |
| 266 | 282 | } |
| 267 | 283 | |
| 268 | 284 | /** |
| 269 | 285 | * Retrieves image edit options for a breakpoint |
| @@ -284,9 +300,9 @@ | ||
| 284 | 300 | return; |
| 285 | 301 | } |
| 286 | 302 | $originalMediaWidth = $attachment[1] ?: null; |
| 287 | 303 | $originalMediaHeight = $attachment[2] ?: null; |
| 288 | - list( $mediaWidth, $mediaHeight ) = $this->fitToSourceMedia( 'contain', $this->inheritedOptions['resizeW'], $this->inheritedOptions['resizeH'], $originalMediaWidth, $originalMediaHeight ); | |
| 304 | + [ $mediaWidth, $mediaHeight ] = Media::fitInBox( 'contain', $this->inheritedOptions['resizeW'], $this->inheritedOptions['resizeH'], $originalMediaWidth, $originalMediaHeight ); | |
| 289 | 305 | $params[] = $mediaWidth; |
| 290 | 306 | $params[] = $mediaHeight; |
| 291 | 307 | |
| 292 | 308 | } else { |
| @@ -326,9 +342,9 @@ | ||
| 326 | 342 | * @return array |
| 327 | 343 | */ |
| 328 | 344 | protected function getSourceUrls( $device = 'default' ){ |
| 329 | 345 | |
| 330 | - list( $resizeWidth, $resizeHeight, $cropWidth, $cropHeight, $args ) = $this->getImageEditOptions( $device ); | |
| 346 | + [ $resizeWidth, $resizeHeight, $cropWidth, $cropHeight, $args ] = $this->getImageEditOptions( $device ); | |
| 331 | 347 | |
| 332 | 348 | if( ! $cropWidth && ! $cropHeight && ! $resizeWidth && ! $resizeHeight ){ |
| 333 | 349 | return []; |
| 334 | 350 | } |
| @@ -377,15 +393,18 @@ | ||
| 377 | 393 | protected function appendSourceTag( $imageSources = [], $mediaQueryCondition = 'max-width', $mediaQuerySize = null ){ |
| 378 | 394 | if( ! $imageSources ){ |
| 379 | 395 | return; |
| 380 | 396 | } |
| 397 | + | |
| 398 | + $this->addMediaUlrToDictionary( $imageSources, $mediaQueryCondition, $mediaQuerySize ); | |
| 399 | + | |
| 381 | 400 | $attributes = [ |
| 382 | - 'data-depicter-srcset' => trim( implode( ', ', $imageSources ), ', ' ) | |
| 401 | + 'data-depicter-srcset' => trim( implode( ', ', $imageSources ), ', ' ), | |
| 402 | + 'srcset' => \Depicter::media()::IMAGE_PLACEHOLDER_SRC, | |
| 383 | 403 | ]; |
| 384 | 404 | if( $mediaQueryCondition && $mediaQuerySize ){ |
| 385 | 405 | $attributes['media'] = '(' . $mediaQueryCondition . ': ' . $mediaQuerySize . 'px)'; |
| 386 | 406 | } |
| 387 | - | |
| 388 | 407 | $sourceTag = Html::source( $attributes ); |
| 389 | 408 | |
| 390 | 409 | $this->markup->nest( "\n" . $sourceTag . "\n" ); |
| 391 | 410 | } |
| @@ -430,31 +449,7 @@ | ||
| 430 | 449 | } |
| 431 | 450 | $this->appendSourceTag( $desktopSources, 'min-width', $mediaQuerySize ); |
| 432 | 451 | } |
| 433 | 452 | |
| 434 | - } | |
| 435 | - | |
| 436 | - /** | |
| 437 | - * Fit image to source image with and height | |
| 438 | - * Mostly used in data source images | |
| 439 | - * | |
| 440 | - * @param $type | |
| 441 | - * @param $sourceMediaWidth | |
| 442 | - * @param $sourceMediaHeight | |
| 443 | - * @param $width | |
| 444 | - * @param $height | |
| 445 | - * | |
| 446 | - * @return float[]|int[] | |
| 447 | - */ | |
| 448 | - protected function fitToSourceMedia( $type, $sourceMediaWidth, $sourceMediaHeight, $width, $height ) { | |
| 449 | - $withRatio = $sourceMediaWidth / $width; | |
| 450 | - $heightRatio = $sourceMediaHeight / $height; | |
| 451 | - | |
| 452 | - $ratio = $type === 'cover' ? max($withRatio, $heightRatio) : min($withRatio, $heightRatio); | |
| 453 | - | |
| 454 | - return [ | |
| 455 | - $width * $ratio, | |
| 456 | - $height * $ratio, | |
| 457 | - ]; | |
| 458 | 453 | } |
| 459 | 454 | |
| 460 | 455 | } |