I want to check if the file exists before I use it on the page. From what I understand something like this should work but nothing appears so I am assuming I have not coded it correctly.
<?php
$url = get_bloginfo('template_directory');
$page_id = get_queried_object_id();
if(file_exists( $url . '/images/footerImage' . $page_id . '.png')) { ?>
<img class="footerImage" src="<?php echo get_bloginfo('template_directory'); ?>/images/footerImage<?php echo $page_id; ?>.png" />
<?php } ?>
I want it to check to see if the file exists and if it does create the image tag, if not do nothing. Why is this not showing any images? I know I have the images because it was working before I had the if statement but creating a broken image on a page that I did not have an image for.
get_bloginfo('template_directory') function returns the Absolute URL. But you need to supply the Relative path in order to check the file existence file_exists()
if(file_exists( TEMPLATEPATH . '/images/footerImage' . $page_id . '.png')) { ?>
<img class="footerImage" src="<?php echo get_bloginfo('template_directory'); ?>/images/footerImage<?php echo $page_id; ?>.png" />
<?php } ?>
Related
I want to show a default image if there isn't any record on the table of images. I am using this one, but it shows nothing when the db's table is empty.
<img class="image--cover" id="blah" src="<?php echo $row2['img_local']; ?>" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src='1.png';">
Actually, it doesn't even shows some broken image icon, not even the allocated space for this element. Any thoughts?
Edit: this is the "upload.php" code that i am using to upload the images
<?php
session_start();
include('includes/conexao.php');
$fileinfo=PATHINFO($_FILES["image"]["name"]);
$newFilename=$fileinfo['filename'] ."_". time() . "." . $fileinfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"],"images/perfis/" . $newFilename);
$location="images/perfis/" . $newFilename;
$todas_fotos = mysqli_query($conexao, "select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
if( mysqli_num_rows($todas_fotos) > 0) {
//$path=$location;
//if(unlink($path)) echo "Deleted file ";
mysqli_query($conexao,"UPDATE esc_usuarios_fotos SET img_local = '$location' WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
}
else
{
mysqli_query($conexao,"insert into esc_usuarios_fotos (img_local, img_usu_codigo) values ('$location', '" . $_SESSION['codigo'] . "')");
};
header('location:perfil.php');
?>
you can try
<img class="image--cover" id="blah" src="<?php if(!empty($row2['img_local'])){ echo $row2['img_local'];}else{ echo '1.png';} ?>" alt="Avatar" title="DEFINIDA" >
Also check path of 1.png.. Like images/1.png or img/1.png etc..
we can try
set by-default value to database column img_local (Ex. default.png) , so if user not change image or there is no insert image in database it's remain default.png ,if user change it become user.png , if user remove image it's again update to default.png
Please try the below logic:
<?php
$row2['img_local'] = 'DB_data_folder_Path_to_image_with_image_name.extension';
// You do not have to use above line.
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--cover" id="blah" src="'.$row2['img_local'].'" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src=1.png;">';
} else {
echo 'Show a default image.'; // <img src="path_to_default_image" alt="Default_image"/>
}
?>
My friend,
If you get this from HTML perspective... and put that image in one DIV, your [onerror="";] could work better.
like this example:
<div id="myContainerImg">
<img class="image--cover" id="blah" src="<?php echo $row2['img_local']; ?>" alt="Avatar" title="DEFINIDA" onerror="putNoImage();">
</div>
<script type="text/javascript">
function putNoImage(){
document.getElementById('myContainerImg').innerHTML = 'no image here, or put your own img...';
}
</script>
And, you can see it running here on CodePen.
Hello I'm trying to echo an image on a view page in CodeIgniter but nothing is displayed on the page.
Here I'm making the variable:
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
And here I'm trying to echo the image:
<img src="<?php echo $image;?>">
Intro
This is the most basic php, so what's the addition of this question? Please read the basics of echo here: http://php.net/manual/en/function.echo.php (Example 1).
Learn the basics first!
Solution
1. Assign full html tag to variable and echo full html:
<?php
$image = '<img src="../../images/stoel.jpg" alt="Foo">';
echo $image;
?>
2. Or assign image path to variable and echo concat string:
<?php
$path = '../../images/stoel.jpg';
echo '<img src="' . $path . '" alt="Foo">';
?>
3. Or assign image path to variable and echo only this with php:
<?php
$path = '../../images/stoel.jpg';
?>
<img src="<?= $path; ?>" alt="Foo">
You are inserting full Image tag into src attribute.
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
and
<?php echo $image; ?>
or
<?php $image = "../../images/stoel.jpg"; ?>
and
<img src="<?php echo $image;?>">
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
you try like this
<?php echo $image; ?>
You are already assign full image code in variable so you just need to print your variable:
<img src="<?php echo $image;?>">
To
<?php echo $image;?>
Make sure your images out side of the application folder then you can do something like
application
images
images > stoel.jpg
system
index.php
Use Base url from the url helper
<img src="<?php echo base_url('images/stoel.jpg');?>" />
Make sure you have set the base_url in config.php
$config['base_url'] = 'http://localhost/yourproject/';
You can also use HTML Helper img();
I think this is what you mean. Since you are echoing inside the src attribute, you do not need to store the whole <image>, just the path will do.
<?php
$image = "../../images/stoel.jpg";
?>
<img src="<?php echo $image;?>">
I'm coding a site in WordPress and I'd like to have a bookmark star shown before a course name.
The thing is that the code works fine, I can see that it translates to the right place where the image is, but the image appears to not be found.
Is there any configuration I need to do to make this image to show or anything I'm doing wrong?
Code:
$estrela = get_stylesheet_directory() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory() . '/images/estrela-favorito.png';
if ($isFavorito) {
$img = $estrelaFavorito;
} else {
$img = $estrela;
}
if (is_user_logged_in()) { ?>
<div class="add-remove-bookmark" onclick="addToBookmark(<?php echo $user_id ?>, 'course', <?php echo $course_id ?>)" >
<img src="<?php echo $img; ?>" alt="favoritos" width="20" height="20"/>
</div>
<?php }
Results in:
<div class="add-remove-bookmark" onclick="addToBookmark(x, x, x)">
<img src="/home/xxx/www/wp-content/themes/wplms_child/images/estrela-favorito.png" alt="favoritos" width="20" height="20">
</div>
Edit:
If I change to the code below, it works, but not really best practice:
$estrela = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela.png';
$estrelaFavorito = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela-favorito.png';
Solution:
I've replaced the variables $estrela and $estrelaFavorito for the code below and now it works!
$estrela = get_stylesheet_directory_uri() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory_uri() . '/images/estrela-favorito.png';
Replace:
//Returns an absolute server path
get_stylesheet_directory()
by
// return theme directory url
get_stylesheet_directory_uri()
https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri
I am currently stuck with a situation where I cant get the right line to pass through to make the image show, this is what I have so far:
Controller
// Get image URL and alternate text
$image_filename = "<?php echo base_url('assets/imgs/'". $code ."'.png'); ?>";
$page_data['image_filename'] = $image_filename;
$image_alt = 'Image: ' . $code . '.png';
$page_data['image_alt'] = $image_alt;
$this->load->view('common/header');
$this->load->view('top_nav');
$this->load->view('shop_viewprod', $page_data);
$this->load->view('common/footer');
}
I have to find the image for the product selected that's why I have to use the $code variable, cause it will always change and that's how I get the code for the current item selected.
And then the view
<img src="<?php echo $image_filename; ?>" alt="<?php echo $image_alt; ?>" />
The path to the image is:
assets\imgs\name.png
The thing is, if I do use this in the view, it finds the image
<img src="<?php echo base_url('assets/imgs/fg700s.png'); ?> " alt="<?php echo $image_alt; ?>" />
Just to show you the two side by side in the code
<img src="<?php echo $image_filename; ?>" alt="<?php echo $image_alt; ?>" />
<img src="<?php echo base_url('assets/imgs/fg700s.png'); ?> " alt="<?php echo $image_alt; ?>" />
and the result:
So I reckon its the way I make the image_filename variable and how I use it in the view, but I cant understand what it is I am missing?
Can anyone help me solve this please?
You are not passing the variable(s) to the view.
When you load your view, you are passing a variable, probably $data, to the $this->load->view() function as the second parameter.
It is probably an array.
You should set a key like $data['image_filename'] = base_url[...]; before loading the view.
So I played around with it alil more and came up with this:
In the controller:
$page_data['code'] = $code;
$image_alt = 'Image: ' . $code . '.png';
$page_data['image_alt'] = $image_alt;
In the View
<img src="<?php echo base_url('assets/imgs/'.$code.'.png'); ?>" alt="<?php echo $image_alt; ?>" />
Works like a charm.
I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>