How to implement PHP inside a image src code? - php

<img src="socimages/logo/"<?php if ($soca == ""){ echo "logo.jpg"; } else { echo $anotherimage . ".jpg";} ?>>
Basically what I am trying to do is to change the end part of the image src, to pick from a range of specifically named images.
The syntax for the code is obviously not right since I closed the src after /logo/ with the ". But if I do not have the " then my php will not function.

I would suggest, don't mess the logic in your img tag.
<?php
$img = ($soca == "") ? "logo.jpg" : $anotherimage.".jpg";
?>
<img src="socimages/logo/<?php echo $img; ?>" />

Use a ternary, and remember to distinct between "" and '' :
<img src="socimages/logo/<? echo $soca == '' ? 'logo.jpg' : $anotherimage . '.jpg'; ?>">
This will produce a correct <img src="image.jpg"> tag.

A ternary conditional makes the line shorter:
<img src="socimages/logo/<?php echo ($soca=='')?'logo.jpg':$anotherimage.'.jpg';?>">
This is probably shortest:
<img src="socimages/logo/<?php echo ($soca=='')?'logo':$anotherimage;?>.jpg">
Or even this variant, but it depends on the short tags configuration of your site:
<img src="socimages/logo/<?= ($soca=='')?'logo':$anotherimage;?>.jpg">
But all variants are horrible style. You should always try to implement in a transparent manner, easy to read and understand. So instead store the final path inside a variable and echo that into the src attribute of the img tag.

Related

WordPress IF statement isn't outputting ELSE

I have a straightforward if statement to evaluate whether or not an image is set to appear as the logo on a WordPress build. If it returns empty/false, it displays a default image whose location is set as an absolute value.
The problem is when an image ISN'T set, the else statement is failing. I'm not receiving an error, but the code returned is simply an image tag without any source i.e. "< img src >".
Here is the statement:
<?php
$logo = $wp_options['header_logo'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo['url']; ?>">
<?php } else { ?>
<img src="wp-content/themes/wpdev/images/logo.png">
<?php } ;?>
I guess that if statement is failing, because you are treating $logo variable as a string.
It seems you're using $logo variable as an array, if you want to check out if it is empty you can use is_null() function of PHP.
By the way, we can't understand your problem this way, you should be more specific. Share your error or warning messages, the way it's behaving, etc..
Found a solution to this by specifically referring to the url of the image array when the variable $logo was defined. This way, the IF is evaluating the URL of the image.
The problem seemed to be that the initial IF was being evaluated as TRUE but obviously not returning the URL as this wasn't originally specified in $logo. By looking for the URL in the 'header_logo' array, it corrected the problem.
<?php
$logo = $wp_options['header_logo']['url'];
if(isset($logo) && ($logo !='')) { ?>
<img src="<?php echo $logo; ?>">
<?php } else { ?>
<img src="<!-- Image Location -->">
<?php } ;?>

How to put php tag in background url?

I have code like this ;
$Photo = filtering($row['FileIcon128']);
<div class="image" style="background:url(/images/imgs/<?php echo $Photo ?>)"></div>
I tried everything
filtering($row['FileIcon128']); , echo ''.$Photo.'';
instead of
$Photo
But i cant get picture. Any idea?
By the way, i have to use
<div class="image"
Give this a shot:
<?php
$Photo = filtering($row['FileIcon128']);
$string = "/images/imgs/" . $Photo;
?>
<div class="image" style="background-image:url('<?php echo $string; ?>')"></div>
Explanation
I saw 3 possible bugs in your code, which were:
1) <?php echo $Photo ?> without a semicolon after $Photo. I don't know if that's a syntax error or not in inline php, however I added the semicolon to make sure and concatenated the static string with the dynamic variable to make for a less messy inline PHP like this: <?php echo $string; ?>.
2) background:url() is not the right way to do this. You want to be as explicit as you can, so background-image:url().
3) background-image:url() without '' (apostrophes) surrounding the parameter passed. background-image:url('') fixed that. I just echoed the code in between the two apostrophes.

Defining <img src = according to the end of the link

How do I set the src= of a <img> element, according to the end of my link?
Example 1:
Link:
http://www.endertec.com.br/hf/see-img.php?img=http://www.endertec.com.br/hf/do.php?imgf=325356456.png
<img> element in see-img.php file:
<img src="http://www.endertec.com.br/hf/do.php?imgf=325356456.png"/>
Example 2:
Link:
http://www.endertec.com.br/hf/see-img.php?img=http://www.endertec.com.br/hf/do.php?imgf=342424234.png
<img> element in see-img.php file:
<img src="http://www.endertec.com.br/hf/do.php?imgf=342424234.png"/>
I.E.: I want to know if there is any script that I can use in see-img.php do what I was demonstrating above file.
Edited
Try this:
<?php
//$str = 'http://www.endertec.com.br/hf/see-img.php?img=http://www.endertec.com.br/hf/do.php?imgf=342424234.png';
$str = 'http://www.endertec.com.br'.$_SERVER['REQUEST_URI'];
$src = str_replace('see-img.php?img=' , '' ,stristr($str , 'see-img.php?img='));
echo '<img src="'.$src.'"/>';
?>
In PHP you could do this:
$img_url = isset($_POST['img']) ? $_POST['img'] : null;
$img_file = isset($_POST['imgf']) ? $_POST['imgf'] : null;
Then with that $img_url you can do this:
if (!empty($img_url) && !empty($img_file)) {
echo '<img src="' . $img_url . '?imgf=' . $img_file . '"/>';
}
The idea is to get the URL values which are known as post values, assign them to a variable & then do a check where you echo the values into <img src= tags.
But that is the best that I can do based on the lack of detail in your question. But the concept is solid and should work in PHP.
You could try using a built in php function know as sttrpos, it will return an int with the position where the substring you need begins. Then a normal php substring will do the trick.

Detect empty variable in PHP not working

I have been fighting with the following code and variants, but have not been able to come up with anything that works. The idea is that I detect if the variable $largeimg6 (the URL of an image) is empty or not (whether an image has been loaded) and if not, load the value of a default variable ($defaultimage) which holds the URL of a default image..
<?php if(!empty($largeimg6)) : ?>
<img class="rpPicture" id="rpPicture<?php echo $key; ?>" onload="showPicture(this, <?php echo SHOW_PICTURES; ?>)" width="60" height="60" src="<?php echo $largeimg6; ?>" alt="Buy CD!" title="Buy CD!" />
<?php endif; ?>
The problem is that I do not know where to add the arguement for $largeimg6 being empty and therefore adding:
$largeimg6 = $defaultimage
Any help would be appreciated.
Thanks.
You can try this before that if statement:
<?php $largeimg6 = $largeimg6 != '' ? $largeimg6 : $defaultimage; ?>
The != '' comparision may be change according to your need to isset(), strlen(), is_null()(as Ville Rouhiainen suggested) or whatever.
You can use the alternative (and trim as already suggested):
$largeimg6 = trim($largeimg6);
if (isset($largeimg6)&&strlen($largeimg6)>0)
but you'll probably be doing better by filtering the url:
$largeimg6 = trim($largeimg6);
if(filter_var($largeimg6, FILTER_VALIDATE_URL))
More info: http://php.net/manual/es/book.filter.php
Solution
This answer assumes that you have a variable $largeimg6 which is either going to be set to a url or be empty.
That being the case it's a fairly simple fix. You need to remove the if statement entirely and replace your:
<?php echo $largeimg6; ?>
with:
<?php echo (empty($largeimg6)) ? '/default/image.jpg' : $largeimg6; ?>
Explanation
The above is equivalent to:
if(empty($largeimg6)){
echo '/default/image.jpg';
}
else{
echo $largeimg6;
}
But in the form:
(IF) ? THEN : ELSE;

if statement on one line if poss

I am printing an image using an ID which is generated. however i wanted to do a check to see if this image exists and if it doesnt print no-image.jpg instead...
<img src="phpThumb/phpThumb.php?src=../public/images/'.$row["id"].'/th.jpg&w=162" alt="" width="162" />
It would be great if this could be kept on one line is possible. Any help would be appreciated.
What Kristopher Ives says, and file_exists:
echo (file_exists("/path/file/name/here") ? "web/path/goes/here" : "no_image.jpg")
btw, your snippet is unlikely to work, as you seem to be combining plain HTML output and PHP without putting the PHP into <? ?>
My recommendation would actually be to abstract the decision making from the html tag itself, in a separate block of php logic that is not outputting html...here is an abbreviated example that assumes you are not using a template engine, or MVC framework.
<?php
$filename = 'th.jpg';
$filePath = '/public/images/' . $row['id'] '/';
$webPath = '/images/' . $row['id'] . '/';
//Logic to get the row id etc
if (!file_exists($filePath . $filename)) {
$filename ='no-image.jpg';
$webPath = '/images/';
}
?>
<img src="<?php echo $webpath . $filename;?>" />
Wow, this question gets asked and answered a lot:
http://en.wikipedia.org/wiki/Ternary_operation
You could do it by:
<img src="<?php ($row['id'] != 0) ? "../public/{$row['id']}.jpeg" : 'no_image.jpg'; ?> >

Categories