Laravel 使用 HTML::image 作为背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22235431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel use HTML::image for background-image
提问by DolDurma
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. but after redirect such as login.blade.php
images do not show. but after switch to index i do not have a problem.
在 main.blade.php 我的页面查看正确的模板。但重定向后如login.blade.php
图像不显示。但切换到索引后,我没有问题。
in public directory i have images
,js
,css
folders and my public is this:
在公共目录中,我有images
, js
,css
文件夹,我的公共目录是:
'public' => __DIR__.'/..',
i can remove public directory. how to convert background-image:url('../public/images/header_pattern2.png');
to blade HTML::image()
tag?
我可以删除公共目录。如何转换background-image:url('../public/images/header_pattern2.png');
为刀片HTML::image()
标签?
回答by The Alpha
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:
渲染输出:
回答by mookofe
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;
}
回答by Waleed
I tried this and it worked
我试过了,它奏效了
background: url('{{asset('uploads/avatars/'.$employee->avatar)}}')