0

Im trying to display images from backend of my app

<?php foreach ($img as $key=>$row): ?>
<div class="products_inside_wrapper intro_wrapper">
<div class="classes_inside_item bordered_wht_border">
<?php
foreach (explode(';',rtrim($row['images'],';')) as $key_img => $value_img)
{
?>
<?php echo  Html::img('@backend/web'.'/'.$value_img);?>                  
<?php
}
?>
</div>
</div>
<?php endforeach; ?>

Tried with above code to display all images, but getting error Not allowed to load local resource when I open Google Chrome Inspect Element

JKLM
  • 1,470
  • 3
  • 28
  • 66

3 Answers3

2

i think you are using a local url instead of using this

<?php echo  Html::img('@backend/web'.'/'.$value_img);?>   

try using it like

  <?= Html::img(Yii::getAlias('@web').'/images/'.$value_img]);?>
Midhun
  • 3,850
  • 1
  • 23
  • 26
  • by using @web it will get files from frontend web folder I need to use files from backend web folders to frontend of my app – JKLM Apr 09 '16 at 11:47
  • can u try a custom alias in the common config Yii::setAlias('@res', 'http://example.com/backend/web/images/'); an in the view use like = Html::img(Yii::getAlias('@res').'/images/'.$value_img]);?> – Midhun Apr 09 '16 at 13:19
1

Images must be accesible by an url, like

yoursite.com/backend/imagedir/IMG'

If yoursite.com/backend points to your backend/web folder.

Backend alias points to your local path, so you need a custom alias to reach image folders.

Yii2 aliases: http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html

1

As stig-js answered you can't load local saved image directly, If you're really interested into loading resources from a local path, you can open image as a binary file with fopen and echo the content of it with a proper header to output. In general way, you can add a method to your model like this:

public function getImage($imageName)
{
    $imagePath = '@backend/web' . '/' . $imageName;

    $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
    $contentType = finfo_file($fileInfo, $imagePath);
    finfo_close($fileInfo);

    $fp = fopen($imagePath, 'r');

    header("Content-Type: " . $contentType);
    header("Content-Length: " . filesize($imagePath));

    ob_end_clean();
    fpassthru($fp);
}

P.S: Also you can use combination of this answer with showing image as base64 on HTML. See How to display Base64 images in HTML?

Community
  • 1
  • 1
meysam
  • 1,754
  • 2
  • 20
  • 30