Why won't my image name convert to lowercase in PHP? - php

Here is the code I'm using:
<img src="/images/<?php strtolower(the_title()); ?>.jpg" border="0" >

Wordpress the_title() function echoes the title by default. You need to set the echo argument to false and echo the lowercase output yourself.
<img src="/images/<?php echo strtolower(the_title('','',false)); ?>.jpg" border="0" />

It looks like the_title() actually echo's out the title since you don't have an echo statement in your snippet. So your call to strtolower() is basically doing nothing. You'll need to capture the output of the_title() then you can convert it to lower case.
ob_start();
the_title();
$title = $template = ob_get_clean();
<img src="/images/<?php echo strtolower($title); ?>.jpg" border="0" />

Related

Assign Image SRC in php

After retrieve image path from database I want to pass it to <img> tag to show up,
I read this solution, but I do not want to echo it, I want to assign it as src for image tag
I tried this:
$image = mysqli_fetch_array($user_images);
$img1 = $image['file_path'];
$imageData = base64_encode(file_get_contents($img1));
$src = 'data: '.mime_content_type($img1).';base64,'.$imageData;
How can I assign $src to image, I tried this but no luck :(
<img id="img1" width="150" height="150" src="' . $src . '"/>
Thanks in advance.
Inside the HTML you still need to echo the value of $src. This should work:
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
Try with PHP tags like below:-
<img id="img1" width="150" height="150" src="<?php echo $src;?>"/>
Reasonable solution I can think of is
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
but the code where you get the image source has to be above the image tag, so you have the $src.
You are using PHP language and in Php, you can't pass variable directly in the Tag you have to set a variable in the PHP environment.
To create Php environment you can right <?php ?> or you can use to echo variable <?= ?> between these PHP tags you can pas your variables to echo and assign or anything else what you want to do.
<img id="img1" width="150" height="150" src="<?= $src ?>"/>

How can I replace and return the replaced text in PHP?

I am shifting an image tag from one place to another. It currently takes 2 regular expressions, one to find the image tag and one to replace it. Can this be done with one regular expression?
<?php
// Always shorttag
$thumbnail_match_result = preg_match('{<\s*img.*?/>}', $thumbnail, $matches);
$thumbnail_tag = array_shift($matches);
$thumbnail_caption = preg_replace('{<\s*img.*?/>}',"", $thumbnail);
?>
<h4><?php print $title ?></h4>
<a title="<?php print $title ?>" href="<?php print $original_image ?>" data-gallery="">
<?php print $thumbnail_tag ?>
</a>
<?php print $thumbnail_caption; ?>
Thumbnail looks like:
<img typeof="foaf:Image" class="img-responsive" src="/files/styles/photocomp_large/public/lovejoymask400mm_0.jpg?itok=pqICHn8s" width="960" height="656" alt="aly" title="title" /> <blockquote class="image-field-caption">test</blockquote>
Use preg_replace_callback:
$thumbnail_tag='';
$thumbnail_caption = preg_replace_callback('{<\s*img.*?/>}', function($m)
use(&$thumbnail_tag) { $thumbnail_tag=$m[0]; return '';}, $thumbnail);
Check values:
echo $thumbnail_tag . "\n";
//=> <img typeof="foaf:Image" class="img-responsive" src="/files/styles/photocomp_large/public/lovejoymask400mm_0.jpg?itok=pqICHn8s" width="960" height="656" alt="aly" title="title" />
echo $thumbnail_caption . "\n";
//=> <blockquote class="image-field-caption">test</blockquote>
You could use two capture groups, one for each part (image tag, text), like this:
preg_match('{(<\s*img.*?/>)(.*)}', $thumbnail, $matches);
Then $matches will contain the complete matched string in index 0, which you don't need, but in index 1 and 2 you will find the captured groups:
print $matches[1]; // <img ... />
print $matches[2]; // <blocknote>text...

PHP file_get_contents with if/else

On this link I was learning about file_get_contents and I'd like to incorporate it into a simple if/else statement but can't seem to get it correct.
I'd like to check if the file exists and if not, display a no_image.jpg graphic.
My struggle is that the tag already has a PHP echo so it is causing the snippet to stop working. How do I format the if/else while still haveing the use the dynamic value from the echo if file_get_contents is true?
Currently I have a simple tag like this:
<img width="200" height="150" src="images/<?php echo(rawurlencode($row['MLS_NUMBER'])); ?>_1.jpg" alt="" align="left" vspace="3" hspace="3" />
Basically:
<?php
if (file_exists(...)) {
$path = ...;
} else {
$path = 'no_image.jpg';
}
?>
<img src="images/<?php echo $path; ?>" />
//add a semicolon after echo $path otherwise it won't work
Assuming you are looking for a jpg file based on $row['MLS_NUMBER']
<img width="200" height="150" src="images/
<?php
if (file_exists($row['MLS_NUMBER']) {
echo(rawurlencode($row['MLS_NUMBER'])).'_1.jpg';
} else {
echo "no_image.jpg";
}
?>" alt="" align="left" vspace="3" hspace="3" />

How to pass a string from PHP into an html tag?

I have a string, an img url, in a php block called $str. And I want to set that string as the img src in an img src but its not working.
<img src="<?php $str ?>" height="50px" width="50px">
how else can i set the src to the $str string?
<img src="<?php echo $str;?>" height="50px" width="50px">
Well, I found correct answer for this problem. Nothing works well from above answers, that codes only print out source string to html page on site.
This works for me (I have my function that return source string of picture):
require_once("../../my_coded_php_functions.php");
<?php echo '<img src="' . getSourcePathOfImage() . '" />' ?>
This site(article) helped me to find and understand solution:
http://php.net/manual/en/faq.html.php
<?php echo '<img src="'.$str.'" height="50px" width="50px">' ?>
Other ways, although is not recommended. You may try :
<img src="<?=$str?>" height="50px" width="50px">
this will work, only if (in php.ini)
short_open_tag = On

Using Wordpress methods in echo - doesn't seem to work correctly?

Not even sure if methods is the correct terminology...
Here is the original working code:
<a href="<?php bloginfo('url'); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo.png" alt="Polished Logo" id="logo"/></a>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
<p id="logo_title"><?php bloginfo('description'); ?></p>
I wanted it to only execute on the homepage, so I wrote this:
<?
if ( $_SERVER["REQUEST_URI"] == '/' ){
echo '<a href="'.bloginfo('url').'">
<img src="'.bloginfo('stylesheet_directory').'/images/logo.png" alt="Polished Logo" id="logo"/></a>
<img src="'.bloginfo('stylesheet_directory').'/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
<p id="logo_title">'.bloginfo('description').'</p>';
}
?>
But it outputs the bloginfo() and the other declarations completely outside the html tags I have created. For instance, with bloginfo('stylesheet_directory') it will display the directory outside the IMG tags I created.
Any ideas? Apparently my syntax isn't correct or something....
bloginfo function directly echoes the output. In this case you should use get_bloginfo to add the returned value to the string and echo the complete string. I believe this will work
<?php
if ( $_SERVER["REQUEST_URI"] == '/' ) {
echo '<a href="'.get_bloginfo('url').'">
<img src="'.get_bloginfo('stylesheet_directory').'/images/logo.png" alt="Polished Logo" id="logo"/></a>
<img src="'.get_bloginfo('stylesheet_directory').'/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
<p id="logo_title">'.get_bloginfo('description').'</p>';
}
?>
Here is a better alternative:
<?php if ( $_SERVER["REQUEST_URI"] == '/' ) { ?>
<a href="<?php bloginfo('url') ?>">
<img src="<?php bloginfo('stylesheet_directory') ?>/images/logo.png" alt="Polished Logo" id="logo"/>
</a>
<img src="<?php bloginfo('stylesheet_directory') ?>/images/separator.png" width="2" height="59" alt="Line" class="logo_line"/>
<p id="logo_title"><?php bloginfo('description') ?></p>
<?php } ?>
I also suggest using the is_home() function provided by wordpress to check for the homepage instead of checking the $_SERVER['REQUEST_URI'] value.
bloginfo() outputs data with echo and returns nothing, so instead of trying to concatenate everything, just output in sequence, e.g.
echo '<a href="';
bloginfo('url');
echo '"><img src="';
bloginfo('stylesheet_directory');
//etc...
Ugly I know, but see answer by Nithesh for a possible alternative.
if you want to get the template path without auto echoing it by the bloginfo() function use:
get_bloginfo( 'stylesheet_directory', 'display' )

Categories