Jump to content
tpr

MarkupSrcSet

Recommended Posts

Could you show your code and module settings?

Share this post


Link to post
Share on other sites

It works perfectly well:

<div <?php echo $page->images->first()->bgset('hero'); ?>>Lorem ipsum</div>

Call to a member function srcset() on a non-object

<img <?php echo $page->featured_image->srcset('featured-image'); ?>>

What should I do?

Share this post


Link to post
Share on other sites
$page->featured_image

doesn't return a valid object! (It returns a "non-object")
You need to check that it returns a valid image object. Check if you set the image field to return a single image or an array of images.
Also you always need a fallback for cases where no image is uploaded to a field!

  • Like 1

Share this post


Link to post
Share on other sites

Thank you! It works like this:
 

<img <?php echo $page->images->first()->srcset('featured-image'); ?>>

 

  • Like 1

Share this post


Link to post
Share on other sites

Just updated the module to version 0.1. Almost everything in it has changed so reading the docs is a must :)

The module does less "magic" as before, eg. doesn't add the "lazyload" class and data-sizes automatically. These should be added manually or you can write a wrapper function if you would like to simplify things (please notify me if you think you wrote one that should be part of the module).

Also all the module settings are gone except the "Load scripts" checkbox because all other settings became useless after the rewrite.

The new syntax looks like this (here with a helper to add a noscript tag for no-js fallback):

<?php $img = $page->featured_image; ?>

<img data-srcset="<?php echo $img->srcset('200x300,*2,*3'); ?>" class="lazyload" data-sizes="auto" alt="" />

<noscript>
    <img src="<?php echo $img->srcsetUrls[0]; ?>" />
</noscript>

The main change is that you can easily tell what image sizes (sets) you need ("200x300,*2,*3" in the above example). Of course you can use WxH values for each set or use divisors too. What's more you don't have to start with the lowest image size, eg. "1200x900, /2, /4" is perfectly fine. 

  • Like 4

Share this post


Link to post
Share on other sites

Thanks for the update, Something to possible consider -  maybe a field in the module settings where the user can type in the url segment to identify that the page is an AMP page and from there replace the <img> tage with the <amp img ></amp> tag?

Share this post


Link to post
Share on other sites

The module doesn't output the img tag, only the value for the srcset attribute so I don't see how this feature could be implemented.

Share this post


Link to post
Share on other sites

Maybe that my question is a bit OT. As the new modules version works perfect! ^-^

My question is related to data-bgset. I want to show an image covering the whole vieport. Therefor I use a div with background image. The css for the div is:

        position: relative !important;
        display: block;
        height: 100vh;
        background-position: 50% 50%;
        background-repeat: no-repeat;
        background-size: cover;

My list is defined like: 600x0, 1200x0, 1800x0

Related to the background-size: cover, I believe, I get always the image displayed what is one step to small from the list, because they get stretched by css, to cover the viewport.

Can someone tell me how to solve this with MarkupSrcSet?

Share this post


Link to post
Share on other sites

Ok, I may add a workaround to my own question:

I have found out, that, for what ever reason, the largest variation never was fetched from the server !! ??

With my example form above, the largest variation fetched was the 1200x0. Then, with adding a 1801x0 to the list, the 1800x0 was fetched as largest. So, not sure why this behave is.

Has anyone else seen this for bg-sets?
Besides the workaround, it isn't useful to get the HDs filled up with (regularly) unnessecary large variations.

Share this post


Link to post
Share on other sites

I have previously been using uppercase URL in $page->image->URL for browser cache busting query string purpose. Is this compatible with MarkupSrcSet? My current image url is $page->image->srcset('860x0,/2,/3'). 

  • Like 1

Share this post


Link to post
Share on other sites

I think it doesn't support it yet. I'll check how to add this, thanks for pointing this out.

  • Like 2

Share this post


Link to post
Share on other sites

@franctic-aerobic It was easy to add a new $image->SRCSET method which generates such srcset:

/site/assets/files/1/pic.400x300.png?nc=1545348144 400w,/site/assets/files/1/pic.600x400.png?nc=1545348181 600w

But there's a problem: these resized images won't get re-created if you modify the original image, which I suppose your original plan was. I don't know how to deal with this without overcomplicating things.

Share this post


Link to post
Share on other sites

I've been using LazySizes manually for a while and wanted to try your Module.

Can I check that the following is setup correctly? I'm applyng this is a background image which is new to me.

First of all, I have been setting a variable of photo_header which uses a named, cropped image of 1250x550

<?php $photo_header = $page->image_staff_header->getCrop('crop-staff-photo'); ?>

My actual html which outputs this image looks like this (pre MarkupSrcSet)

<div class="uk-width-uk-width-1-1 staff-pic" style="background-image:url(<?= $photo_header->url?>);">

When I apply my MarkupSrc set, I think this is the proper way

<div 
data-bgset="<?php echo $photo_header->srcset('1200x550,/1.5,/2,/3'); ?>" 
data-sizes="auto"
class="lazyload uk-width-uk-width-1-1 staff-pic" 
style="background-image:url(<?php echo $photo_header->srcsetUrls[0]; ?>);"
>

and it does produce the following...

<div 
data-bgset="/site/assets/files/1089/adams_-_cubes-1.-crop-staff-photo.1200x550.jpg 1200w,
            /site/assets/files/1089/adams_-_cubes-1.-crop-staff-photo.800x366.jpg 800w,
            /site/assets/files/1089/adams_-_cubes-1.-crop-staff-photo.600x275.jpg 600w,
            /site/assets/files/1089/adams_-_cubes-1.-crop-staff-photo.400x183.jpg 400w" 
class="uk-width-uk-width-1-1 staff-pic uk-first-column" 
style="background-image:url(/site/assets/files/1089/adams_-_cubes-1.-crop-staff-photo.1200x550.jpg);"
>

However, I notice that when I inspect the Div above in Chrome, I always seem to have the same image regardless of device width

element.style {
    background-image: url(/site/assets/files/1089/adams_-_cubes-1.-crop-staff-photo.1200x550.jpg);
}

Is this right? Surely I should be seeing different image sizes above?

Cheers @tpr
 

Share this post


Link to post
Share on other sites

If you view the page source, is the largest img variation set for the background image? The 0-index should be the smallest.

Btw if you start devtools in desktop size, then the smaller img variations will not be loaded. Start in small width and check if it loads the larger ones when you resize the viewport.

Edit: there is no lazyload or lazyloaded class in your final src, perhaps some hook is messing with the ing classes?

Share this post


Link to post
Share on other sites

Yep - I think it's all good.

<div data-sizes="auto" 
     data-bgset="/site/assets/files/1089/adams-cubes.-crop-staff-photo.1200x550.jpg 1200w,
                 /site/assets/files/1089/adams-cubes.-crop-staff-photo.800x366.jpg 800w,
                 /site/assets/files/1089/adams-cubes.-crop-staff-photo.600x275.jpg 600w,
                 /site/assets/files/1089/adams-cubes.-crop-staff-photo.400x183.jpg 400w" 
     class="uk-width-uk-width-1-1 staff-pic uk-first-column lazyloaded" 
     style="background-image:url('/site/assets/files/1089/adams-cubes.-crop-staff-photo.1200x550.jpg') ;">

Have cleared the cache, refreshed from small size and expanded that way.

Share this post


Link to post
Share on other sites

Perhaps the 0-index fails to get the smallest img (I do not remember wether if it should or not). Could you try with $photo_header->srcsetUrls['smallest'], or with no initial style attribute at all?

  • Like 1

Share this post


Link to post
Share on other sites
12 minutes ago, tpr said:

Perhaps the 0-index fails to get the smallest img (I do not remember wether if it should or not). Could you try with $photo_header->srcsetUrls['smallest'], or with no initial style attribute at all?

That's it. Removing the [0] at the end worked. Many thanks 🙂

Share this post


Link to post
Share on other sites

You mean replacing with "smallest"? Otherwise it should throw an error.

Share this post


Link to post
Share on other sites

Correct. I am using

echo $photo_header->srcsetUrls['smallest']; 

If I use

echo $photo_header->srcsetUrls; 

there is an error but it only shows up in the network tab. The images seem to still work but they could be cached versions.

  • Like 1

Share this post


Link to post
Share on other sites

Hi again @tpr

I notice on the code below I am getting both a data-srcset and a srcset too. Is this correct?

My input

<?php $myimage = $page->Images->first(); ?>
<img 
     class="lazyload"
     data-src="<?php echo $myimage->url ?>"
     data-srcset="<?php echo $myimage->srcset('800x0, /2, /3') ?>"
     alt="" 
 />

My output in browser

 <img class=" lazyloaded" 
 data-src="/site/assets/files/1/photo-steps-video.jpg" 
 data-srcset="
              /site/assets/files/1/photo-steps-video.800x0.jpg 800w,
              /site/assets/files/1/photo-steps-video.400x0.jpg 400w,
              /site/assets/files/1/photo-steps-video.266x0.jpg 266w" 
alt="" 
srcset="/site/assets/files/1/photo-steps-video.800x0.jpg 800w,
         /site/assets/files/1/photo-steps-video.400x0.jpg 400w,
         /site/assets/files/1/photo-steps-video.266x0.jpg 266w" 
src="/site/assets/files/1/photo-steps-video.jpg"
 >  

 

Share this post


Link to post
Share on other sites
1 hour ago, Peter Knight said:

I notice on the code below I am getting both a data-srcset and a srcset too. Is this correct? 

Guess this is the result from the lazyload which simply copies the data-srcset into srcset. Did you check the page source (as received from server) or DOM tree in inspector?

  • Like 1

Share this post


Link to post
Share on other sites

I think @Autofahrn is right, the module should output anything only on $img->srcset().

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By gebeer
      I am happy to present my new fieldtype FieldtypeImageFromPage. It is made up of 2 modules:
      Fieldtype Image Reference From Another Page is a Fieldtype that stores a reference to a single image from another page. The image can be selected with the associated Inputfield.
      Inputfield Select Image From Page is an Inputfield to select a single image from images on a predefined page and it's children.
      And there also is a helper module that takes care of cleanup tasks.
      This module evolved out of a discussion about my other Module FieldtypeImagePicker.  It caters for use cases where a set of images is being reused multiple times across a site. With this fieldtype these images can be administered through a chosen page. All images uploaded to that page will be available in the inputfield.
      When to use ?
      Let editors choose an image from a set of images that is being used site-wide. Ideal for images that are being re-used across the site.
      Suited for images that are used on multiple pages throughout the site (e.g. icons).
      Other than the native ProcessWire images field, the images here are not stored per page. Only references to images on another page are stored. This has several advantages:
      one central place to organize images when images change, you only have to update them in one place. All references will be updated, too. (Provided the name of the image that has changed stays the same) Features
      Images can be manipulated like native ProcessWire images (resizing, cropping etc.) Image names are fully searchable through the API Accidental image deletion is prevented. When you want to delete an image from one of the pages that hold your site-wide images, the module searches all pages that use that image. If any page contains a reference to the image you are trying to delete, deletion will be prevented. You will get an error message to help you edit those pages and remove references there before you can finally delete the image. How to install and setup
      Download and install this module like any other modules in ProcessWire Create a page in the page tree that will hold your images. This page's template must have an images field Upload some images to the page you created in step 2 Create a new field. As type choose 'Image Reference From Another Page'. Save the field. In 'Details' Tab of the field choose the page you created in step 2 Click Save button Choose the images field name for the field that holds your images (on page template from step 2) Click Save button again Choose whether you want to include child pages of page from step 2 to supply images Add the field to any template You are now ready to use the field View of the inputfield on the page edit screen:

      View of the field settings

      The module can be installed from this github repo. Some more info in the README there, too.
      In my tests it was fairly stable. After receiving your valued feedback, I will eventually add it to the modules directory.
      My ideas for further improvement:
      - add ajax loading of thumbnails
      Happy to hear your feedback!
       
    • By gebeer
      Although the PW backend is really intuitive, ever so often my clients need some assistance. Be it they are not so tech savvy or they are not working in the backend often.
      For those cases it is nice to make some help videos available to editors. This is what this module does.
      ProcessHelpVideos Module
      A Process module to display help videos for the ProcessWire CMS. It can be used to make help videos (screencasts) available to content editors.
      This module adds a 'Help Videos" section to the ProcessWire backend. The help videos are accessible through an automatically created page in the Admin page tree. You can add your help videos as pages in the page tree. The module adds a hidden page to the page tree that acts as parent page for the help video pages. All necessary fields and templates will be installed automatically. If there are already a CKEditor field and/or a file field for mp4 files installed in the system, the module will use those. Otherwise it will create the necessary fields. Also the necessary templates for the parent help videos page and it's children are created on module install. The module installs a permission process-helpvideos. Every user role that should have access to the help video section, needs this permission. I use the help video approach on quite a few production sites. It is stable so far and well received by site owners/editors. Up until now I installed required fields, templates and pages manually and then added the module. Now I added all this logic to the install method of the module and it should be ready to share.
      The module and further description on how to use it is available on github: https://github.com/gebeer/ProcessHelpVideos
      If you like to give it a try, I am happy to receive your comments/suggestions here.
    • By Robin S
      A module created in response to the topic here:
      Page List Select Multiple Quickly
      Modifies PageListSelectMultiple to allow you to select multiple pages without the tree closing every time you select a page.
      The screencast says it all:

       
      https://github.com/Toutouwai/PageListSelectMultipleQuickly
      https://modules.processwire.com/modules/page-list-select-multiple-quickly/
    • By gebeer
      Hello all,
      sharing my new module FieldtypeImagePicker. It provides a configurable input field for choosing any type of image from a predefined folder.
      The need for it came up because a client had a custom SVG icon set and I wanted the editors to be able to choose an icon in the page editor.
      It can also be used to offer a choice of images that are used site-wide without having to upload them to individual pages.
      There are no image manipulation methods like with the native PW image field.
      Module and full description can be found on github https://github.com/gebeer/FieldtypeImagePicker
      Kudos to @Martijn Geerts. I used his module FieldTypeSelectFile as a base to build upon.
      Here's how the input field looks like in the page editor:

      Hope it can be of use to someone.
      If you like to give it a try, I'm happy to hear your comments or suggestions for improvement. Eventually this will go in the module directory soon, too.
    • By bernhard
      @Sergio asked about the pdf creation process in the showcase thread about my 360° feedback/survey tool and so I went ahead and set my little pdf helper module to public.
      Description from PW Weekly:
       
      Modules Directory: https://modules.processwire.com/modules/rock-pdf/
      Download & Docs: https://github.com/BernhardBaumrock/RockPDF
       
      You can combine it easily with RockReplacer: 
      See also a little showcase of the RockPdf module in this thread:
       
×
×
  • Create New...