img tag in php code [duplicate] - php

This question already has answers here:
How to extract img src, title and alt from html using php? [duplicate]
(10 answers)
Closed 9 years ago.
How can I use img tag in php code?:
This is the code I have so far:
$message = $message."...<br />".$lang->messagemore."";
And I wish to use an image instead of ".$lang->messagemore." but I don't know how to use img tag in php :|
I have tried googling but I only find some tag info from html but not php code.
<img src="/res/gif/bullet_info_sq.gif" alt="" />
But I wish to use that in php so I could use your help.
Thank you very much!

tl;dr
Replace: $lang->messagemore
With: <img src="/res/gif/bullet_info_sq.gif" alt="" />
When you replace your PHP variable with the img tag, the HTML must still remain within the quotes of the PHP string, and thus the actual quotes for your img tag must be escaped, just as you've done for your a tag. You may also opt to use single quotes, to avoid having to escape. This should result any one of the following equivalent code snippets (I've added whitespace for readability):
Double quotes (with escaping):
$message = $message . "...<br />
<a href=\"" . $mybb->settings['bburl'] . "/" . $announcement['threadlink'] . "\">
<img src=\"/res/gif/bullet_info_sq.gif\" alt=\"\" />
</a>";
Single quotes for HTML attributes:
$message = $message . "...<br />
<a href='" . $mybb->settings['bburl'] . "/" . $announcement['threadlink'] . "'>
<img src='/res/gif/bullet_info_sq.gif' alt='' />
</a>";
Single quotes for PHP string:
$message = $message . '...<br />
<a href="' . $mybb->settings['bburl'] . '/' . $announcement['threadlink'] . '">
<img src="/res/gif/bullet_info_sq.gif" alt="" />
</a>';

Just echo the HTML code ( in your case , the <img> tag ) in PHP , just like you did for <a> tag.

You can use like this,
$message = $message."...<br /><img src='/res/gif/bullet_info_sq.gif' alt='' />";

use this:
$message = $message."...<br />".'<img src="/res/gif/bullet_info_sq.gif" alt="" />'."";

Use this
$message = $message."...<br /><img src=\"/res/gif/bullet_info_sq.gif\" alt=\"\" />";

It works like that:
$message = $message."...<br />"."<img src='".$aImageURL."' alt="" />";
Simply replace de img src with a PHP variable. ($aImageURL will be the URI of the image)
Take a look at the quotes (I put simply quotes in the src attribute, you can do that)

So simple, do not create mess with double quotes " PHP optimally used single quotes '. Use standard html within string!
echo ' <img src="php.gif" /> ';

Related

Correct PHP Syntax

I need to add this HTML tag:
<img src = "<?php echo esc_url( get_avatar_url( $post->post_author ) ); ?>" />
Inside PHP:
echo '<h3 class="nome-vendor-cat">',
$current_cat->name,
" di ",
'<a href="' . $vendor_link->get_shop_url() . '" class="nome-vendor">',
get_usermeta( $post->post_author, 'dokan_store_name' ),
'</span>',
'</h3>';
I have to insert that image in PHP, but it always generates syntax errors.
Based on:
<img src = "<?php echo esc_url(get_avatar_url($post->post_author));?>" />
Detect the PHP part:
esc_url(get_avatar_url($post->post_author))
Detect the HTML part:
<img src = "
//and
" />
Write it in PHP:
echo '<img src = "'.esc_url(get_avatar_url($post->post_author)).'" />';
And you should better use . for concating things.
Its ok to use , when you echo stuff.
But if you replace echo with an assignment $var = , you have to replace all , with . to get it work again.
So always use ..

How to add "../" into a PHP echo img src

In my code I show some images in this way:
...
<?php echo '<img src="' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="' .$imagetwo. '" alt="" /> '?></a></div>
...
where $imageone, $imagetwo have path from Mysql database
e.g.
$imagesone = "images/exposition/02-05-2017-11-28-00-foto 2_b.JPG";
This code is working in the same place where I have my images folder, but now I need to put the same code in a subfolder page and I'd like to use the same image.
So, I need to put dinamically "../" before my varible, something like this:
...
<?php echo '<img src="../' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="../' .$imagetwo. '" alt="" /> '?></a></div>
...
but it's not working.
Any suggestion?
EDIT This is my solution:
if ($images1 != ""){
$imageone = "../".$images1;
}
if ($images2 != ""){
$imagetwo = "../".$images2;
}
in this way I fixed my code and it's working!
Since you're using double quotation marks after src, you can just write down <img src="../$imageone" alt="" />
There's no need to use simple quotation marks to write the variable because the double quotation marks gets the variable number.

How to php echo an image with set size [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am echoing an image which works fine, but then i want to set how big it needs to be, and I just can't seem to get the syntax right.
This is what I have:
echo "<img src='" . $row['imglink'] . "' height="130" width="150"> ";
What am I doing wrong? If I remove the height and width it works fine.
You are mixing single and double quotes. So you get a syntax error.
This works:
echo "<img src='" . $row['imglink'] . "' height='130' width='150'> ";
If you like to output double quotes you have to escape:
echo "<img src=\"" . $row['imglink'] . "\" height=\"130\" width=\"150\"> ";
Or you switch to cover the string in single quotes:
echo '<img src="' . $row['imglink'] . '" height="130" width="150"> ';
try changing double quotes into single quotes for these attributes
height='130' width='150'
since double quotes are actually delimiting the PHP string. Optionally you may instead escape the double quotes like so
height=\"130\" width=\"150\"
but this syntax is less readable and more error prone (not recommended)
ok its here
echo '<img src="'$row['imglink']'" height="130" width="150">';
You can uses variables inside double quotes, like this:
<?
$img_url = $row['imglink'];
echo "<img src='$img_url' height='130' width='150'>";
?>
Sometimes the way that you're using causes to errors if you don't do it properly.Therefore you can try this way also.
<img src="<?php echo $row['imglink']?>" height="130" width="150" />
if you're using this inside a loop,you can do like this.
<?php foreach($result as $rows):?>
<img src="<?php echo $row['imglink']?>" height="130" width="150" />
<?php endforeach; ?>
Hope this helped to you.

Link to be displayed as imgsrc

I have a code here that outputs a image link like http://img.domain.com/2515.jpg
<?php echo IMG_URL . $code . ".jpg" ?>
But i want to make it print this entire thing <img src="http://img.domain.com/2515.jpg" alt="" title="Created by domain.com" />
How can i format that php string <?php echo IMG_URL . $code . ".jpg" ?>
to include that entire img src link?
I am trying to fit it in this html code below
<li><a class="linkInsert" data-value="<?php echo IMG_URL . $code . ".jpg" ?>">Direct Link (email & IM)</a></li>
Update:
i figured it out below with just using '
<li><a class="linkInsert" data-value='<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">'>HTML Image (websites / blogs)</a></li>
Try this,
<?php echo '<img src="'.IMG_URL.$code.'.jpg" alt="" title="Created by domain.com" />' ;?>
You just have to use single quotes and double quotes alternatively.
PHP can be embedded inside HTML:
<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">

Setting size of an image in PHP

I am trying to set size of an image in PHP but it's not working..
echo "<img src=".$media."width=200 height=200";
$media is the src link. Without the width and height attributes, it works perfectly well. I think that 200 must be enclosed in double quotations but I am unable to do that. Any ideas on how to solve this problem?
width is currently concatenated with your file name. Use:
echo '<img src="'.$media.'" width="200" height="200" />';
The /> closing tag is necessary to correctly render your image element.
The quotes around tag names are recommended. Omitting these quotes will only cause issues when:
The attribute value contain spaces, or
You forgot to add a space after the attribute value
Yet another alternative, just for kicks:
echo <<<EOL
<img src="{$media}" width="200" height="200" />
EOL;
aka a HEREDOC.
That is because that is not proper HTML.
In your code you'd get something like:
<img src=image.jpgwidth=200 height=200
And what you need is:
<img src="image.jpg" width="200" height="200" />
So do:
echo '<img src="' . $media . '" width="200" height="200" />';
echo '<img src="' . $media . '" width="200" height="200" />';
You should be able to make it work with the following code
echo "<img src=\"".$media."\" width=200 height=200>";

Categories