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

Image resize for srcset #357

Closed
realcgslav opened this issue Jul 3, 2019 · 3 comments
Closed

Image resize for srcset #357

realcgslav opened this issue Jul 3, 2019 · 3 comments

Comments

@realcgslav
Copy link

realcgslav commented Jul 3, 2019

Sorry if this is silly question. I'm completely new in Laravel world.

Would be possible to resize images in Jigsaw to have ability to use them for img srcset tag?

Something like this one: https://medium.com/codefield-community/responsive-images-and-progressive-image-loading-with-laravel-a67d5d1f42c5 or here: https://docs.spatie.be/laravel-medialibrary/v7/responsive-images/getting-started-with-responsive-images/#generating-the-necessary-images but not on upload but... Before build?

@anthonyterrell
Copy link

Welcome to the Laravel world, @realcgslav 👋

This is not a silly question. Yes, this is possible with Jigsaw using its built-in Event Listeners. Within the bootstrap.php file you can add:

// bootstrap.php
$events->beforeBuild(function ($jigsaw) {
   // your code here that resizes images
});

ImageMagick is a popular Open Source tool with decades of great documentation and examples out there. Once you determine your file sizes and their location, you will be able to use them with the srcset attribute.

Here is a little more information about the srcset attribute

@sytheveenje
Copy link

sytheveenje commented Jan 9, 2020

Hi @anthonyterrell,

I'm also wondering how to do this. I created a function that loops trough my collection of posts (which are .md files) and I want to replace all images with responsive images.

I want to replace <img src="assets/images/jigsaw.png"> with <img src="/images/tiny/jigsaw.png" srcset="images/medium/jigsaw.png">

I wrote this:

$events->afterCollections(function (Jigsaw $jigsaw) {
    
    $posts = $jigsaw->getCollection('posts');

    foreach($posts as $post) {

        $dom = new DOMDocument();
        $dom->loadHTML($post->getContent());

        foreach ($dom->getElementsByTagName('img') as $img) {

            $src = $img->getAttribute('src');

            $tiny_src = str_replace('/assets/images/', '/images/tiny/', $src);

            $medium_src = str_replace('/assets/images/', '/images/medium/', $src);

            $img->setAttribute('src', $tiny_src);
            $img->setAttribute('srcset', $medium_src);
        }

        dd($dom); // Outputs the html I want
    }

});

But how can I publish my modifications to the build?

@GenieTim
Copy link

GenieTim commented Mar 3, 2020

Publishing modifications can be done using the method ->setContent($domstring), i.e.:

$events->afterCollections(function (Jigsaw $jigsaw) {
    
    $posts = $jigsaw->getCollection('posts');

    foreach($posts as $post) {

        $dom = new DOMDocument();
        $dom->loadHTML($post->getContent());

        foreach ($dom->getElementsByTagName('img') as $img) {

            $src = $img->getAttribute('src');

            $tiny_src = str_replace('/assets/images/', '/images/tiny/', $src);

            $medium_src = str_replace('/assets/images/', '/images/medium/', $src);

            $img->setAttribute('src', $tiny_src);
            $img->setAttribute('srcset', $medium_src);
        }
        $post->setContent($dom->saveHTML());
    }

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants