简体   繁体   English

Laravel使用HTML :: image作为背景图像

[英]Laravel use HTML::image for background-image

how to use HTML::image() for inline styled with background-image such as this html tags: 如何将HTML::image()用于具有background-image样式的内联样式,例如以下html标签:

this line is into my main.blade.php file and that is main skeletion of my template. 这行代码进入了我的main.blade.php文件,这是我模板的主要骨架。

<div style="height: 45px;background-image:url('../public/images/header_pattern2.png');border-radius: 5px;position:relative">

in main.blade.php my page view correct template. 在main.blade.php中,我的页面查看了正确的模板。 but after redirect such as login.blade.php images do not show. 但是重定向后,例如login.blade.php图像不会显示。 but after switch to index i do not have a problem. 但是切换到索引后我没有问题。

in public directory i have images , js , css folders and my public is this: 在公共目录中,我有imagesjscss文件夹,而我的公共是这样的:

'public' => __DIR__.'/..',

i can remove public directory. 我可以删除公共目录。 how to convert background-image:url('../public/images/header_pattern2.png'); 如何转换background-image:url('../public/images/header_pattern2.png'); to blade HTML::image() tag? 刀片HTML::image()标签?

You may try this: 您可以尝试以下方法:

<div style="height: 45px;background-image:url('{{ asset('images/header_pattern2.png') }}');border-radius: 5px;position:relative">

Update: 更新:

Actually, HTML::image() generates an <img /> tag and you are using a div, if you want to use this using HTML::div() then you may create a custom macro . 实际上, HTML::image()生成一个<img />标记,并且您正在使用div,如果要使用HTML::div()来使用它,则可以创建一个自定义宏

Update: I've created this custom macro : 更新:我已经创建了这个custom macro

/**
 * Param $attr : An array of styles
 * Param $html : HTML content for div
 * Returns HTML DIV
 */
HTML::macro('divWithBG', function($attr = array(), $html = ''){
    $style = 'style = ';
    foreach ($attr as $key => $value) {
        if($key == 'background-image') $style .= $key.':url(\'' . $value .'\');';
        else $style .= $key . ':' . $value .';';
    }
return "<div $style >$html</div>";
});

You may use it in blade view as: 您可以在blade view中将其用作:

{{ HTML::divWithBG(array('background-image' => asset('images/8_big.jpg'), 'height' => '45px', 'border-radius'=>'5px', 'position'=>'relative' )) }}

Output: 输出:

<div style="background-image:url('http://blog.dev/images/8_big.jpg');height:45px;border-radius:5px;position:relative;"></div>

Rendered Output: 渲染输出:

alt text

Use the following function to solve it. 使用以下函数来解决它。 This is assuming that the image exist. 这是假设图像存在。

public function inline($path)
{      
    $filetype = File::type( $path );
    $response = Response( File::get($path), 200 );
    $response->header('Content-Type', $filetype);
    return $response;
}

我尝试了这个并且有效

 background: url('{{asset('uploads/avatars/'.$employee->avatar)}}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM