PHP echo background image - php

I know I'm probably missing something really obvious, but the background image isn't pulling through...
<div style='background-image: url(<?php echo $blogImage; ?>");'>
I've also tried:
<div <?php echo "style='background-image: url($blogImage);'";?> >

Syntax error
Use this
<div style="background-image: url('<?php echo $blogImage; ?>');">
Hope this helps

You have a missing " before the php opening tag. try to add it and check.
<div style='background-image: url("<?php echo $blogImage; ?>");'>

Related

PHP in Background-image tag

On a Wordpress theme site I have this code:
<div class="gallery-img" style="background-image: url(<?php echo esc_url($image['sizes']['thumbnail']); ?>);">
However I need the background image url to be in ''.
So it works like this:
<div class="gallery-img" style="background-image: url('<?php echo esc_url($image['sizes']['thumbnail']); ?>');">
With it like that the PHP then does not work, how can I do the above so it works and so it has the '' around it?
How about inverting your single and double quotes ?
<div class="gallery-img" style='background-image: url("<?php echo esc_url($image['sizes']['thumbnail']); ?>");'>

Why PHP echo in `background-image:ulr()` always get an escape character?

My code always got this problem:
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = 'background-image: url(\''.$imgurl.'\'); ';
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;">
But whatever I do, the html always shows:
<figure id="centerbg" class="centerbg" style="background-image: url("https://cdn.jsdelivr.net/example.png"); background-position: center center; background-attachment: inherit;">
I also tried:
<figure id="centerbg" class="centerbg" style="background-image: url(<?php echo $imgurl ?>);background-position: center center;background-attachment: inherit;">
But no use at all!
Can anyone help me? TAT
Thanks for your help, but seems my and your codes all work fine in this single file:https://api.mashiro.top/cover/test.php , but always get the escape character in my production site (the same HTML id as the cover image): https://2heng.xin/ , really strange, and could be any possibility that other parts get wrong in my site?
I don't know why style="background-image: url("https://cdn.jsdelivr.net/example.jpg");" could show images in my browesr (Chrome & Firefox), but this is not a good expression ugh?
You can also just remove the single quotes on your $get_style variable.
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = 'background-image: url('.$imgurl.');';
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;height:100%;">
And it seems your site is using WordPress, so this link might help you as well.
You can mix double-quotes and single-quotes for this situation:
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = "background-image: url('".$imgurl."'); ";
?>
<figure id="centerbg" class="centerbg" style="<?php echo $get_style; ?>background-position: center center;background-attachment: inherit;">
Just us this.
<?php
$imgurl = "https://cdn.jsdelivr.net/example.png";
$get_style = "background-image: url('".$imgurl."');";
?>
You also can take a look into http://php.net/manual/en/function.htmlspecialchars-decode.php if you still face the same issue.
Try this out - I use this method a lot without issues.
<?php $imgurl = 'https://cdn.jsdelivr.net/example.png'; ?>
<figure id="centerbg" class="centerbg" style="background-image: url('<?php echo $imgurl; ?>'); background-position: center center; background-attachment: inherit;">

initialize image background in css using php variable

I have initialize a variable to fetch from this directory as shown
<?php $imgSrc = base_url()."decoy/thumbs/".$item["filename"]; ?>
now I want to assign the variable to a background url of css code block as shown but it is not displaying the image background
<div style="background: url('<?php echo $imgSrc ?>') no-repeat;" class="large"></div>
can anyone kindly assist me on how I can I achieve the above.
Follow these easy steps to investigate your problem.
After getting the image
<?php $imgSrc = base_url()."decoy/thumbs/".$item["filename"]; ?>
Verify what was returned to the image variable <?php echo $imgSrc; ?>
That way you will be better placed if you find nothing wrong update your question with the necessary information.
Finally I prefer you use string concatenation instead change this
<div style="background: url('<?php echo $imgSrc ?>') no-repeat;" class="large"></div>
to
<?php
$imgSrc = base_url()."decoy/thumbs/".$item["filename"];
echo '<div style="background:'.url($imgSrc).' no-repeat;" class="large"></div>';
?>
You need to put the "imagePath" in place of "imgSrc" so that the image will prompt in backgroud, because in the backroud-url there is no need of absoulte path only relative path will be ok for this.
$imgPath = "../decoy/thumbs/".$item["filename"]";
<div style="background: url('<?php echo $imgPath; ?>') no-repeat;" class="large"></div>

How can I show my image in CodeIgniter base_url

How can I show my image in this situation?
Here is my code:
<div class="clinic-logo" style="background-image: url( <?= base_url() ?>asset/dist/css/img/mylogo.gif )"></div>
When I saw it in inspect element it gives me this:
background-image: url( http://localhost/clinic/asset/dist/css/img/mylogo.gif );
Why does it say 'could not load image'?
Try this..
<div class="clinic-logo" style="background-image: url('<?php echo base_url();?>asset/dist/css/img/mylogo.gif')"></div>
Don't forget to load url helper in application/config/autoload.php.
should do the trick
<div class="clinic-logo" style="background-image: url("<?php echo base_url('asset/dist/css/img/mylogo.gif');?>")"></div>
anything passed into base_url() will be appended
<div class"container">
<img src="<? = base_url('Your image path')?>">
</div>

Variable in URL in PHP Echo

I need some help..
I have a line of PHP that is echoing a url for me -
<?php foreach($day as $item) : ?>
<li style="background: url('<?php echo http://$base:$sickport/api/$SICKAPI/?cmd=show.getbanner&tvdbid=$item['tvdbid']?>')"><?php echo $item['show_name'] ?> </br> <?php echo $item['ep_name'] ?></li>
<?php endforeach ?>
The problem I have is that the URL here -
<li style="background: url('<?php echo http://$base:$sickport/api/$SICKAPI/?cmd=show.getbanner&tvdbid=$item['tvdbid']?>')">
Appears to kill the code, so I assume my syntax is wrong, if I remove the "background stuff" the code works and the other 2 variables are mapped ok, i'm now wanting to set the backgroud of each li as per the image being returned from the API call, but I can't get it working..i'm sure someone here will sort it really easily for me..
Thanks
Yes you have syntax error in your code:
Modified Example:
<li style='background: url(<?php echo 'http://$base:$sickport/api/$SICKAPI/?cmd=show.getbanner&tvdbid='.$item['tvdbid'];?>)'>
Alternate Solution:
<?php
$url = "http://$base:$sickport/api/$SICKAPI/?cmd=show.getbanner&tvdbid=".$item['tvdbid'];
?>
<li style='background: url(<?php echo $url; ?>)'>

Categories