An easier approach will be providing the default img
tag inside the modal and providing the source on clicking the image inside DetailView
which will load the image inside the modal.
Also, the image can be of any size and so can be your modal window, which can be configured to any size small or large using class .modal-lg
or .modal-sm
classes, so add the img-responsive
class to the default img
tag inside the modal window and the images will be responsive relative to the size of the modal window.
So, the first thing to do is add an image tag inside the modal with class img-responsive
and src='//:0'
, you can Read Here why use ://0
for empty image tag
\yii\bootstrap\Modal::begin(['id' => 'identity_file', 'footer' => '<a href="#" class="btn btn-sm btn-primary" data-dismiss="modal">Close</a>']);
echo "<img src='//:0' class='img-responsive'>";
\yii\bootstrap\Modal::end();
Add a class to the image inside the DetailView
code, I assume the $user->identity_file
has the full path of the file.
<?php echo DetailView::widget(
[
'model' => $user,
'attributes' => [
'id',
[
'attribute' => 'identity_file',
'value' => $user->identity_file,
'format' => [
'image',
['width' => '120', 'height' => '120', 'class' => 'popup-image'],
],
],
],
]
)
?>
Then add the following javascript on top of your view where you are using the DetailView
widget.
$js = <<<JS
$(".popup-image").on('click',function(){
$("#identity_file img").attr('src', $(this).attr('src'))
$("#identity_file").modal('show');
});
JS;
$this->registerJs($js, View::POS_READY);
now click on the image and you will see a responsive image contained inside the modal window with responsive dimensions.