首页 > 解决方案 > 如何使用带有yii2语法的html代码增加图片?

问题描述

嗨伙计们,以下代码将通过单击它来增加图片:

      <a class="example-image-link" href="http://lokeshdhakar.com/projects/lightbox2/images/image-1.jpg"><img  src="http://lokeshdhakar.com/projects/lightbox2/images/thumb-1.jpg" alt="image-1" /></a>

)

在我的 yii2 应用程序中,我在匿名函数中有以下有效图片 URL:

 $url = '@web/img/' . $bild->dateiname;

如何在 yii2 语法中转换上层代码。我尝试这样,但它不起作用:

return  Html::a(
        Html::img($url, ['alt' => 'PicNotFound', 'class' => 'img-circle', 'style' => 'width:225px;height:225px']
                  ), [Html::img($url, ['alt' => 'PicNotFound']
                                )], ['title' => 'Immobiliendaten abrufen', 'data' => ['pjax' => '0'], 'class' => 'example-image-link']
);

Firedebug 将显示以下 html 代码。链接中的url是错误的!

a class="example-image-link" href="/yii2_ErkanImmo/frontend/web/index.php/" title="Immobiliendaten abrufen"><img class="img-circle" src="/yii2_ErkanImmo/frontend/web/img/villa1.jpg" alt="PicNotFound" style="width:125px;height:125px;">event
<img class="img-circle" src="/yii2_ErkanImmo/frontend/web/img/villa1.jpg" alt="PicNotFound" style="width:125px;height:125px;">
</a>

改写问题

在使用如下代码单击图片后,我将获得如附件所示的输出:

    return Html::a(Html::img($url, ['alt' => 'image-1']), $url, ['class' => 'example-image-link']);[![enter image description here][1]][1]

在此处输入图像描述

这是视图文件的完整代码(用于 GRU)

 [
                'attribute' => $dummy,
                'label' => Yii::t('app', ''),
                'format' => 'html', // sorgt dafür,dass das HTML im return gerendert wird
                'vAlign' => 'middle',
                'value' => function($model) {
                    $bmp = '/bmp/';
                    $tif = '/tif/';
                    $png = '/png/';
                    $psd = '/psd/';
                    $pcx = '/pcx/';
                    $gif = '/gif/';
                    $jpeg = '/jpeg/';
                    $jpg = '/jpg/';
                    $ico = '/ico/';
                    try {
                        $bilder = \frontend\models\Dateianhang::GetBild($model);
                        foreach ($bilder as $bild) {
                            if (preg_match($bmp, $bild->dateiname) || preg_match($tif, $bild->dateiname) || preg_match($png, $bild->dateiname) || preg_match($psd, $bild->dateiname) || preg_match($pcx, $bild->dateiname) || preg_match($gif, $bild->dateiname) || preg_match($jpeg, $bild->dateiname) || preg_match($jpg, $bild->dateiname) || preg_match($ico, $bild->dateiname)) {
                                $url = '@web/img/' . $bild->dateiname;
                            }
                        }
return Html::a(Html::img($url, ['alt' => 'image-1', 'class' => 'img-circle', 'style' => 'width:225px;height:225px']), $url, ['class' => 'example-image-link', 'title' => 'Touch picture to increase', 'target' => '_blank']);
                        //return Html::img($url, ['alt' => 'Bewerberbild nicht vorhanden', 'class' => 'img-circle', 'style' => 'width:225px;height:225px']);
                    } catch (Exception $e) {
                        return;
                    }
                }
 ],

既不会增加图片,也不会增加 target=>_blank 的工作:=(

标签: yii2-advanced-app

解决方案


尝试以下代码 -

<?php
$url = 'http://lokeshdhakar.com/projects/lightbox2/images/thumb-1.jpg';
//in your case
//$url = '@web/img/'.$bild->dateiname;
echo Html::a(
  Html::img($url, ['alt'=>'image-1']),
$url, ['class'=>'example-image-link']);
?>


如果要重定向到控制器中的某些操作,请将$url最后一行替换为['/controller/action']


推荐阅读