How to have image src a variable? - php

I want to keep src attribute in a variable. I am working on php and tried the following method but it doesn't display the image on html page.
Code:
<?php $path="C:/horizontal.jpg"; ?>
<image src="<?php echo $path; ?>" style="width:304px;height:228px" />

I think you can use this technique:
<?php $path="http://" . $_SERVER["SERVER_NAME"];?>
<img src="<?php echo $path.'/projectName/image.jpg'; ?>" style="width:304px;height:228px" />
Where:
$_SERVER["SERVER_NAME"]: to get the server name, for example: www.example.com.
Also you can use $_SERVER["SERVER_ADDR"], in this case you can get the address IP of your server, for example: "127.0.0.1".
I hope this information helps you.
Good Luck.

<?php $path="localhost/projectName/";?>
<img src="<?php echo $path.'image.jpg'; ?>" style="width:304px;height:228px" />

Related

Image not loading even though path is correct

The file path is:
theme
assets
src
images
grey-arrow.svg
Markup:
<?php $getIcon = get_template_directory().'/assets/src/images/grey-arrow.svg';?>
<div><img src="<?php echo $getIcon; ?>"/></div>
<?php echo $getIcon;?>
The image doesn't load and an echo of $getIcon returns:
/var/www/html/wp-content/themes/theme/assets/src/images/grey-arrow.svg
... Which is the correct path. Ideas on why the image doesn't load?
You should echo it and also you are closing your php tag properly.
<img src="<?php echo get_template_directory_uri(); ?>/assets/src/images/grey-arrow.svg"/>
or you can use bloginfo which is easier to remember and use (You need not echo)
<img src="<?php bloginfo('template_url'); ?>/assets/src/images/grey-arrow.svg"/>

Linking in wordpress, which link is right?

Which link version is right - if any?
contact-us
contact-us
and image links:
<img alt="xxx" src="<?php echo get_home_url(null, 'fl-images/banner/teaser.png', null); ?>">
<img alt="xxx" src="<?php echo get_home_url(); ?>/fl-images/banner/svatebni-teaser.png">
For <a> tag use.
kontakty
For <img> tag use.
<img alt="xxx" src="<?php echo home_url('/fl-images/banner/svatebni-teaser.png'); ?>">
In your case both will work assuming fl-images is in the public root.
However, if you want to reference a blog id or add a scheme to the home URL context then you can use the arguments. eg: get_home_url( 2, 'contact-us/', 'https');.
See docs here

How can I transfer data from php back to HTML?

My site reads all files in a directory using glob(); statement in php. I want to use those files' names as a source for images to be displayed as
<img src="Here I want those file names from php">
I am a beginner and I hope if anyone can help me for this.
$files = glob();
<img src="$files[0]" />
<img src="$files[1]" />
You can use forloop to show all images.
<?php for($i=0;$i<count($files);$i++){ ?>
<img src="<?php echo $files[$i] ?>" />
<?php } ?>

setting the img src via php?

Here is my problem.
If I do:
$imagePath = "images/spalt_images/body_images/525A.JPG";
?>
<img src="<?php $imagePath ?>" alt="front image" class="productImage"/>
<?php
Then my image does not show up.
However, if I do:
<img src="images/spalt_images/body_images/525A.JPG" alt="front image" class="productImage"/>
Then my image shows up just fine. Why would it not work with php?
Thanks
You need to echo it:
<?php echo $imagePath ?>
or use a short tag (not recommended, but that's another discussion):
<?=$imagePath ?>
You need to echo $imagepath otherwise it won't 'print' it out.
Change <?php $imagePath ?> in your code to <?=$imagePath?>, or add an echo if your server doesn't allow shorttags.

PHP echo function inside a HTML link

I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
<img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>
Turn
<?php $image=troll/GrannyTroll.jpg?>
into
<?php echo "troll/GrannyTroll.jpg"; ?>
?
Or provide more details on what you are trying to achieve.
Also, you might consider urlencode-ing some of those URL parameters.
Edit:
So you might try setting the variable beforehand:
<?php $image = "troll/GrannyTroll.jpg"; ?>
<img src="<?php echo $picture; ?>" width="100" height="94" />
So now i understand what you are trying to do.
One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:
$image = 'troll/GrannyTroll.jpg';
The second error is that you do it in the wrong order, you have to define $image first, before you use it.
That's what I believe you want to do:
<?php
$image = "troll/GrannyTroll.jpg";
?>
<img src="<?php echo $image; ?>" width="100" height="94"/>

Categories