Skip to content

Commit

Permalink
add ability to get Image Tag with FrontendPath (pimcore#15913)
Browse files Browse the repository at this point in the history
* add ability to get Image Tag with FrontendPath

* add ability to get Image Tag with FrontendPath
  • Loading branch information
ITspirit committed Jan 31, 2024
1 parent 6ff63fc commit 483f95e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Pimcore offers the method `getHTML(array $options)` to get a ready to use `<pict
You can configure the generated markup with the following options:

| Name | Type | Description |
| ------------------------------ | -------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|--------------------------------| -------- |------------------------------------------------------------------------------------------------------------------------------------|
| `disableWidthHeightAttributes` | bool | Width & height attributes are set automatically by Pimcore, to avoid this set this option (eg. to true => isset check) |
| `disableAutoTitle` | bool | Set to true, to disable the automatically generated title attribute (containing title and copyright from the origin image) |
| `disableAutoAlt` | bool | Set to true, to disable the automatically generated alt attribute |
Expand All @@ -94,6 +94,7 @@ You can configure the generated markup with the following options:
| `imgCallback` | callable | A callable to modify the attributes for the generated `<img>` tag. There 1 argument passed, the array of attributes. |
| `disableImgTag` | bool | Set to `true` to not include the `<img>` fallback tag in the generated `<picture>` tag. |
| `useDataSrc` | bool | Set to `true` to use `data-src(set)` attributes instead of `src(set)`. |
| `useFrontendPath` | bool | Set to `true` to use the full url (including the frontend_prefix). |

## Usage Examples

Expand Down
60 changes: 33 additions & 27 deletions models/Asset/Image/Thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Pimcore\Model\Asset\Image;

use Exception;
use Pimcore\Event\AssetEvents;
use Pimcore\Event\FrontendEvents;
use Pimcore\Logger;
Expand Down Expand Up @@ -52,15 +53,17 @@ public function getPath(array $args = []): string
$frontend = $args['frontend'] ?? \Pimcore\Tool::isFrontend();

$pathReference = null;
if ($this->getConfig()) {
if ($this->useOriginalFile($this->asset->getFilename()) && $this->getConfig()->isSvgTargetFormatPossible()) {
// we still generate the raster image, to get the final size of the thumbnail
// we use getRealFullPath() here, to avoid double encoding (getFullPath() returns already encoded path)
$pathReference = [
'src' => $this->asset->getRealFullPath(),
'type' => 'asset',
];
}
if (
$this->getConfig() &&
$this->useOriginalFile($this->asset->getFilename()) &&
$this->getConfig()->isSvgTargetFormatPossible()
) {
// we still generate the raster image, to get the final size of the thumbnail
// we use getRealFullPath() here, to avoid double encoding (getFullPath() returns already encoded path)
$pathReference = [
'src' => $this->asset->getRealFullPath(),
'type' => 'asset',
];
}

if (!$pathReference) {
Expand Down Expand Up @@ -96,13 +99,7 @@ protected function hasListeners(string $eventName): bool

protected function useOriginalFile(string $filename): bool
{
if ($this->getConfig()) {
if (!$this->getConfig()->isRasterizeSVG() && preg_match("@\.svgz?$@", $filename)) {
return true;
}
}

return false;
return $this->getConfig() && preg_match("@\.svgz?$@", $filename) && !$this->getConfig()->isRasterizeSVG();
}

/**
Expand All @@ -124,7 +121,7 @@ public function generate(bool $deferredAllowed = true): void
try {
$deferred = $deferredAllowed && $this->deferred;
$this->pathReference = Thumbnail\Processor::process($this->asset, $this->config, null, $deferred, $generated);
} catch (\Exception $e) {
} catch (Exception $e) {
Logger::error("Couldn't create thumbnail of image " . $this->asset->getRealFullPath() . ': ' . $e);
}
}
Expand Down Expand Up @@ -221,7 +218,7 @@ public function getHtml(array $options = []): string
$options['previewDataUri'] = $image->getLowQualityPreviewDataUri() ?: $emptyGif;
}

$isAutoFormat = $thumbConfig instanceof Config ? strtolower($thumbConfig->getFormat()) === 'source' : false;
$isAutoFormat = $thumbConfig instanceof Config && strtolower($thumbConfig->getFormat()) === 'source';

if ($isAutoFormat) {
// ensure the default image is not WebP
Expand Down Expand Up @@ -260,7 +257,7 @@ protected function getMediaConfigHtml(Config $thumbConfig, Image $image, array $

// currently only max-width is supported, the key of the media is WIDTHw (eg. 400w) according to the srcset specification
ksort($mediaConfigs, SORT_NUMERIC);
array_push($mediaConfigs, $thumbConfig->getItems()); //add the default config at the end - picturePolyfill v4
$mediaConfigs[] = $thumbConfig->getItems(); //add the default config at the end - picturePolyfill v4

foreach ($mediaConfigs as $mediaQuery => $config) {
$thumbConfig->setItems($config);
Expand Down Expand Up @@ -292,7 +289,7 @@ public function getImageTag(array $options = [], array $removeAttributes = []):
if (isset($options['previewDataUri'])) {
$attributes['src'] = $options['previewDataUri'];
} else {
$path = $this->getPath();
$path = ($options['useFrontendPath'] ?? false) ? $this->getFrontendPath() : $this->getPath();
$attributes['src'] = $this->addCacheBuster($path, $options, $image);
}

Expand Down Expand Up @@ -326,7 +323,10 @@ public function getImageTag(array $options = [], array $removeAttributes = []):
}

// get copyright from asset
if ($image->getMetadata('copyright') && (!isset($options['disableAutoCopyright']) || !$options['disableAutoCopyright'])) {
if (
(!isset($options['disableAutoCopyright']) || !$options['disableAutoCopyright']) &&
$image->getMetadata('copyright')
) {
if (!empty($altText)) {
$altText .= ' | ';
}
Expand Down Expand Up @@ -372,11 +372,15 @@ public function getImageTag(array $options = [], array $removeAttributes = []):
/**
*
*
* @throws \Exception
* @throws Exception
*/
public function getMedia(string $name, int $highRes = 1): ?ThumbnailInterface
{
$thumbConfig = $this->getConfig();
if ($thumbConfig === null) {
return null;
}

$mediaConfigs = $thumbConfig->getMedias();

if (isset($mediaConfigs[$name])) {
Expand All @@ -386,12 +390,11 @@ public function getMedia(string $name, int $highRes = 1): ?ThumbnailInterface
$thumbConfigRes->setMedias([]);
/** @var Image $asset */
$asset = $this->getAsset();
$thumb = $asset->getThumbnail($thumbConfigRes);

return $thumb;
} else {
throw new \Exception("Media query '" . $name . "' doesn't exist in thumbnail configuration: " . $thumbConfig->getName());
return $asset->getThumbnail($thumbConfigRes);
}

throw new Exception("Media query '" . $name . "' doesn't exist in thumbnail configuration: " . $thumbConfig->getName());
}

/**
Expand Down Expand Up @@ -434,7 +437,10 @@ private function getSrcset(Config $thumbConfig, Image $image, array $options, ?s
// encode comma in thumbnail path as srcset is a comma separated list
$srcSetValues[] = str_replace(',', '%2C', $this->addCacheBuster($thumb . ' ' . $descriptor, $options, $image));

if ($this->useOriginalFile($this->asset->getFilename()) && $this->getConfig()->isSvgTargetFormatPossible()) {
if (
$this->useOriginalFile($this->asset->getFilename()) &&
$this->getConfig()?->isSvgTargetFormatPossible()
) {
break;
}
}
Expand Down

0 comments on commit 483f95e

Please sign in to comment.