Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pregeneration of Thumbnail via CLI does not support Webp #5295

Closed
andreas-gruenwald opened this issue Nov 20, 2019 · 5 comments · Fixed by #5391
Assignees

Comments

@andreas-gruenwald
Copy link
Contributor

@andreas-gruenwald andreas-gruenwald commented Nov 20, 2019

Feature Request

Sometimes it is necessary to pregenerate images so that user's do not have to wait when a new version of a Webshop launches.

We are doing this by calling the CLI:

$image->getThumbnail($thumbnailFormat)->getFileSystemPath()

This command calls the generate methode and the Pimcore\Model\Asset\Image\Thumbnail\Processor if the file system path does not exist yet.

Unfortunatelly - if the thumbnail config setting is "optimized", then only a "png" will be generated, no webp which is more commonly used by modern webbrowsers. The reason is that Frontend::hasWebpSupport() is queried, which is always false in CLI scripts.

There should be a way to
1 control the format of the generated thumbnail, e.g. via listener.
2 generate all formats at once.

@markus-moser

This comment has been minimized.

Copy link
Contributor

@markus-moser markus-moser commented Nov 20, 2019

The same would be helpfull for the x2 images. Is it possible to extend the Pimcore Command (pimcore:thumbnails:image) to provide this?

@andreas-gruenwald

This comment has been minimized.

Copy link
Contributor Author

@andreas-gruenwald andreas-gruenwald commented Nov 25, 2019

Also, all formats should be generated (mobile, etc.) @markus-moser maybe you were already talking about that?

@markus-moser

This comment has been minimized.

Copy link
Contributor

@markus-moser markus-moser commented Nov 25, 2019

@andreas-gruenwald no, but you are right :)

@andreas-gruenwald

This comment has been minimized.

Copy link
Contributor Author

@andreas-gruenwald andreas-gruenwald commented Nov 28, 2019

Here is our project-specific solution. Maybe we can include that into ThumbnailsImageCommand and ThumbnailsVideoCommand?

/**
  *  Create thumbnail configs that are relevant for the Pimcore src-set.
  *  @param $thumbnailFormats string[] thumbnail names, such as thumb-1, thumb-2.
  *  @param $allFormats bool if true, common additional thumbnail formats will be generated.
  *  @return Asset\Image\Thumbnail\Config[]
**/
function createRelevantThumbnailConfigs(array $thumbnailFormats, bool $allFormats = true) {
        $thumbnailConfigList = [];
        foreach ($this->getThumbnailFormats() as $thumbnailFormat) {
            $config = Asset\Image\Thumbnail\Config::getByName($thumbnailFormat);

            $medias = array_merge(['default' => 'defaultMedia'], $config->getMedias() ? : []);
            foreach ($medias as $mediaName => $media) {

                $currentConfig = clone $config;
                if ($mediaName != 'default') {
                    $currentConfig->selectMedia($mediaName);
                }
                $thumbnailConfigList[] = $config;

                if ($all) {
                    //high resolution
                    $configX2 = clone $currentConfig;
                    $configX2->setHighResolution(2);
                    $thumbnailConfigList[] = $configX2;

                    if ($config->getFormat() != 'webp') {
                        $webpConfig = clone $currentConfig;
                        $webpConfig->setFormat('webp');
                        $thumbnailConfigList[] = $webpConfig;

                        //Retina image config
                        $webpConfigX2 = clone $currentConfig;
                        $webpConfigX2->setFormat('webp');
                        $webpConfigX2->setHighResolution(2);
                        $thumbnailConfigList[] = $webpConfigX2;
                    }
                }
            }
        }
       return $thumbnailConfigList;
}

//...
//Application-specific example code:

$thumbnailFormatsExample =  [ //thumbnail formats for pre-generation
            'product-result-item',
            'thumb-slider-small',
            'thumb-slider',
        ]
;

$image = Asset::getById(123); //fetch test image

$createdFilePathList = [];
$thumbnailConfigList = $this->createRelevantThumbnailConfigs($thumbnailFormatsExample);
foreach ($thumbnailConfigList as $thumbnailConfig) {
     $path = $image->getThumbnail($thumbnailConfig)->getFileSystemPath(); //create thumbnail if not existing yet.
     $createdFilePathList[] = $path;
 }
dump($createdFilePathList);
@andreas-gruenwald

This comment has been minimized.

Copy link
Contributor Author

@andreas-gruenwald andreas-gruenwald commented Nov 28, 2019

In the above solution different medias were still missing. I added them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.