Search code examples
phpimageattributesthumbnailsconcrete5

Loading Thumbnail of a page Image Attribute in Concrete5


I like to load a image attribute and assign a custom thumbnail named small (created in Dashboard > System > Files> Thumbnails) to the image.

I created a custom image attribute blogimage in a template. Loading the image attribute works. Just need to know how to load the custom thumbnail.

<?php
    $img = $c->getAttribute('blogimage'); ?>
    <?php if ($img): ?>
    <img src="<?php  echo ($img->getVersion()->getRelativePath()); ?>"/>
<?php endif; ?

Solution

  • If blogimage is the handle of a Image/File attribute, and $c is a Page instance, the following code

    $img = $c->getAttribute('blogimage');
    

    returns null if the page doesn't have a value for that attribute, or a Concrete\Core\Entity\File\File instance otherwise.

    Then

    $imgVersion = $img->getVersion();
    

    returns a Concrete\Core\Entity\File\Version instance, which has the getThumbnailURL method.

    So, in order to have the URL of the thumbnail type with handle small, you simply have to write this:

    $img = $c->getAttribute('blogimage');
    if ($img !== null) {
        $imgVersion = $img->getVersion();
        $thumbnailURL = $imgVersion->getThumbnailURL('small');
        ?><img src="<?= $thumbnailURL ?>" /><?php
    }