Trying to get image src only from Page List thumbnail

Permalink
Hi

I am trying to get the image src only from the thumbnail image from a page list so I can include it as a background image. I have looked all over this forum, stack overflow and getting nowhere. I am new to PHP, so I apologise if this is a really simple request.

I currently have this which is not working

<?php if(is_object($thumbnail)): 
       $img = Core::make('html/image', array($thumbnail));
       $tag = $img->getTag();
 ?>
     <a class="<?php echo $entryClasses?>" href="<?php echo $url; ?>" title="<?php echo $title; ?>" style="background-image: url('<?php echo $img_src ?>')">
<?php endif; ?>


Any ideas what I need to do?

moonshark
 
linuxoid replied on at Permalink Reply
linuxoid
This is from the page list block:
foreach ($pages as $page):
    $thumbnail = $page->getAttribute('thumbnail');
    if (is_object($thumbnail)):
        $thumb = Core::make('html/image', array($thumbnail));
        echo '<a href="' . $url . '" target="' . $target . '">';
        $tag = $thumb->getTag();
        $tag->addClass('img-responsive');
        print $tag;
    endif;
...

The page should have a thumbnail attribute first.
mnakalay replied on at Permalink Reply
mnakalay
It is not as simple as one might think.

Using that getTag() function does some work in the background.

First it gets your theme's settings to decide whether the image should be sent back as is or resized. If you theme defines image sizes, you will get a PICTURE tag back. Otherwise a simle IMG tag.

Then it checks whether it should be using thumbnail types (faster and with proper sizing) or legacy thumbnailer (slower and no resizing).

So in the end that tag can be a fully responsive PICTURE tag with several source sets using proerly resized images depending on screen size and one image src for fallback.

In that case, you can't really get the source from the tag and your best move is to just use the tag.

Now if your theme didn't define image sizes, you will get a simple img tag with one source and that will be the path or URL to the full-sized image (so not resized in any way which is a problem if your image is huge)

If you really just want the image src you can but you won't get the benefit of all the work C5 does in the background

you can do either
$thumbnail->getRelativePath();

or
$thumbnail->getURL();