0

How do I set scandir to list the module web/images directory? getViewFileUrl generates the file path but what I need is the directory path, so scandir can read the folder contents.

Vendor > Module > view > frontend > web

template file:

<?php
    $images = scandir("images/base/thumbnails");
    $ignore = Array('.', '..');
    foreach($images as $curimg){
        if(!in_array($curimg, $ignore)) {
            echo '<a href="'.$this->getViewFileUrl("Vendor_Module::images/base/full/$curimg").'"><img src="'.$this->getViewFileUrl("Vendor_Module::images/base/thumbnails/$curimg").'"></a>';
        }
    };
?>
1
  • Not getting your question, what you need to do ? you need image url for display in front ?
    – Yogesh
    Nov 11, 2017 at 12:13

2 Answers 2

0

Add your image file to this path

app/code/Vendor/Module/view/frontend/web/images/image.png

Now call this image in your phtml file like this

<img src="<?php echo $this->getViewFileUrl('Vendor_Module::images/image.png'); ?>" />

Reference

1
  • You are reffering to image.png but i'd like to know how all images from the thumbnails directory can be listed.
    – Bas
    Nov 12, 2017 at 9:08
0

You can define function in your block file :-

<?php
namespace Namespace\Module\Block;

class test extends \Magento\Framework\View\Element\Template { 

    public function __construct(

       \Magento\Framework\View\Asset\Repository $assetRepo,
    ){
        $this->assetRepo = $assetRepo;
    }

    public function testImageUrl($path){
       $url =  $this->assetRepo->getUrl($path);;
       return $url;
    }
} 

Now all in phtml file :-

<img src="<?php echo $block->testImageUrl('Vendor_Module::images/image.png'); ?>
4
  • This calls image.png but does not list all images from the directory
    – Bas
    Nov 12, 2017 at 12:02
  • Can you able to clarify your question so i can able to get more idea ?
    – Yogesh
    Nov 13, 2017 at 5:11
  • Updated my question. Hope it is clear now
    – Bas
    Nov 14, 2017 at 16:05
  • clarified the question.
    – Bas
    Nov 21, 2017 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.