Skip to content

[LazyImage] Be friendlier with the path passed to data_uri_thumbnail() #342

Closed
@weaverryan

Description

@weaverryan

Hi!

The main usage of lazy image looks like this

<img
   src="{{ data_uri_thumbnail('build/images/large.jpg', 40, 30) }}"
   {{ stimulus_controller('symfony/ux-lazy-image/lazy-image', {
       src: asset('build/images/large.jpg')
   }) }}
>

The path passed to data_uri_thumbnail() (build/images/large.jpg) is loaded simply via file_get_contents('build/images/large.jpg'). That seems to work for me... though not in my tests... and it feels "shaky" at best to pass in a non-absolute path.

Possible solution: if the path to the file cannot be found, we try again using the "public" path. Basically, something like this:

public function createDataUriThumbnail(string $filename, ...): string
{
    if (!file_exists($filename)) {
        $path = $this->projectDir.'/public/'.$filename;
        if (file_exists($path)) {
            $filename = $path;
        }
    }

    return $this->blurHash->createDataUriThumbnail($filename, $width, $height, $encodingWidth, $encodingHeight);
}

It's a bit ugly, bit it would be much more useful. An alternative would be some sort of general-purpose Twig filter for this:

src="{{ data_uri_thumbnail('build/images/large.jpg'|asset_file_path, 40, 30) }}"

But, I'm not sure I love this, and that seems like something that belongs in Symfony itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions